00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iomanip>
00021 #include "dic.h"
00022 #include "tile.h"
00023 #include "rack.h"
00024 #include "round.h"
00025 #include "pldrack.h"
00026 #include "results.h"
00027 #include "player.h"
00028 #include "ai_player.h"
00029 #include "freegame.h"
00030
00031 #include "debug.h"
00032
00033
00034 FreeGame::FreeGame(const Dictionary &iDic): Game(iDic)
00035 {
00036 }
00037
00038
00039 FreeGame::~FreeGame()
00040 {
00041 }
00042
00043
00044 int FreeGame::setRackRandom(int p, bool iCheck, set_rack_mode mode)
00045 {
00046 int res;
00047 do
00048 {
00049 res = helperSetRackRandom(p, iCheck, mode);
00050 } while (res == 2);
00051 return res;
00052 }
00053
00054
00055 int FreeGame::play(const string &iCoord, const string &iWord)
00056 {
00057
00058 Round round;
00059
00060 int res = checkPlayedWord(iCoord, iWord, round);
00061 if (res != 0)
00062 {
00063 return res;
00064 }
00065
00066
00067 m_players[m_currPlayer]->addPoints(round.getPoints());
00068 m_players[m_currPlayer]->endTurn(round, m_history.getSize());
00069
00070
00071 helperPlayRound(round);
00072
00073
00074
00075 endTurn();
00076
00077 return 0;
00078 }
00079
00080
00081 void FreeGame::freegameAI(int n)
00082 {
00083 ASSERT(0 <= n && n < getNPlayers(), "Wrong player number");
00084 ASSERT(!m_players[n]->isHuman(), "AI requested for a human player");
00085
00086 AIPlayer *player = static_cast<AIPlayer*>(m_players[n]);
00087
00088 player->compute(*m_dic, m_board, m_history.getSize());
00089 if (player->changesLetters())
00090 {
00091 helperPass(player->getChangedLetters(), n);
00092 endTurn();
00093 }
00094 else
00095 {
00096 const Round &round = player->getChosenRound();
00097
00098 player->addPoints(round.getPoints());
00099 player->endTurn(round, m_history.getSize());
00100
00101 helperPlayRound(round);
00102 endTurn();
00103 }
00104 }
00105
00106
00107 int FreeGame::start()
00108 {
00109 ASSERT(getNPlayers(), "Cannot start a game without any player");
00110
00111
00112 for (int i = 0; i < getNPlayers(); i++)
00113 {
00114 setRackRandom(i, false, RACK_NEW);
00115 }
00116
00117
00118 m_currPlayer = 0;
00119
00120
00121 if (!m_players[0]->isHuman())
00122 {
00123 freegameAI(0);
00124 }
00125
00126 return 0;
00127 }
00128
00129
00130 int FreeGame::endTurn()
00131 {
00132
00133 if (setRackRandom(m_currPlayer, false, RACK_NEW) == 1)
00134 {
00135
00136 end();
00137 return 1;
00138 }
00139
00140
00141 nextPlayer();
00142
00143
00144 if (!m_players[m_currPlayer]->isHuman())
00145 {
00146 freegameAI(m_currPlayer);
00147 }
00148
00149 return 0;
00150 }
00151
00152
00153
00154 void FreeGame::end()
00155 {
00156 vector<Tile> tiles;
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 for (int i = 0; i < getNPlayers(); i++)
00173 {
00174 if (i != m_currPlayer)
00175 {
00176 const PlayedRack &pld = m_players[i]->getCurrentRack();
00177 pld.getAllTiles(tiles);
00178 for (unsigned int j = 0; j < tiles.size(); j++)
00179 {
00180 m_players[i]->addPoints(- tiles[j].getPoints());
00181 m_players[m_currPlayer]->addPoints(tiles[j].getPoints());
00182 }
00183 }
00184 }
00185
00186
00187 m_finished = true;
00188 }
00189
00190
00191 int FreeGame::pass(const string &iToChange, int n)
00192 {
00193 if (m_finished)
00194 return 3;
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 vector<Tile> tilesVect;
00205 for (unsigned int i = 0; i < iToChange.size(); i++)
00206 {
00207 Tile tile(toupper(iToChange[i]));
00208 tilesVect.push_back(tile);
00209 }
00210
00211 int res = helperPass(tilesVect, n);
00212 if (res == 0)
00213 endTurn();
00214 return res;
00215 }
00216
00217
00218 int FreeGame::helperPass(const vector<Tile> &iToChange, int n)
00219 {
00220 ASSERT(0 <= n && n < getNPlayers(), "Wrong player number");
00221
00222
00223
00224 Bag bag;
00225 realBag(bag);
00226 if (bag.nTiles() < 7)
00227 {
00228 return 1;
00229 }
00230
00231 Player *player = m_players[n];
00232 PlayedRack pld = player->getCurrentRack();
00233 Rack rack;
00234 pld.getRack(rack);
00235
00236 for (unsigned int i = 0; i < iToChange.size(); i++)
00237 {
00238
00239 if (!rack.in(iToChange[i]))
00240 {
00241 return 2;
00242 }
00243 rack.remove(iToChange[i]);
00244 }
00245
00246 pld.reset();
00247 pld.setOld(rack);
00248
00249 player->setCurrentRack(pld);
00250
00251
00252
00253
00254 return 0;
00255 }
00256
00257
00258
00259
00260
00261
00262