twopart.cpp
来自「一个扑克牌游戏集合的源码,包含了很多基本c-c++语言应用」· C++ 代码 · 共 1,255 行 · 第 1/4 页
CPP
1,255 行
removePlayer (i); return cPlayers;}//-----------------------------------------------------------------------------/// Converts a position in the played staple into the number of the player/// \param pos: Position to convert/// \param start: First player of round/// \returns unsigned int: Number of player//-----------------------------------------------------------------------------unsigned int Twopart::pos2Player (unsigned int pos) const { TRACE9 ("Twopart::pos2Player (unsigned int) - Pos to convert: " << pos << "; starting with player " << startPlayer); unsigned int start (startPlayer); while (pos) { ++start &= 0x3; if (bfPlayers & (1 << start)) --pos; } TRACE9 ("Twopart::pos2Player (unsigned int) - Calculated player: " << start); return start;}//-----------------------------------------------------------------------------/// Method to move the cards of the actual round to the winner/// \param receiver: Nr. of player getting all played cards/// \param start: Startposition of cards to move//-----------------------------------------------------------------------------void Twopart::movePlayedCardsToPlayer (unsigned int receiver, unsigned int start) { TRACE8 ("Twopart::movePlayedCardsToPlayer () - " << (played.size () - start) << " cards for player " << receiver << " til position " << start); Check3 (receiver < NUM_PLAYERS); Check3 (start < played.size ()); movePile (((gameStatus () == PLAYING) ? players[receiver].won : players[receiver].hand), played, start); if (!receiver && (gameStatus () == PLAYING)) enableWonCards (players[0].won); if (gameStatus () == PLAYING2) players[receiver].hand.sort (compByColourAccTrumps);}//-----------------------------------------------------------------------------/// Remove cards from everything which can hold them//-----------------------------------------------------------------------------void Twopart::clean () { staple.clear (); // Clear staple for (unsigned int i (0); i < NUM_PLAYERS; ++i) { // Clear cards of players players[i].hand.clear (); players[i].won.clear (); } played.clear (); disableHuman (); staple.show (); if (pTrump) { remove (*pTrump); delete pTrump; pTrump = NULL; } Game::clean ();}//-----------------------------------------------------------------------------/// Starts part two of the game/// \param player: Player starting part II/// \returns int: Value indicating if timer should continue//-----------------------------------------------------------------------------bool Twopart::startPartTwoTimerFnc (unsigned int player) { TRACE9 ("Twopart::startPartTwoTimerFnc (unsigned int) - Continuing with " << player); Check3 (!bfPlayers); setGameStatus (PLAYING2); disableWonCards (); // Prepare array for sorting according to trumps Check3 (pTrump); for (unsigned int i (0); i < 4; ++i) sortOrder[i] = (i - pTrump->colour () + 3) & 0x3; Check3 (sortOrder[pTrump->colour ()] == 3); startPlayer = -1U; unsigned int nrPlayers (0); // Check if there are players without cards for (unsigned int i (0); i < NUM_PLAYERS; ++i) if (!players[i].won.size ()) { TRACE5 ("Twopart::startPartTwoTimerFnc (unsigned int) - Player " << i << " has no cards"); bfPlayers |= 1 << i; ++nrPlayers; } // Now move the cards from the played pile to the hand; if there are // players without cards give them the cards up to 5 unsigned int victim (player); for (unsigned int i (0); i < NUM_PLAYERS; ++i) { for (unsigned int j (players[i].won.size ()); j; --j) { CardWidget& card (players[i].won.removeTopCard ()); TRACE8 ("Twopart::startPartTwoTimerFnc (unsigned int) - Moving cards " << card << " for player " << i); if (bfPlayers && (card.number () <= CardWidget::FIVE)) { players[pos2Player (++victim)].hand.append (card); victim %= nrPlayers; } else players[i].hand.append (card); } } // Finally sort and show the cards for (unsigned int i (0); i < NUM_PLAYERS; ++i) { players[i].hand.sort (compByColourAccTrumps); players[i].hand.setStyle (i ? ICardPile::VERY_COMPRESSED : ICardPile::COMPRESSED); } bfPlayers = (1 << NUM_PLAYERS) - 1; pos1Play = pos2Play = -1U; setNextPlayer (player); makeNextMoves (); return false;}//-----------------------------------------------------------------------------/// Compares the cards in the pile with regard of the colour and with special/// consideration of trumps/// \param a: Card to compare/// \param b: Card to compare/// \returns bool: True, if a < b//-----------------------------------------------------------------------------bool Twopart::compByColourAccTrumps (const CardWidget* a, const CardWidget* b) { Check3 (a); Check3 (b); TRACE9 ("Twopart::compByColourAccTrumps (const CardWidget*, const CardWidget*) - " << *a << " < " << *b << " = " << ((a->colour () == b->colour ()) ? a->number () < b->number () : (sortOrder[a->colour ()] <sortOrder[b->colour ()]))); return ((a->colour () == b->colour ()) ? a->number () < b->number () : (sortOrder[a->colour ()] < sortOrder[b->colour ()]));}//-----------------------------------------------------------------------------/// Starts part two of the game/// \param player: Player to start part II//-----------------------------------------------------------------------------void Twopart::startPartTwo (unsigned int player) { TRACE8 ("Twopart::startPartTwo (unsigned int) - Continuing with " << player); Glib::signal_timeout ().connect (bind (mem_fun (*this, &Twopart::startPartTwoTimerFnc), player), 50); disableHuman ();}//-----------------------------------------------------------------------------/// Shows or hides the cards of the computer player/// \param open: Flag if cards should be shown or hidden//-----------------------------------------------------------------------------void Twopart::playOpen (bool open) { ICardPile::ShowOpt show (open ? ICardPile::SHOWFACE : ICardPile::SHOWBACK); for (unsigned int i (0); i < NUM_PLAYERS; ++i) { players[i].won.setShowOption (show); players[i].won.setStyle (open ? ICardPile::COMPRESSED : ICardPile::VERY_COMPRESSED); if (i) players[i].hand.setStyle ((gameStatus () == PLAYING2) ? (open ? ICardPile::COMPRESSED : ICardPile::VERY_COMPRESSED) : ICardPile::COMPRESSED); players[i].hand.setShowOption (i ? show : ICardPile::SHOWFACE); }}//-----------------------------------------------------------------------------/// Finds a bigger card, with respect to trumps/// \param pile: Pile to analyze/// \param nr: Number of card to beat/// \returns int: Pos to play (or -1, if no card is bigger)//-----------------------------------------------------------------------------int Twopart::findBigger (const ICardPile& pile, CardWidget::NUMBERS nr) const { // Find first bigger (or equal) card without checking for trumps int pos (pile.findFirstEqualOrBigger (nr)); // Now check if there's a bigger trump if (pTrump && (pos != -1)) { unsigned int newPos (pos); while (++newPos < pile.size ()) if (pile[newPos]->colour () == pTrump->colour ()) { pos = newPos; break; } } TRACE3 ("Twopart::findBigger (const ICardPile&, CardWidget::NUMBERS) const - Pos " << pos); return pos;}//-----------------------------------------------------------------------------/// Changes the names of the playing people/// \param newPlayer: Array holding the new player//-----------------------------------------------------------------------------void Twopart::changeNames (const std::vector<Player*>& newPlayer) { Game::changeNames (newPlayer); for (unsigned int i (0); i < NUM_PLAYERS; ++i) players[i].name.set_text (actPlayers[i]->getName ());}//----------------------------------------------------------------------------/// Converts a pile-number to the actual pile/// \param newPlayer: Array holding the new player/// \param pile: ID of the pile to return/// \returns ICardPile*: Pile corresponding to the passed number or NULL//----------------------------------------------------------------------------ICardPile* Twopart::getPileOfPlayer (unsigned int player, unsigned int pile) { TRACE8 ("Twopart::getPileOfPlayer (unsigned int, unsigned int) - Player " << player << "; Pile " << pile); if ((player >= NUM_PLAYERS) || (pile > 1)) return NULL; return &(pile ? played : players[player].hand);}//----------------------------------------------------------------------------/// Handles the messages the server might send for the Twopart cardgame/// \param player: ID of player sending the message/// \param message: Message received from the server/// \returns bool: True, if message has been completey processed/// \throw YGP::ParseError, YGP::CommError: In case of an error an describing text//----------------------------------------------------------------------------bool Twopart::handleMessage (unsigned int player, const std::string& message) throw (YGP::ParseError, YGP::CommError) { TRACE1 ("Twopart::handleMessage (unsigned int player, const std::string&) - " << message << " (" << player << ')'); YGP::Tokenize command (message); std::string cmd (command.getNextNode ('=')); bool rc (Game::handleMessage (player, message)); if (cmd == "ActPlayer") { TRACE1 ("Twopart::handleMessage (unsigned int player, const std::string&) - " "Next player: " << currentPlayer ()); startPlayer = currentPlayer (); makeNextMoves (); return true; } return rc;}//----------------------------------------------------------------------------/// Executes the remote move locally/// \param pile: Pile to move to/from/// \param target: ID of target as send by the partner/// \pre Expects \c pos1Play and \c pos2Play to be set to the positions to play/// \throw YGP::ParseError: In case of an error//----------------------------------------------------------------------------bool Twopart::executeRemoteMove (ICardPile& pile, unsigned int target) throw (YGP::ParseError) { Check2 (pos1Play != -1U); Check2 (pos2Play != -1U); if (target) { TRACE7 ("Twopart::executeRemoteMove (ICardPile&, unsigned int) - Target " << target); Check3 (gameStatus () == PLAYING2); pos1Play = pos2Play = -1U; setNextPlayer (pickUpPlayedPile (currentPlayer ())); return false; } return Game::executeRemoteMove (pile, target);}//-----------------------------------------------------------------------------/// Adds game-specific menus/// \param mgrUI: UIManager to add to//-----------------------------------------------------------------------------void Twopart::addMenus (Glib::RefPtr<Gtk::UIManager> mgrUI) { Check1 (mgrUI); Glib::ustring ui ("<menubar name='Menu'>" " <placeholder name='GameMenu'>" " <menu action='MB'>" " <menuitem action='TwopartSort'/>" " <menuitem action='TwopartSortCol'/>" " </menu></placeholder></menubar>"); Glib::RefPtr<Gtk::ActionGroup> grpAction (Gtk::ActionGroup::create ()); grpAction->add (Gtk::Action::create ("MB", _("_Twopart"))); grpAction->add (Gtk::Action::create ("TwopartSort", Gtk::Stock::SORT_ASCENDING, _("_Sort won cards (by number)")), Gtk::AccelKey ("<shft>S"), mem_fun (*this, &Twopart::sortWonByNumber)); grpAction->add (Gtk::Action::create ("TwopartSortCol", Gtk::Stock::SORT_ASCENDING, _("Sort won cards (by _colour)")), Gtk::AccelKey ("S"), mem_fun (*this, &Twopart::sortWonByColour)); mgrUI->insert_action_group (grpAction); idMrg = mgrUI->add_ui_from_string (ui);}//-----------------------------------------------------------------------------/// Removes the game-specific menus/// \param mgrUI: UIManager to remove from//-----------------------------------------------------------------------------void Twopart::removeMenus (Glib::RefPtr<Gtk::UIManager> mgrUI) { Check1 (mgrUI); mgrUI->remove_ui (idMrg);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?