⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 machiavelli.cpp

📁 一个扑克牌游戏集合的源码,包含了很多基本c-c++语言应用
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//$Id: Machiavelli.cpp,v 1.45 2006/08/02 04:54:48 markus Rel $//PROJECT     : Cardgames//SUBSYSTEM   : Machiavelli//REFERENCES  ://TODO        ://BUGS        ://REVISION    : $Revision: 1.45 $//AUTHOR      : Markus Schwab//CREATED     : 05.11.2003//COPYRIGHT   : Copyright (C) 2003 - 2006// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#include <cardgames-cfg.h>#include <sstream>#include <gtk/gtkdnd.h>#include <gtkmm/stock.h>#include <gtkmm/statusbar.h>#include <YGP/Check.h>#include <YGP/Trace.h>#include <YGP/ConnMgr.h>#include <YGP/ANumeric.h>#include <YGP/Tokenize.h>#include <YGP/AttrParse.h>#include <YGP/StatusObj.h>#include <XGP/MessageDlg.h>#include <Player.h>#include <CardSet.h>#include <CardWidget.h>#include <ComputerPlayer.h>#include "Machiavelli.h"// Some Windows-header seems to define ERROR#ifdef ERROR#  undef ERROR#endifenum { HAND, TABLE };static std::vector<Gtk::TargetEntry> dndTypeHand;static std::vector<Gtk::TargetEntry> dndTypeTable;static std::vector<Gtk::TargetEntry> dndTypeBoth;//-----------------------------------------------------------------------------/// Constructor/// \param parent: Parent widget to display the game in/// \param statusbar: Status bar widget to display information about the game/// \param cardset: Cardset to use/// \param player: Vector of player/// \param posPlayer: Position of player for the server/// \param mxSerialize: Mutex to serialize messages from the server//-----------------------------------------------------------------------------Machiavelli::Machiavelli (Gtk::Box& parent, Gtk::Statusbar& statusbar,                CardSet& cardset, const std::vector<Player*>& player,                unsigned int posPlayer, YGP::Mutex& mxSerialize)   : Game (parent, statusbar, cardset, player, posPlayer, mxSerialize, 3, 10)     , startPlayer (-1U), newPile (_("New pile"))     , staple (ICardPile::TOTALLY_COMPRESSED, ICardPile::SHOWBACK)     , nextTurn (_("_End turn"), true)     , target (-1U), undoDlg (NULL) {   TRACE9 ("Machiavelli::Machiavelli (Box&, Statusbar&, CardSet&, const "           "std::vector<Glib::ustring>&)");   int width (cards.getCard (0).getImageWidth ());   int height (cards.getCard (0).getImageHeight ());   TRACE9 ("Machiavelli::Machiavelli (Box&, Statusbar&, CardSet&, const "           "std::vector<Glib::ustring>&) - Init common staples");   staple.set_size_request (width, height);   for (unsigned int i (1); i < NUM_PLAYERS; ++i) {      attach (hands[i], (i << 2) - 4, (i << 2), 4, 5,              Gtk::EXPAND, Gtk::SHRINK, 5, 5);      attach (names[i], (i << 2) - 4, (i << 2), 5, 6,              Gtk::EXPAND, Gtk::SHRINK, 0);      hands[i].setStyle (ICardPile::QUITE_COMPRESSED);      hands[i].setShowOption (ICardPile::SHOWBACK);   }   hands[0].setStyle (ICardPile::COMPRESSED);   hands[0].setShowOption (ICardPile::SHOWFACE);   TRACE9 ("Machiavelli::Machiavelli (Box&, Statusbar&, CardSet&, const "           "std::vector<Glib::ustring>&) - Attach widgets");   attach (hands[0], 3, 12, 0, 1, Gtk::EXPAND, Gtk::SHRINK, 1, 5);   attach (names[0], 3, 12, 1, 2, Gtk::EXPAND, Gtk::SHRINK, 1, 5);   attach (staple, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 5);   attach (nextTurn, 0, 1, 1, 2, Gtk::FILL, Gtk::SHRINK, 5);   attach (newPile, 0, 12, 2, 3, Gtk::EXPAND | Gtk::FILL, Gtk::FILL, 0, 5);   attach (piles, 0, 12, 3, 4, Gtk::EXPAND | Gtk::FILL,           Gtk::EXPAND | Gtk::FILL, 0, 5);   TRACE9 ("Machiavelli::Machiavelli (Box&, Statusbar&, CardSet&, const "           "std::vector<Glib::ustring>&) - Show widgets");   changeNames (player);   if (dndTypeHand.empty ()) {      Check3 (dndTypeTable.empty ());      Check3 (dndTypeBoth.empty ());      dndTypeHand.push_back         (Gtk::TargetEntry ("icon/card/hand", Gtk::TARGET_SAME_APP, HAND));      dndTypeTable.push_back         (Gtk::TargetEntry ("icon/card/table", Gtk::TARGET_SAME_APP, TABLE));      dndTypeBoth.push_back (dndTypeHand.front ());      dndTypeBoth.push_back (dndTypeTable.front ());   }   nextTurn.set_flags (Gtk::CAN_DEFAULT);   nextTurn.grab_default ();   nextTurn.set_sensitive (false);   nextTurn.signal_clicked ().connect (mem_fun (*this, (&Machiavelli::endTurn)));   show_all_children ();}//-----------------------------------------------------------------------------/// Destructor//-----------------------------------------------------------------------------Machiavelli::~Machiavelli () {   TRACE9 ("Machiavelli::~Machiavelli ()");   clean ();}//-----------------------------------------------------------------------------/// Starts the game by dealing the cards//-----------------------------------------------------------------------------void Machiavelli::start () {   TRACE9 ("Machiavelli::start ()");   Game::start ();   pos1Play = pos2Play = 0;   target = -1U;   if (randomizeCardsToPile (staple)) {      for (unsigned int i (0); i < NUM_PLAYERS; ++i)          for (unsigned int j (0); j < 7; ++j)             hands[(i - posServer) & 0x3].append (staple.removeTopCard ());      hands[0].sortByColour ();      for (unsigned int i (1); i < NUM_PLAYERS; ++i) {         hands[i].setStyle (ICardPile::QUITE_COMPRESSED);         hands[i].setShowOption (ICardPile::SHOWBACK);         hands[i].sortByNumber ();      }      status.pop ();      status.push (_("You can sort the cards in your hand with drag and drop or put"                     " them on the table - click the staple to end turn"));      if (getConnectionMgr ().getMode () != YGP::ConnectionMgr::CLIENT) {         // Set random startplayer (if not already set)         if (startPlayer == -1U)            startPlayer = rand () & 0x3;         setStartPlayer ();      }   }}//-----------------------------------------------------------------------------/// Remove cards from everything which can hold them//-----------------------------------------------------------------------------void Machiavelli::clean () {   TRACE9 ("Machiavelli::clean ()");   disableHuman ();   for (unsigned int i (0); i < NUM_PLAYERS; ++i)      hands[i].clear ();   staple.clear ();   for (std::vector<MachiPile*>::iterator i (tablePiles.begin ());        i != tablePiles.end (); ++i) {      (*i)->clear ();      piles.remove (**i);   }   tablePiles.clear ();   Game::clean ();}//-----------------------------------------------------------------------------/// Shows or hides the cards of the computer player/// \param open: Flag if cards should be shown or hidden//-----------------------------------------------------------------------------void Machiavelli::playOpen (bool open) {    for (unsigned int i (1); i < NUM_PLAYERS; ++i) {      hands[i].setShowOption (open ? ICardPile::SHOWFACE : ICardPile::SHOWBACK);      hands[i].setStyle (open ? ICardPile::COMPRESSED : ICardPile::QUITE_COMPRESSED);    }}//-----------------------------------------------------------------------------/// Makes the move for the next player./// \param player: Actual player/// \returns \c int: Next player or -1 if end of game/// \remarks This method expects the target pile to play in the target-member///     and the positions to play in pos1Play and pos2Play//-----------------------------------------------------------------------------int Machiavelli::makeMove (unsigned int player) {   TRACE5 ("Machiavelli::makeMove (unsigned int) - Turn of player " << player           << "; Target: " << std::hex << (int)target << std::dec);   Check1 (player); Check1 (player < NUM_PLAYERS);   Check1 (gameStatus () == PLAYING);   if (target == -1U) {      target = showCardsToPlay (player);      TRACE8 ("Machiavelli::makeMove (unsigned int) - Going to play cards to "              << std::hex << (int)target << std::dec);      // No more cards found: Continue with next player      if (target == -1U) {         if (getConnectionMgr ().getMode () != YGP::ConnectionMgr::NONE) {            // Send played card to all clients (if any)            if (getConnectionMgr ().getMode () == YGP::ConnectionMgr::CLIENT)               ++ignoreNextMsg;            broadcastMessage ("EndTurn");         }         unsigned int nextPlayer (findNextPlayer (player));         if (nextPlayer == findNextPlayer (nextPlayer)) {            endGame (nextPlayer);            return -1;         }         displayTurn (player = nextPlayer);         dealCard (player);      }      else {         Check3 (pos1Play <= pos2Play);         Check3 (pos2Play <= hands[player].size ());         if (pos2Play < hands[player].size ())            flipCards2Play (hands[player], pos1Play, pos2Play);      }   }   else {      TRACE4 ("Machiavelli::makeMove (unsigned int) - Moving cards to pile " << target);      unsigned int pos (target & 0xffff);      target >>= 16;      TRACE8 ("Machiavelli::makeMove (unsigned int) - Pile " << target              << "; Size: " << tablePiles.size ());      Check3 (target < tablePiles.size ());      // Check if cards have to be moved from the table      while (posPiles.size ()) {         unsigned int posPile (posPiles.back ());         unsigned int pile ((posPile >> 8) & 0xff);         unsigned int nr (posPile >> 16);         unsigned int posSrc (posPile & 0xff);         TRACE9 ("Machiavelli::makeMove (unsigned int) - Add from " << pile                 << " cards " << posSrc << '-' << (posSrc + nr - 1));         Check3 (pile < tablePiles.size ());         Check3 ((posSrc + nr) <= tablePiles[pile]->size ());         while (nr--) {            CardWidget& card (tablePiles[pile]->remove (posSrc));            card.unmark ();            tablePiles[target]->insert (card, pos++);         }         if (tablePiles[pile]->empty ())            removePile (pile);         posPiles.pop_back ();      }      if (pos2Play < hands[player].size ()) {         pos = tablePiles[target]->getPosition4Card (*hands[player][pos1Play]);         // getPosition4Card can't handle a coloured pair with a gap         if (pos == -1U) {            Check3 (((tablePiles[target]->size () == 2)                     && (MachiPile::cardDistance (*(*tablePiles[target])[1],                                                 *(*tablePiles[target])[0]) == 2))                    || (tablePiles[target]->size () == 1));            pos = (tablePiles[target]->size () == 1) ? 0 : 1;         }         Check3 (pos != -1U);      }      Check1 (pos1Play <= pos2Play);      if (pos2Play < hands[player].size ()) {#if CHECK > 2         for (unsigned int t (pos1Play); t < pos2Play; ++t) {            int diff (MachiPile::cardDistance (*hands[player][t + 1], *hands[player][t],                                               (t == pos1Play) ? MachiPile::BOTH : MachiPile::ACE));            TRACE1 ("Buraco::makeMove (unsigned int) - Card " << *hands[player][t]);            Check3 ((diff == 0) || (diff == 1));         }#endif         // Move the played cards to the pile to play         Check3 (hands[player].size () > pos2Play);         for (; (int)pos1Play <= (int)pos2Play; --pos2Play)            tablePiles[target]->insert (hands[player].remove (pos1Play), pos++);      }      target = -1U;#if CHECK > 2      if (typeid (*actPlayers[player]) == typeid (ComputerPlayer)) {         YGP::StatusObject obj;         checkPiles (obj);         if (obj.getType () != YGP::StatusObject::UNDEFINED) {            TRACE1 ("Machiavelli::makeMove (unsigned int) - Invalid piles!\n"		    << obj.getMessage ());            Check (!"Valid piles");         }      }#endif   }   return player;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -