training.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 1999-2005 Eliot
00003  * Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
00004  *          Olivier Teuliere  <ipkiss@via.ecp.fr>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  *****************************************************************************/
00020 
00021 #include "dic.h"
00022 #include "tile.h"
00023 #include "rack.h"
00024 #include "round.h"
00025 #include "pldrack.h"
00026 #include "player.h"
00027 #include "training.h"
00028 
00029 #include "debug.h"
00030 
00031 
00032 Training::Training(const Dictionary &iDic): Game(iDic)
00033 {
00034 }
00035 
00036 
00037 Training::~Training()
00038 {
00039 }
00040 
00041 int Training::setRackRandom(int p, bool iCheck, set_rack_mode mode)
00042 {
00043     int res;
00044     m_results.clear();
00045     do
00046     {
00047         res = helperSetRackRandom(p, iCheck, mode);
00048     } while (res == 2);
00049     // 0 : ok
00050     // 1 : not enough tiles
00051     // 2 : check failed (number of voyels before round 15)
00052     return res;
00053 }
00054 
00055 int Training::setRackManual(bool iCheck, const string &iLetters)
00056 {
00057     int res;
00058     int p = m_currPlayer;
00059     string::iterator it;
00060     string uLetters; // uppercase letters
00061     // letters can be lowercase or uppercase as they are
00062     // coming from user input. We do not consider a lowercase
00063     // letter to be a joker which has been assigned to a letter.
00064     m_results.clear();
00065     uLetters = iLetters;
00066     for(it = uLetters.begin(); it != uLetters.end(); it ++)
00067       {
00068         *it = toupper(*it);
00069       }
00070     res = helperSetRackManual(p, iCheck, uLetters);
00071     // 0 : ok
00072     // 1 : not enough tiles
00073     // 2 : check failed (number of voyels before round 15)
00074     return res;
00075 }
00076 
00077 int Training::setRack(set_rack_mode iMode, bool iCheck, const string &iLetters)
00078 {
00079     int res = 0;
00080     switch(iMode)
00081         {
00082         case RACK_MANUAL:
00083             res = setRackManual(iCheck, iLetters);
00084             break;
00085         case RACK_ALL:
00086             res = setRackRandom(m_currPlayer, iCheck, iMode);
00087             break;
00088         case RACK_NEW:
00089             res = setRackRandom(m_currPlayer, iCheck, iMode);
00090             break;
00091         }
00092     return res;
00093 }
00094 
00095 int Training::play(const string &iCoord, const string &iWord)
00096 {
00097     /* Perform all the validity checks, and fill a round */
00098     Round round;
00099     int res = checkPlayedWord(iCoord, iWord, round);
00100     if (res != 0)
00101     {
00102         return res;
00103     }
00104 
00105     /* Update the rack and the score of the current player */
00106     m_players[m_currPlayer]->addPoints(round.getPoints());
00107     m_players[m_currPlayer]->endTurn(round, m_history.getSize());
00108 
00109     /* Everything is OK, we can play the word */
00110     helperPlayRound(round);
00111 
00112     /* Next turn */
00113     // XXX: Should it be done by the interface instead?
00114     endTurn();
00115 
00116     return 0;
00117 }
00118 
00119 
00120 int Training::start()
00121 {
00122     if (getNPlayers() != 0)
00123         return 1;
00124 
00125     // Training mode implicitly uses 1 human player
00126     Game::addHumanPlayer();
00127     m_currPlayer = 0;
00128     return 0;
00129 }
00130 
00131 int Training::endTurn()
00132 {
00133     // Nothing to do?
00134     return 0;
00135 }
00136 
00137 
00138 void Training::search()
00139 {
00140     // Search for the current player
00141     Rack r;
00142     m_players[m_currPlayer]->getCurrentRack().getRack(r);
00143     debug("Training::search for %s\n",r.toString().c_str());
00144     m_results.search(*m_dic, m_board, r, m_history.getSize());
00145 }
00146 
00147 
00148 int Training::playResult(int n)
00149 {
00150     Player *player = m_players[m_currPlayer];
00151     if (n >= m_results.size())
00152         return 2;
00153     const Round &round = m_results.get(n);
00154 
00155     /* Update the rack and the score of the current player */
00156     player->addPoints(round.getPoints());
00157     player->endTurn(round, m_history.getSize());
00158 
00159     int res = helperPlayRound(round);
00160 
00161     if (res == 0)
00162         m_results.clear();
00163 
00164     /* Next turn */
00165     // XXX: Should it be done by the interface instead?
00166     endTurn();
00167 
00168     return res;
00169 }
00170 
00171 
00172 void Training::addHumanPlayer()
00173 {
00174     // We are not supposed to be here...
00175     ASSERT(false, "Trying to add a human player in Training mode");
00176 }
00177 
00178 
00179 void Training::addAIPlayer()
00180 {
00181     // We are not supposed to be here...
00182     ASSERT(false, "Trying to add a AI player in Training mode");
00183 }
00184 
00185 
00186 void Training::testPlay(int num)
00187 {
00188     ASSERT(0 <= num && num < m_results.size(), "Wrong result number");
00189     m_testRound = m_results.get(num);
00190     m_board.testRound(m_results.get(num));
00191 }
00192 
00193 
00194 void Training::removeTestPlay()
00195 {
00196     m_board.removeTestRound();
00197     m_testRound = Round();
00198 }
00199 
00200 std::string Training::getTestPlayWord() const
00201 {
00202     return m_testRound.getWord();
00203 }
00204 
00205 /****************************************************************/
00206 /****************************************************************/
00207 
00208 /// Local Variables:
00209 /// mode: c++
00210 /// mode: hs-minor
00211 /// c-basic-offset: 4
00212 /// indent-tabs-mode: nil
00213 /// End:

Generated on Thu Dec 29 02:01:14 2005 for Eliot by  doxygen 1.4.5