player.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE games lskat program
00003    Copyright (c) 2006 Martin Heni <kde@heni-online.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // Qt includes
00022 
00023 // KDE includes
00024 #include <klocale.h>
00025 #include <kdebug.h>
00026 
00027 // Local includes
00028 #include "player.h"
00029 #include "abstractinput.h"
00030 #include "deck.h"
00031 
00032 
00033 
00034 // Constructor for the player
00035 Player::Player(int id, QObject* parent)
00036     : QObject(parent)
00037 {
00038   mId     = id;
00039   mCards.clear();
00040   mPoints = 0;
00041   mDeck   = 0;
00042   mInput  = 0;
00043   mTrump  = Club;
00044   
00045   // Reset internal variables - they are set by 'load' 
00046   setName("");
00047   mScore         = 0;
00048   mNumberOfGames = 0;
00049   mGamesWon      = 0;
00050 }
00051 
00052 
00053 // Destructor 
00054 Player::~Player()
00055 {
00056   if (mInput) delete mInput;
00057 }
00058 
00059 
00060 // Save properties
00061 void Player::save(KConfigGroup& config)
00062 {
00063   config.writeEntry("name", mName);
00064   config.writeEntry("gameswon", mGamesWon);
00065   config.writeEntry("score", mScore);
00066   config.writeEntry("noofgames", mNumberOfGames);
00067 }
00068 
00069 
00070 // Load properties
00071 void Player::load(KConfigGroup& config)
00072 {
00073   mName          = config.readEntry("name", mName);
00074   mGamesWon      = config.readEntry("gameswon", mGamesWon);
00075   mScore         = config.readEntry("score", mScore);
00076   mNumberOfGames = config.readEntry("noofgames", mNumberOfGames);
00077 
00078   // Emit signals
00079   refresh();
00080 
00081 }
00082 
00083 
00084 // Set the deck for drawing cards.
00085 void Player::setDeck(Deck* deck)
00086 {
00087   mDeck = deck;
00088 }
00089 
00090 
00091 // Clear the all time statistics of this player.
00092 void Player::clear()
00093 {
00094   mNumberOfGames = 0;
00095   mScore = 0;
00096   mGamesWon = 0;
00097   refresh();
00098 }
00099 
00100 
00101 // Deal a number of cards to this player 
00102 void Player::deal(int amount)
00103 {
00104   if (!mDeck) 
00105   {
00106     kFatal() << "No deck set to player." << endl;
00107     return;
00108   }
00109   mCards.clear();
00110   mCards.resize(amount);
00111   for (int i=0; i<amount; i++)
00112   {
00113     int card = mDeck->drawCard();
00114     mCards[i] = card;
00115   }
00116 
00117   // Reset moves and points
00118   mMovesWon = 0;
00119   setPoints(0);
00120   mWonCards.clear();
00121 
00122   refresh();
00123 }
00124 
00125 
00126 // Retrieve the input device of the player 
00127 AbstractInput* Player::input()
00128 {
00129   return mInput;
00130 }
00131 
00132 
00133 // Set the input device of the player 
00134 void Player::setInput(AbstractInput* input)
00135 {
00136   // Try to set the same turn status after changing input
00137   bool oldTurnAllowed = false;
00138   // Get rid of old input devive if existing
00139   if (mInput) 
00140   {
00141     oldTurnAllowed = mInput->inputAllowed();
00142     mInput->setInputAllowed(false);
00143     delete mInput;
00144   }
00145   // Store new input
00146   mInput = input;
00147   // Store player
00148   mInput->setId(mId);
00149   // Restore turn status
00150   mInput->setInputAllowed(oldTurnAllowed);
00151 
00152   refresh();
00153 }
00154 
00155 
00156 // Set this player to start a turn 
00157 void Player::startTurn()
00158 {
00159   mInput->setInputAllowed(true);
00160 }
00161 
00162 
00163 // Set this player to stop a turn 
00164 void Player::stopTurn()
00165 {
00166   mInput->setInputAllowed(false);
00167 }
00168    
00169 
00170 // Remove a card from the given position. Typically if the
00171 void Player::deleteCard(int cardPosition)
00172 {
00173   if (cardPosition >= mCards.size())
00174   {
00175     kFatal() << "Player " << mId << " tries to delete non esxisting card position " 
00176               << cardPosition <<" >= " << mCards.size() << endl;
00177   }
00178   mCards[cardPosition] = -1;
00179 }
00180 
00181 
00182 // Add a card to the player
00183 void Player::addCard(int cardPosition, int card)
00184 {
00185   if (cardPosition >= mCards.size())
00186   {
00187     kFatal() << "Player " << mId << " tries to add to esxisting card position " 
00188               << cardPosition <<" >= " << mCards.size() << endl;
00189   }
00190   mCards[cardPosition] = card;
00191 }
00192 
00193 
00194 // Retrive card value at given logical position
00195 int Player::getCard(int playerNumber)
00196 {
00197   if (playerNumber >= mCards.size())
00198   {
00199     kFatal() << "Player " << mId << " tries to get non esxisting card " 
00200               << playerNumber <<" >= " << mCards.size() << endl;
00201   }
00202   
00203   int card = mCards[playerNumber];
00204   return card;
00205 }
00206 
00207 
00208 // Increases the numebr of moves one for this player
00209 void Player::increaseMovesWon(int amount)
00210 {
00211   mMovesWon += amount;
00212 }
00213 
00214 
00215 // Retrive the number of won moves for this player
00216 int Player::noOfMovesWon()
00217 {
00218   return mMovesWon;
00219 }
00220 
00221 
00222 // Adds a card which is one in a move to this player.
00223 void Player::addWonCard(int card)
00224 {
00225   // Store card
00226   mWonCards.append(card);
00227 
00228   // Add points
00229   int value = mDeck->getCardValue(card);
00230   setPoints(points() + value);
00231 }
00232 
00233 
00234 // Retrieve a card won by this player.
00235 int Player::getWonCard(int no)
00236 {
00237   if (no>=mWonCards.size())
00238   {
00239     kFatal() << "Player::getWonCard This card " << no << " is not available. "
00240               << "Only " << mWonCards.size() << " cards stored." << endl;
00241     return -1;
00242   }
00243   return mWonCards[no];
00244 }
00245 
00246 
00247 // Retrive the amount of points this player has
00248 int Player::points()
00249 {
00250   return mPoints;
00251 }
00252 
00253 
00254 // Set the points of the player.
00255 void Player::setPoints(int points)
00256 {
00257   mPoints = points;
00258   refresh();
00259 }
00260 
00261 
00262 // Retrive the player's name
00263 QString Player::name()
00264 {
00265   return mName;
00266 }
00267 
00268 
00269 // Set the name of the player.
00270 void Player::setName(QString name)
00271 {
00272   mName = name;
00273   refresh();
00274 }
00275 
00276 
00277 // Add a number of won games to the overall statistic
00278 void Player::addWonGame(int amount)
00279 {
00280   mGamesWon += amount;
00281   refresh();
00282 }
00283 
00284 
00285 // Get amount of won games
00286 int Player::wonGames()
00287 {
00288   return mGamesWon;
00289 }
00290 
00291 
00292 // Add a number of games to the overall statistic
00293 void Player::addGame(int amount)
00294 {
00295   mNumberOfGames += amount;
00296   refresh();
00297 }
00298 
00299 
00300 // Get number of games
00301 int Player::games()
00302 {
00303   return mNumberOfGames;
00304 }
00305 
00306 
00307 // Add a score to the overall statistic
00308 void Player::addScore(int amount)
00309 {
00310   mScore += amount;
00311   refresh();
00312 }
00313 
00314 // Get score
00315 int Player::score()
00316 {
00317   return mScore;
00318 }
00319 
00320 
00321 // Set trump
00322 void Player::setTrump(Suite trump)
00323 {
00324   mTrump = trump;
00325   refresh();
00326 }
00327 
00328 
00329 // Get trump
00330 Suite Player::trump()
00331 {
00332   return mTrump;
00333 }
00334 
00335 
00336 // Emit all signals for GUI
00337 void Player::refresh()
00338 {
00339   emit signalUpdate(this);
00340 }
00341 
00342 #include "player.moc"

Generated on Tue May 1 09:34:40 2007 for LSkat by  doxygen 1.4.6