📄 game.cpp
字号:
/// move and re-enable receiving of messages/// \param player: ID of remote player/// \returns bool: False//-----------------------------------------------------------------------------bool Game::endRemoteMove (unsigned int player) { TRACE8 ("Game::endRemoteMove () - " << player); actPlayer = makeMove (player); mxSerializeMsgs.unlock (); stati.pendingTurn = 0; makeNextMoves (); return false;}//-----------------------------------------------------------------------------/// Enables the cards of the human player//-----------------------------------------------------------------------------bool Game::enableHuman () { Check3 (!actPlayer); TRACE8 ("Game::enableHuman () - enabling " << actPlayers[0]->getName ()); return false;}//-----------------------------------------------------------------------------/// Makes the move for the next player./// \returns \c int: Flag for timer, if it should continue (0: no; else: yes)//-----------------------------------------------------------------------------bool Game::makeComputerMove () { TRACE5 ("Game::makeComputerMove () - Turn of player " << actPlayer); if (statGame == TOSTOP) { stop (); stati.pendingTurn = 0; return false; } unsigned int newPlayer (makeMove (actPlayer)); TRACE7 ("Game::makeComputerMove () - Next player: " << newPlayer); if (static_cast<int> (newPlayer) == actPlayer) return true; else { actPlayer = newPlayer; stati.pendingTurn = 0; makeNextMoves (); return false; }}//-----------------------------------------------------------------------------/// Displays information about whose turn it is/// \param player: Player in turn//-----------------------------------------------------------------------------void Game::displayTurn (unsigned int player) { Check1 (player < actPlayers.size ()); status.pop (); Glib::ustring stat (_("Turn of %1")); stat.replace (stat.find ("%1"), 2, actPlayers[player]->getName ()); status.push (stat);}//-----------------------------------------------------------------------------/// Displays information about whose turn it is/// \param player: Player in turn//-----------------------------------------------------------------------------void Game::displayTurn (unsigned int player, const Glib::ustring& preText) { status.pop (); Glib::ustring stat (_("Turn of %1")); stat.replace (stat.find ("%1"), 2, actPlayers[player]->getName ()); status.push (preText + stat);}//-----------------------------------------------------------------------------/// Changes the game-status/// \param newStatus: Status to set//-----------------------------------------------------------------------------void Game::setGameStatus (unsigned int newStatus) { statGame = newStatus; control (statGame);}//-----------------------------------------------------------------------------/// Flips the cards the user is about to play/// \param pile: Pile to manipulate/// \param cards: String containing the (comma-separated) IDs of the cards to flip/// \throw YGP::ParseError: If invalid numbers for cards are found//-----------------------------------------------------------------------------void Game::flipCards2Play (ICardPile& pile, const std::string& cards) throw (YGP::ParseError) { TRACE2 ("Game::flipCards2Play (ICardPile&, const std::string&) - Cards " << cards); Check1 (cards.size ()); YGP::Tokenize tokCards (cards); unsigned long card (0); unsigned int cCards (0); bool bFollow (false); while (tokCards.getNextNode (' ').size ()) { if (stringToNumber (card, tokCards.getActNode ().c_str ())) { std::string error (N_("Invalid card specification!")); throw YGP::ParseError (error); } card = pile.find (static_cast <unsigned int> (card)); if ((card != -1U) && (card < (pile.size () - cCards))) { Check3 (card < pile.size ()); TRACE9 ("Game::flipCards2Play (ICardPile&, const std::string&) - Found " << card << " = " << *pile[card]); ++cCards; CardWidget& cardWg (*pile[card]); pile.move (pile.size () - 1, card); cardWg.showFace (); if ((pile.getStyle () != ICardPile::NORMAL) && bFollow) { Check3 (pile.size () > 1); pile.resize (pile.size () - 2, ICardPile::COMPRESSED); } bFollow = true; } else { TRACE1 ("Game::flipCards2Play (ICardPile&, const std::string&) - " "Card " << tokCards.getActNode () << " not found in " << pile.size () << " cards"); std::string error ("Card not found!"); throw YGP::ParseError (error); } } // end-while string has data pos2Play = pile.size () - 1; pos1Play = pos2Play - cCards + 1; Check3 (pos1Play <= pos2Play); TRACE8 ("Game::flipCards2Play (ICardPile&, const std::string&) - " "New positions " << pos1Play << " and " << pos2Play);}//----------------------------------------------------------------------------/// Returns the actual target, where flipCard2Play should position the cards to/// \returns unsigned int: ID of the target//----------------------------------------------------------------------------unsigned int Game::getActTarget () const { return 0;}//-----------------------------------------------------------------------------/// Flips the cards the user is about to play/// \param pile: Pile to manipulate/// \param start: Position of first card to play; update to reflect moving/// \param start: Position of last card to play; update to reflect moving//-----------------------------------------------------------------------------void Game::flipCards2Play (ICardPile& pile, unsigned int& start, unsigned int& end) { TRACE2 ("Game::flipCards2Play (ICardPile&, unsigned int, unsigned int) - " "Cards " << start << " - " << end); Check3 (end < pile.size ()); Check3 (start <= end); unsigned int s (start); unsigned int e (end); bool bFollow (false); // Inform clients about cards to play if (getConnectionMgr ().getMode () == YGP::ConnectionMgr::SERVER) { std::ostringstream msg; msg << "Play="; for (unsigned int i (start); i < end; ++i) msg << pile[i]->id () << ' '; msg << pile[end]->id () << ";Target=" << getActTarget (); broadcastMessage (msg.str ()); } do { CardWidget& card (*pile[s]); pile.move (pile.size () - 1, s); card.showFace (); if ((pile.getStyle () != ICardPile::NORMAL) && bFollow) { Check3 (pile.size () > 1); pile.resize (pile.size () - 2, ICardPile::COMPRESSED); } bFollow = true; } while (e-- && (s <= e)); start = pile.size () - 1 - (end - start); end = pile.size () - 1; TRACE8 ("Game::flipCards2Play (ICardPile&, unsigned int, unsigned int) - " "New positions " << start << " and " << end);}//-----------------------------------------------------------------------------/// Shows or hides the won cards/// \param show: Flag if to show or to hide the cards/// \param style: Style how pile should be displayed; must be a value understood/// by ICardPile::setStyle//-----------------------------------------------------------------------------void Game::showWonCards (bool show, unsigned int style) { if (pWonPile) { if (style == -1U) style = show ? ICardPile::COMPRESSED : ICardPile::VERY_COMPRESSED; Check3 (style < ICardPile::LAST); pWonPile->setShowOption (show ? ICardPile::SHOWFACE : ICardPile::SHOWBACK); pWonPile->setStyle ((ICardPile::PileStyle)style); Glib::signal_idle ().connect (mem_fun (*this, &Game::enableActWonCards)); disableWonCards (); }}//-----------------------------------------------------------------------------/// Callback for any event for the top of the won cards/// \param event: Caused event//-----------------------------------------------------------------------------bool Game::wonCardsSelected (GdkEvent* event) { TRACE9 ("Game::wonCardsSelected (GdkEvent*) - " << event->type); if (event->type == GDK_BUTTON_PRESS) { GdkEventButton* bev ((GdkEventButton*)(event)); switch (bev->button) { case 1: Check3 (pWonPile); showWonCards (pWonPile->getShowOption () == ICardPile::SHOWBACK); break; case 3: { if (!pMenuPopSort) { TRACE9 ("Game::wonCardsSelected (GdkEvent*) - Creating menu"); pMenuPopSort = new Gtk::Menu; pMenuPopSort->items ().push_back (Gtk::Menu_Helpers::MenuElem (_("Sort by _number"), mem_fun (*this, &Game::sortWonByNumber))); pMenuPopSort->items ().push_back (Gtk::Menu_Helpers::MenuElem (_("Sort by _colour"), mem_fun (*this, &Game::sortWonByColour))); } pMenuPopSort->popup (bev->button, bev->time); break; } } return true; } return false;}//-----------------------------------------------------------------------------/// Shows and sorts the won cards by number//-----------------------------------------------------------------------------void Game::sortWonByNumber () { TRACE8 ("Game::sortWonByNumber ()"); if (pWonPile) { pWonPile->sortByNumber (); showWonCards (); Glib::signal_idle ().connect (mem_fun (*this, &Game::enableActWonCards)); disableWonCards (); }}//-----------------------------------------------------------------------------/// Shows and sorts the won cards by colour//-----------------------------------------------------------------------------void Game::sortWonByColour () { TRACE8 ("Game::sortWonByColour ()"); if (pWonPile) { pWonPile->sortByColour (); showWonCards (); Glib::signal_idle ().connect (mem_fun (*this, &Game::enableActWonCards)); disableWonCards (); }}//-----------------------------------------------------------------------------/// Enables the actual won cards//-----------------------------------------------------------------------------bool Game::enableActWonCards () { disableWonCards (); Check3 (pWonPile); TRACE9 ("Game::enableActWonCards () - Enabling " << pWonPile->size () << " cards"); for (int i (pWonPile->size ()); i;) wonCards.push_back
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -