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

📄 cardset.cpp

📁 一个扑克牌游戏集合的源码,包含了很多基本c-c++语言应用
💻 CPP
字号:
//$Id: CardSet.cpp,v 1.16 2004/01/15 05:53:48 markus Rel $//PROJECT     : Cardgames//SUBSYSTEM   : Common//REFERENCES  ://TODO        ://BUGS        ://REVISION    : $Revision: 1.16 $//AUTHOR      : Markus Schwab//CREATED     : 8.5.2002//COPYRIGHT   : Copyright (C) 2002 - 2004// 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 <cstdlib>#include <YGP/Check.h>#include <YGP/Trace.h>#include "CardImgs.h"#include "CardWidget.h"#include "CardSet.h"//-----------------------------------------------------------------------------/// Destructor//-----------------------------------------------------------------------------CardSet::~CardSet () {   TRACE9 ("CardSet::~CardSet ()");   clear ();}//-----------------------------------------------------------------------------/// Adds a set of cards (with images specified by decks) to set/// \param decks: Class holding the images to add//-----------------------------------------------------------------------------void CardSet::addPacket (const CardImages& decks) {   TRACE9 ("CardSet::addPacket (const CardImages&)");   for (unsigned int i (0); i < decks.size (); ++i) {      CardWidget* card (new CardWidget (decks, i, true)); Check3 (card);      card->show ();      cards_.push_back (card);   } // endfor}//-----------------------------------------------------------------------------/// Shuffles the cards in the deck//-----------------------------------------------------------------------------void CardSet::shuffle () {   TRACE2 ("CardSet::shuffle ()");   unsigned int nr;   for (int i (size ()); i > 0;) {      nr = rand () % i--;      TRACE2 ("CardSet::shuffle () - " << i << " = " << nr);      std::swap (cards_[i], cards_[nr]);   }}//-----------------------------------------------------------------------------/// Sets the specified card in the passed slot of the set/// \param pos: Slot (position) of the card to set/// \param card: Card to swap/// \pre The \c card must not be in a position before \c pos.//-----------------------------------------------------------------------------void CardSet::set (unsigned int pos, unsigned int card) {   TRACE2 ("CardSet::set (unsigned int, unsigned int) - [" << pos << "] = " << card);   Check1 (card < cards_.size ());   for (std::vector<CardWidget*>::iterator i (cards_.begin () + pos);        i != cards_.end (); ++i)      if ((*i)->id () == card) {         std::vector<CardWidget*>::iterator t (cards_.begin () + pos);         std::swap (*t, *i);         return;       }   Check3 (0);}//-----------------------------------------------------------------------------/// Retrieves the specified card/// \returns \c CardWidget&: Reference to CardWidget//-----------------------------------------------------------------------------CardWidget& CardSet::getCard (unsigned int nr) const {   TRACE3 ("CardSet::getCard (unsigned int) - " << nr);   Check3 (nr < size ()); Check3 (cards_[nr]);   return *cards_[nr];}//-----------------------------------------------------------------------------/// Actualizes the card set (after changes of the images)//-----------------------------------------------------------------------------void CardSet::update () const {   TRACE3 ("CardSet::update () const");   for (std::vector<CardWidget*>::const_iterator i (cards_.begin ());        i != cards_.end (); ++i)      (*i)->update ();}//-----------------------------------------------------------------------------/// Removes all cards from the set//-----------------------------------------------------------------------------void CardSet::clear () {   for (std::vector<CardWidget*>::iterator i (cards_.begin ());        i != cards_.end (); ++i)      delete *i;   cards_.clear ();}

⌨️ 快捷键说明

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