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

📄 sgtmayor.cpp

📁 一个扑克牌游戏集合的源码,包含了很多基本c-c++语言应用
💻 CPP
📖 第 1 页 / 共 4 页
字号:
			  == typeid (RemotePlayer))) {		     Glib::ustring msg (_("Waiting for %1 to exchange cards ..."));		     msg.replace (msg.find ("%1"), 2, actPlayers[convertPlayer (i % NUM_PLAYERS)]->getName ());		     status.push (msg);		     return;		  }		  else		     exchangeCards (i % NUM_PLAYERS, (i + j) % NUM_PLAYERS);	       }	       else {		  Check3 (diffTicks[0] > 0);		  displayExchangeStatus ();		  for (int i (players[0].hand.size () - 1); i >= 0; --i)		     activeCards.push_back			(players[0].hand[i]->signal_clicked ().connect			 (bind (mem_fun (*this, (&SgtMayor::cardExchange)), i)));		  return;	       }	    }	 }      }   }   if (!humanExchange)      startPlaying ();}//-----------------------------------------------------------------------------/// Displays the number of cards the human can exchange and with whom//-----------------------------------------------------------------------------void SgtMayor::displayExchangeStatus () {   Glib::ustring msg;   unsigned int exchg (1);   if ((diffTicks[1] < 0) && (diffTicks[2] < 0)) {      msg = _("You can exchange %1 %2; %4 with %3 (and than %5 with %6)!");      msg.replace (msg.find ("%4"), 2, 1, (char)((diffTicks[1] < 0) ? ('0' - diffTicks[1]) : ('0' + diffTicks[1])));      msg.replace (msg.find ("%5"), 2, 1,  (char)((diffTicks[2] < 0) ? ('0' - diffTicks[2]) : ('0' + diffTicks[2])));      msg.replace (msg.find ("%6"), 2, actPlayers[convertPlayer (2)]->getName ());   }   else {      msg = _("You can exchange %1 %2 with %3!");      if (diffTicks[2] < 0)	 exchg = 2;   }   msg.replace (msg.find ("%1"), 2, 1, (char)((diffTicks[0] < 0) ? ('0' - diffTicks[0]) : ('0' + diffTicks[0])));   msg.replace (msg.find ("%2"), 2, (ngettext ("bad card", "bad cards", (diffTicks[0] < 0) ? -diffTicks[0] : diffTicks[0])));   msg.replace (msg.find ("%3"), 2, actPlayers[convertPlayer (exchg)]->getName ());   status.pop ();   status.push (msg);}//----------------------------------------------------------------------------/// Exchanges a good card from playerGood with a bad card from player bad/// \param playerBad: Player giving away a bad card/// \param playerGood: Player giving away a good card//----------------------------------------------------------------------------void SgtMayor::exchangeCards (unsigned int playerBad, unsigned int playerGood) {   TRACE7 ("SgtMayor::exchangeCards (unsigned int, unsigned int) - Players "           << playerBad << " and " << playerGood);   Check1 (playerBad < NUM_PLAYERS);   Check1 (playerGood < NUM_PLAYERS);   unsigned int posBad (players[playerBad].hand.findLowestCard ());   Check3 (posBad < players[playerBad].hand.size ());   exchangeCards (playerBad, posBad, playerGood);}//-----------------------------------------------------------------------------/// Exchanges cards directly; e.g. between computer players/// \param playerBad: Player giving away a bad card/// \param posBad: Position of bad card to give away/// \param playerGood: Player giving away a good card/// \param posGood: Position of good card to give away//-----------------------------------------------------------------------------void SgtMayor::directExchange (unsigned int playerBad, unsigned int posBad,			       unsigned int playerGood, unsigned int posGood) {   CardWidget& bad (players[playerBad].hand.remove (posBad));   CardWidget& good (players[playerGood].hand.remove (posGood));   players[playerBad].hand.insertColourSorted (good);   players[playerGood].hand.insertColourSorted (bad);   TRACE9 ("SgtMayor::directExchange (4x unsigned int ) - Player "           << playerBad << " and " << playerGood << " exchange " << bad           << " and " << good);}//-----------------------------------------------------------------------------/// Exchanges cards indirectly; e.g. between a human and a  computer player/// \param playerBad: Player giving away a bad card/// \param posBad: Position of bad card to give away/// \param playerGood: Player giving away a good card/// \param posGood: Position of good card to give away//-----------------------------------------------------------------------------void SgtMayor::delayedExchange (unsigned int playerBad, unsigned int posBad,				unsigned int playerGood, unsigned int posGood) {   Check1 (playerBad < NUM_PLAYERS);   Check1 (playerGood < NUM_PLAYERS);   Check2 (!(playerBad && playerGood));   Check2 (posBad < players[playerBad].hand.size ());   Check2 (posGood < players[playerGood].hand.size ());   // For easier handling: playerBad is human   if (playerBad) {      std::swap (playerGood, playerBad);      std::swap (posGood, posBad);   }   Check3 (playerGood); Check3 (!playerBad);   CardWidget& good (players[playerGood].hand.remove (posGood));   CardWidget* bad (&players[0].hand.remove (posBad));   TRACE9 ("SgtMayor::delayedExchange (4x unsigned int ) - Player "           << playerBad << " and " << playerGood << " exchange " << *bad           << " and " << good);   players[0].hand.insertColourSorted (good);   players[playerGood].hand.insertColourSorted (*bad);   good.mark ();   Glib::signal_timeout ().connect      (bind (mem_fun (*this, &SgtMayor::unmarkExchanged), &good), 1000);   bad = new CardWidget (*bad);   bad->show ();   bad->showFace ();   played.append (*bad);}//----------------------------------------------------------------------------/// Exchanges a good card from playerGood with a bad card from player bad/// \param playerBad: Player giving away a bad card/// \param posBad: Position of bad card to give away/// \param playerGood: Player giving away a good card//----------------------------------------------------------------------------void SgtMayor::exchangeCards (unsigned int playerBad, unsigned int posBad,                              unsigned int playerGood) {   TRACE7 ("SgtMayor::exchangeCards (3x unsigned int int) - Players "           << playerBad << " and " << playerGood);   Check1 (playerBad < NUM_PLAYERS);   Check1 (playerGood < NUM_PLAYERS);   Check1 (posBad < players[playerBad].hand.size ());   unsigned int posGood (players[playerGood].hand.findLastEqualOrBiggerColour                         (players[playerBad].hand[posBad]->colour ()));   if (posGood == -1U)      posGood = players[playerGood].hand.findLowestCard ();   TRACE9 ("SgtMayor::exchangeCards (3x unsigned int) - Player "           << playerBad << ", card " << posBad << " with "           << playerGood << "'s " << posGood);   Check3 (posGood < players[playerGood].hand.size ());   exchangeCards (playerBad, posBad, playerGood, posGood);}//----------------------------------------------------------------------------/// Exchanges a good card from playerGood with a bad card from player bad/// and informs the connected partners about it/// \param playerBad: Player giving away a bad card/// \param posBad: Position of bad card to give away/// \param playerGood: Player giving away a good card//----------------------------------------------------------------------------void SgtMayor::exchangeCards (unsigned int playerBad, unsigned int posBad,                              unsigned int playerGood, unsigned int posGood) {   TRACE7 ("SgtMayor::exchangeCards (4x unsigned int int) - Players "           << playerBad << " and " << playerGood);   Check1 (playerBad < NUM_PLAYERS);   Check1 (playerGood < NUM_PLAYERS);   Check1 (posBad < players[playerBad].hand.size ());   Check1 (posGood < players[playerGood].hand.size ());   // Broadcast exchange-info to others   if (getConnectionMgr ().getMode () != YGP::ConnectionMgr::NONE) {      std::ostringstream msg;      msg << "Exchange=" << players[playerGood].hand[posGood]->id ()	  << ";From=" << (playerGood + posServer) % NUM_PLAYERS          << ";With=" << players[playerBad].hand[posBad]->id ()	  << ";From=" << (playerBad + posServer) % NUM_PLAYERS;      if (getConnectionMgr ().getMode () == YGP::ConnectionMgr::CLIENT)         ignoreNextMsg = true;      broadcastMessage (msg.str ());   }   doExchangeCards (playerBad, posBad, playerGood, posGood);}//----------------------------------------------------------------------------/// Exchanges a good card from playerGood with a bad card from player bad/// \param playerBad: Player giving away a bad card/// \param posBad: Position of bad card to give away/// \param playerGood: Player giving away a good card/// \param posGood: Position of good card to give away//----------------------------------------------------------------------------void SgtMayor::doExchangeCards (unsigned int playerBad, unsigned int posBad,				unsigned int playerGood, unsigned int posGood) {   TRACE7 ("SgtMayor::doExchangeCards (4x unsigned int int) - Players "           << playerBad << " and " << playerGood);   Check1 (playerBad < NUM_PLAYERS);   Check1 (playerGood < NUM_PLAYERS);   Check1 (posBad < players[playerBad].hand.size ());   Check1 (posGood < players[playerGood].hand.size ());   TRACE9 ("SgtMayor::doExchangeCards (4x unsigned int ) - Player "           << playerBad << " and " << playerGood << " exchange "	   << *players[playerBad].hand[posBad] << " and "	   << *players[playerGood].hand[posGood]);   Check2 ((players[playerGood].hand[posGood]->colour ()	    == players[playerBad].hand[posBad]->colour ())	   || (!players[playerGood].hand.exists (players[playerBad].hand[posBad]->colour ())));   --diffTicks[playerBad];   ++diffTicks[playerGood];   if (playerBad && playerGood)      directExchange (playerBad, posBad, playerGood, posGood);   else      delayedExchange (playerBad, posBad, playerGood, posGood);}//----------------------------------------------------------------------------/// Exchanges the passed card with the first one in the played area (if any)/// \param cardHuman: Pointer to (marked) card the human received/// \return bool: Always false to end the timer//----------------------------------------------------------------------------bool SgtMayor::unmarkExchanged (CardWidget* cardHuman) {   TRACE9 ("SgtMayor::unmarkExchanged (CardWidget*, unsigned int)");   Check1 (cardHuman); Check3 (played.size ());   cardHuman->unmark ();   CardWidget* card (0);   played.remove (0);   delete card;   if (played.empty ()) {      if (*diffTicks || diffTicks[1])	 makeExchange ();      else	 startPlaying ();   }   return false;}//----------------------------------------------------------------------------/// Unmarks the passed card/// \param card: Position of card to unmark/// \return bool: Always false to end the timer//----------------------------------------------------------------------------bool SgtMayor::unmark (unsigned int card) {   Check2 (card < players[0].hand.size ());   TRACE9 ("SgtMayor::unmark (unsigned int) - Unmarking " << card);   players[0].hand.at (card)->unmark ();   return false;}//-----------------------------------------------------------------------------/// Adds game-specific menus/// \param mgrUI: UIManager to add to//-----------------------------------------------------------------------------void SgtMayor::addMenus (Glib::RefPtr<Gtk::UIManager> mgrUI) {   Check1 (mgrUI);   Glib::ustring ui ("<menubar name='Menu'>"		     "  <placeholder name='GameMenu'>"		     "    <menu action='MB'>"		     "      <menuitem action='SgMayorSort'/>"		     "      <menuitem action='SgMayorSortCol'/>"		     "    </menu></placeholder></menubar>");   Glib::RefPtr<Gtk::ActionGroup> grpAction (Gtk::ActionGroup::create ());   grpAction->add (Gtk::Action::create ("MB", _("_Sgt. Mayor")));   grpAction->add (Gtk::Action::create ("SgMayorSort", Gtk::Stock::SORT_ASCENDING,					_("_Sort won cards (by number)")),		   Gtk::AccelKey ("<shft>S"),		   mem_fun (*this, &SgtMayor::sortWonByNumber));   grpAction->add (Gtk::Action::create ("SgMayorSortCol", Gtk::Stock::SORT_ASCENDING,					_("Sort won cards (by _colour)")),		   Gtk::AccelKey ("S"),		   mem_fun (*this, &SgtMayor::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 SgtMayor::removeMenus (Glib::RefPtr<Gtk::UIManager> mgrUI) {   Check1 (mgrUI);   mgrUI->remove_ui (idMrg);}//-----------------------------------------------------------------------------/// Shows or hides the won cards/// \param show: Flag if to show or to hide the cards//-----------------------------------------------------------------------------void SgtMayor::showWonCards (bool show, unsigned int style) {   TRACE9 ("SgtMayor::showWonCards (bool, unsigned int) - " << show << '/' << style);   for (CardVPile::iterator c (players[0].won.begin ());	c != players[0].won.end (); ++c)      if (show)	 (*c)->show ();      else	 if (((c - players[0].won.begin ()) % 3) != 2)	    (*c)->hide ();   if (show) {      Game::showWonCards (true);   }   else      Game::showWonCards (false, ICardPile::QUITE_COMPRESSED);}

⌨️ 快捷键说明

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