2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Copyright (C) 1999-2005 Eliot
|
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
|
|
|
|
* Olivier Teuliere <ipkiss@via.ecp.fr>
|
|
|
|
*
|
|
|
|
* 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
|
2005-10-23 16:53:42 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-02-05 12:14:56 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "dic.h"
|
|
|
|
#include "dic_search.h"
|
|
|
|
#include "tile.h"
|
|
|
|
#include "rack.h"
|
|
|
|
#include "round.h"
|
|
|
|
#include "pldrack.h"
|
|
|
|
#include "results.h"
|
|
|
|
#include "player.h"
|
2005-02-17 21:01:59 +01:00
|
|
|
#include "ai_percent.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "game.h"
|
2005-02-24 09:06:24 +01:00
|
|
|
#include "game_factory.h"
|
2005-11-06 00:22:41 +01:00
|
|
|
#include "turn.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
2005-02-26 23:57:34 +01:00
|
|
|
const int Game::RACK_SIZE = 7;
|
|
|
|
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
Game::Game(const Dictionary &iDic):
|
|
|
|
m_dic(&iDic)
|
|
|
|
{
|
2005-03-29 00:07:22 +02:00
|
|
|
m_variant = kNONE;
|
2005-02-05 12:14:56 +01:00
|
|
|
m_points = 0;
|
|
|
|
m_currPlayer = -1;
|
|
|
|
m_finished = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Game::~Game()
|
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
for (unsigned int i = 0; i < m_history.size(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
delete m_history[i];
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
for (int i = 0; i < getNPlayers(); i++)
|
|
|
|
{
|
|
|
|
delete m_players[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-05 16:48:59 +01:00
|
|
|
const Player& Game::getPlayer(int num) const
|
|
|
|
{
|
|
|
|
ASSERT(0 <= num && num < (int)m_players.size(), "Wrong player number");
|
|
|
|
return *(m_players[num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
Game * Game::load(FILE *fin, const Dictionary &iDic)
|
|
|
|
{
|
|
|
|
char buff[4096];
|
|
|
|
char delim[] = " \t\n|";
|
|
|
|
char *token;
|
|
|
|
|
|
|
|
// Check characteristic string
|
|
|
|
if (fgets(buff, sizeof(buff), fin) == NULL)
|
|
|
|
return NULL;
|
|
|
|
if ((token = strtok(buff, delim)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (string(token) != IDENT_STRING)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
int num;
|
|
|
|
char rack[20];
|
|
|
|
char word[20];
|
|
|
|
char ref[4];
|
|
|
|
int pts;
|
|
|
|
int player;
|
|
|
|
char *pos;
|
|
|
|
Tile tile;
|
|
|
|
Game *pGame = NULL;
|
|
|
|
|
|
|
|
while (fgets(buff, sizeof(buff), fin))
|
|
|
|
{
|
|
|
|
// Indication of game type
|
|
|
|
pos = strstr(buff, "Game type: ");
|
|
|
|
if (pos != NULL)
|
|
|
|
{
|
|
|
|
// No Game object should have been created yet
|
|
|
|
if (pGame != NULL)
|
|
|
|
{
|
|
|
|
delete pGame;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Create the correct Game object
|
|
|
|
if (strstr(buff, "Training"))
|
2005-02-24 09:06:24 +01:00
|
|
|
pGame = GameFactory::Instance()->createTraining(iDic);
|
2005-02-05 12:14:56 +01:00
|
|
|
else if (strstr(buff, "Free game"))
|
2005-02-24 09:06:24 +01:00
|
|
|
pGame = GameFactory::Instance()->createFreeGame(iDic);
|
|
|
|
else if (strstr(buff, "Duplicate"))
|
|
|
|
pGame = GameFactory::Instance()->createDuplicate(iDic);
|
2005-02-05 12:14:56 +01:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
// Read next line
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Players type
|
|
|
|
pos = strstr(buff, "Player ");
|
|
|
|
if (pos != NULL && pGame != NULL)
|
|
|
|
{
|
|
|
|
int nb = 0;
|
|
|
|
char type[20];
|
|
|
|
if (sscanf(pos, "Player %d: %19s", &nb, type) > 1)
|
|
|
|
{
|
|
|
|
if (string(type) == "Human")
|
|
|
|
pGame->addHumanPlayer();
|
|
|
|
else if (string(type) == "Computer")
|
|
|
|
pGame->addAIPlayer();
|
|
|
|
else
|
|
|
|
;
|
|
|
|
}
|
|
|
|
// Read next line
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last racks
|
|
|
|
pos = strstr(buff, "Rack ");
|
|
|
|
if (pos != NULL && pGame != NULL)
|
|
|
|
{
|
|
|
|
int nb = 0;
|
|
|
|
char letters[20];
|
|
|
|
if (sscanf(pos, "Rack %d: %19s", &nb, letters) > 1)
|
|
|
|
{
|
|
|
|
// Create the played rack
|
|
|
|
PlayedRack pldrack;
|
|
|
|
char *r = letters;
|
|
|
|
if (strchr(r, '+'))
|
|
|
|
{
|
|
|
|
while (*r != '+')
|
|
|
|
{
|
|
|
|
pldrack.addOld(Tile(*r));
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
while (*r)
|
|
|
|
{
|
|
|
|
pldrack.addNew(Tile(*r));
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give the rack to the player
|
2005-02-09 23:33:56 +01:00
|
|
|
pGame->m_players[nb]->setCurrentRack(pldrack);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
// Read next line
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip columns title
|
|
|
|
if (strstr(buff, "==") != NULL ||
|
|
|
|
strstr(buff, "| PTS | P |") != NULL)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string(buff) != "\n" && pGame != NULL)
|
|
|
|
{
|
|
|
|
char bonus = 0;
|
|
|
|
int res = sscanf(buff, " %2d | %8s | %s | %3s | %3d | %1d | %c",
|
|
|
|
&num, rack, word, ref, &pts, &player, &bonus);
|
|
|
|
if (res < 6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Integrity checks
|
|
|
|
// TODO: add more checks
|
|
|
|
if (pts < 0)
|
|
|
|
continue;
|
|
|
|
if (player < 0 || player > pGame->getNPlayers())
|
|
|
|
continue;
|
|
|
|
if (bonus && bonus != '*')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Build a rack for the correct player
|
|
|
|
PlayedRack pldrack;
|
|
|
|
char *r = rack;
|
|
|
|
if (strchr(r, '+'))
|
|
|
|
{
|
|
|
|
while (*r != '+')
|
|
|
|
{
|
|
|
|
pldrack.addOld(Tile(*r));
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*r)
|
|
|
|
{
|
|
|
|
pldrack.addNew(Tile(*r));
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a round
|
|
|
|
Round round;
|
2005-11-05 14:56:59 +01:00
|
|
|
round.accessCoord().setFromString(ref);
|
|
|
|
if (!round.getCoord().isValid())
|
|
|
|
continue;
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
round.setPoints(pts);
|
|
|
|
if (bonus == '*')
|
|
|
|
round.setBonus(1);
|
|
|
|
|
2005-11-05 14:56:59 +01:00
|
|
|
for (unsigned int i = 0; i < strlen(word); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-11-05 14:56:59 +01:00
|
|
|
tile = Tile(word[i]);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2005-11-05 14:56:59 +01:00
|
|
|
if (round.getCoord().getDir() == Coord::HORIZONTAL)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-11-05 14:56:59 +01:00
|
|
|
if (!pGame->m_board.getTile(round.getCoord().getRow(),
|
|
|
|
round.getCoord().getCol() + i).isEmpty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
round.addRightFromBoard(tile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
round.addRightFromRack(tile, islower(word[i]));
|
|
|
|
pGame->m_bag.takeTile((islower(word[i])) ? Tile::Joker() : tile);
|
|
|
|
}
|
|
|
|
}
|
2005-11-05 14:56:59 +01:00
|
|
|
else
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-11-05 14:56:59 +01:00
|
|
|
if (!pGame->m_board.getTile(round.getCoord().getRow() + i,
|
|
|
|
round.getCoord().getCol()).isEmpty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
round.addRightFromBoard(tile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
round.addRightFromRack(tile, islower(word[i]));
|
|
|
|
pGame->m_bag.takeTile((islower(word[i])) ? Tile::Joker() : tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pGame->m_currPlayer = player;
|
|
|
|
// Update the rack for the player
|
2005-02-09 23:33:56 +01:00
|
|
|
pGame->m_players[player]->setCurrentRack(pldrack);
|
2005-02-05 12:14:56 +01:00
|
|
|
// End the turn for the current player (this creates a new rack)
|
|
|
|
pGame->m_players[player]->endTurn(round, num - 1);
|
|
|
|
// Add the points
|
|
|
|
pGame->m_players[player]->addPoints(pts);
|
|
|
|
// Play the round
|
|
|
|
pGame->helperPlayRound(round);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize the game
|
|
|
|
if (pGame)
|
|
|
|
{
|
|
|
|
// We don't really know whose turn it is, but at least we know that
|
|
|
|
// the game was saved while a human was to play.
|
2005-02-12 19:54:57 +01:00
|
|
|
for (int i = 0; i < pGame->getNPlayers(); i++)
|
|
|
|
{
|
|
|
|
if (pGame->m_players[i]->isHuman())
|
|
|
|
{
|
|
|
|
pGame->m_currPlayer = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
return pGame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::save(ostream &out) const
|
|
|
|
{
|
|
|
|
const string decal = " ";
|
|
|
|
// "Header" of the game
|
|
|
|
out << IDENT_STRING << endl << endl;
|
2005-02-12 19:54:57 +01:00
|
|
|
out << "Game type: " << getModeAsString() << endl;
|
2005-02-05 12:14:56 +01:00
|
|
|
for (int i = 0; i < getNPlayers(); i++)
|
|
|
|
{
|
|
|
|
out << "Player " << i << ": ";
|
|
|
|
if (m_players[i]->isHuman())
|
|
|
|
out << "Human" << endl;
|
|
|
|
else
|
|
|
|
out << "Computer" << endl;
|
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
|
|
|
|
// Title of the columns
|
|
|
|
char line[100];
|
|
|
|
out << decal << " N | RACK | SOLUTION | REF | PTS | P | BONUS" << endl;
|
|
|
|
out << decal << "===|==========|=================|=====|=====|===|======" << endl;
|
|
|
|
|
|
|
|
// Print the game itself
|
2005-11-06 00:22:41 +01:00
|
|
|
for (int i = 0; i < getNTurns(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
string word = getPlayedWord(i);
|
|
|
|
string coord = getPlayedCoords(i);
|
|
|
|
sprintf(line, "%2d | %8s | %s%s | %3s | %3d | %1d | %c",
|
|
|
|
i + 1, getPlayedRack(i).c_str(), word.c_str(),
|
|
|
|
string(15 - word.size(), ' ').c_str(),
|
|
|
|
coord.c_str(), getPlayedPoints(i),
|
|
|
|
getPlayedPlayer(i), getPlayedBonus(i) ? '*' : ' ');
|
|
|
|
|
|
|
|
out << decal << line << endl;
|
|
|
|
}
|
|
|
|
out << endl << decal << "Total: " << m_points << endl;
|
|
|
|
|
|
|
|
// Print current rack for all the players
|
|
|
|
out << endl;
|
|
|
|
for (int i = 0; i < getNPlayers(); i++)
|
|
|
|
{
|
2005-11-05 16:48:59 +01:00
|
|
|
string rack = m_players[i]->getCurrentRack().toString();
|
2005-02-05 12:14:56 +01:00
|
|
|
out << "Rack " << i << ": " << rack << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This function plays a round on the board */
|
|
|
|
int Game::helperPlayRound(const Round &iRound)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We remove tiles from the bag only when they are played
|
|
|
|
* on the board. When going back in the game, we must only
|
|
|
|
* replace played tiles.
|
|
|
|
* We test a rack when it is set but tiles are left in the bag.
|
|
|
|
*/
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// History of the game
|
2005-11-06 00:22:41 +01:00
|
|
|
m_history.push_back(new Turn(m_history.size(), m_currPlayer,
|
|
|
|
m_players[m_currPlayer]->getLastRack(),
|
|
|
|
iRound));
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
m_points += iRound.getPoints();
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// Before updating the bag and the board, if we are playing a "joker game",
|
|
|
|
// we replace in the round the joker by the letter it represents
|
2005-04-02 22:46:42 +02:00
|
|
|
// This is currently done by a succession of ugly hacks :-/
|
2005-03-29 00:07:22 +02:00
|
|
|
if (m_variant == kJOKER)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < iRound.getWordLen(); i++)
|
|
|
|
{
|
|
|
|
if (iRound.isPlayedFromRack(i) && iRound.isJoker(i))
|
|
|
|
{
|
|
|
|
// Is the represented letter still available in the bag?
|
|
|
|
// FIXME: this way to get the represented letter sucks...
|
|
|
|
Tile t(toupper(iRound.getTile(i).toChar()));
|
|
|
|
Bag bag;
|
|
|
|
realBag(bag);
|
2005-04-02 22:46:42 +02:00
|
|
|
// FIXME: realBag() does not give us a real bag in this
|
|
|
|
// particular case! This is because Player::endTurn() is called
|
|
|
|
// before Game::helperPlayRound(), which means that the rack
|
|
|
|
// of the player is updated, while the word is not actually
|
|
|
|
// played on the board yet. Since realBag() relies on
|
|
|
|
// Player::getCurrentRack(), it doesn't remove the letters of
|
|
|
|
// the current player, which are in fact available through
|
|
|
|
// Player::getLastRack().
|
|
|
|
// That's why we have to replace the letters of the current
|
|
|
|
// rack and remove the ones from the previous rack...
|
|
|
|
// There is a big design problem here, but i am unsure what is
|
|
|
|
// the best way to fix it.
|
|
|
|
vector<Tile> tiles;
|
|
|
|
m_players[m_currPlayer]->getCurrentRack().getAllTiles(tiles);
|
|
|
|
for (unsigned int j = 0; j < tiles.size(); j++)
|
|
|
|
{
|
|
|
|
bag.replaceTile(tiles[j]);
|
|
|
|
}
|
|
|
|
m_players[m_currPlayer]->getLastRack().getAllTiles(tiles);
|
|
|
|
for (unsigned int j = 0; j < tiles.size(); j++)
|
|
|
|
{
|
|
|
|
bag.takeTile(tiles[j]);
|
|
|
|
}
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
if (bag.in(t))
|
|
|
|
{
|
|
|
|
// FIXME: A const_cast sucks too...
|
|
|
|
const_cast<Round&>(iRound).setTile(i, t);
|
|
|
|
// FIXME: This shouldn't be necessary either, this is only
|
|
|
|
// needed because of the stupid way of handling jokers in
|
|
|
|
// rounds
|
|
|
|
const_cast<Round&>(iRound).setJoker(i, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the bag and the board
|
|
|
|
for (int i = 0; i < iRound.getWordLen(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
if (iRound.isPlayedFromRack(i))
|
|
|
|
{
|
|
|
|
if (iRound.isJoker(i))
|
2005-03-29 00:07:22 +02:00
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
m_bag.takeTile(Tile::Joker());
|
2005-03-29 00:07:22 +02:00
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
else
|
|
|
|
m_bag.takeTile(iRound.getTile(i));
|
|
|
|
}
|
|
|
|
}
|
2005-03-29 00:07:22 +02:00
|
|
|
m_board.addRound(*m_dic, iRound);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Game::back(int n)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
Player *player;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
if (m_history.size())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
prevPlayer();
|
|
|
|
player = m_players[m_currPlayer];
|
2005-11-06 00:22:41 +01:00
|
|
|
const Round &lastround = m_history.back()->getRound();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
/* Remove the points of this round */
|
|
|
|
player->addPoints(- lastround.getPoints());
|
|
|
|
m_points -= lastround.getPoints();
|
|
|
|
/* Remove the word from the board, and put its letters back
|
|
|
|
* into the bag */
|
|
|
|
m_board.removeRound(*m_dic, lastround);
|
|
|
|
for (j = 0; j < lastround.getWordLen(); j++)
|
|
|
|
{
|
|
|
|
if (lastround.isPlayedFromRack(j))
|
|
|
|
{
|
|
|
|
if (lastround.isJoker(j))
|
|
|
|
m_bag.replaceTile(Tile::Joker());
|
|
|
|
else
|
|
|
|
m_bag.replaceTile(lastround.getTile(j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete &lastround;
|
2005-11-06 00:22:41 +01:00
|
|
|
m_history.pop_back();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2005-11-05 00:26:03 +01:00
|
|
|
else
|
|
|
|
{
|
2005-04-09 18:10:25 +02:00
|
|
|
return 1;
|
2005-11-05 00:26:03 +01:00
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************
|
|
|
|
*********************************************************/
|
|
|
|
|
|
|
|
void Game::realBag(Bag &ioBag) const
|
|
|
|
{
|
|
|
|
vector<Tile> tiles;
|
|
|
|
|
|
|
|
/* Copy the bag */
|
|
|
|
ioBag = m_bag;
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
/* The real content of the bag depends on the game mode */
|
2005-02-05 12:14:56 +01:00
|
|
|
if (getMode() == kFREEGAME)
|
|
|
|
{
|
|
|
|
/* In freegame mode, replace the letters from all the racks */
|
|
|
|
for (int i = 0; i < getNPlayers(); i++)
|
|
|
|
{
|
2005-04-02 22:46:42 +02:00
|
|
|
m_players[i]->getCurrentRack().getAllTiles(tiles);
|
2005-02-05 12:14:56 +01:00
|
|
|
for (unsigned int j = 0; j < tiles.size(); j++)
|
|
|
|
{
|
|
|
|
ioBag.takeTile(tiles[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In training or duplicate mode, replace the rack of the current
|
|
|
|
* player only */
|
2005-04-02 22:46:42 +02:00
|
|
|
m_players[m_currPlayer]->getCurrentRack().getAllTiles(tiles);
|
|
|
|
for (unsigned int j = 0; j < tiles.size(); j++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
ioBag.takeTile(tiles[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Game::rackInBag(const Rack &iRack, const Bag &iBag) const
|
|
|
|
{
|
|
|
|
const list<Tile>& allTiles = Tile::getAllTiles();
|
|
|
|
list<Tile>::const_iterator it;
|
|
|
|
for (it = allTiles.begin(); it != allTiles.end(); it++)
|
|
|
|
{
|
|
|
|
if (iRack.in(*it) > iBag.in(*it))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Game::helperSetRackRandom(int p, bool iCheck, set_rack_mode mode)
|
|
|
|
{
|
2005-03-27 23:45:04 +02:00
|
|
|
ASSERT(0 <= p && p < getNPlayers(), "Wrong player number");
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
int nold, min;
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// Make a copy of the player's rack
|
2005-02-09 23:33:56 +01:00
|
|
|
PlayedRack pld = m_players[p]->getCurrentRack();
|
2005-02-05 12:14:56 +01:00
|
|
|
nold = pld.nOld();
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// Create a copy of the bag in which we can do everything we want,
|
|
|
|
// and remove from it the tiles of the racks
|
2005-02-05 12:14:56 +01:00
|
|
|
Bag bag;
|
|
|
|
realBag(bag);
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// We may have removed too many letters from the bag (i.e. the 'new'
|
|
|
|
// letters of the player)
|
2005-02-05 12:14:56 +01:00
|
|
|
if (mode == RACK_NEW && nold != 0)
|
|
|
|
{
|
2005-03-29 00:07:22 +02:00
|
|
|
vector<Tile> tiles;
|
2005-02-05 12:14:56 +01:00
|
|
|
pld.getNewTiles(tiles);
|
|
|
|
for (unsigned int i = 0; i < tiles.size(); i++)
|
|
|
|
{
|
|
|
|
bag.replaceTile(tiles[i]);
|
|
|
|
}
|
|
|
|
pld.resetNew();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-03-29 00:07:22 +02:00
|
|
|
// RACK_NEW with an empty rack is equivalent to RACK_ALL
|
2005-02-05 12:14:56 +01:00
|
|
|
pld.reset();
|
2005-03-29 00:07:22 +02:00
|
|
|
// Do not forget to update nold, for the RACK_ALL case
|
2005-02-05 12:14:56 +01:00
|
|
|
nold = 0;
|
|
|
|
}
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// Nothing in the rack, nothing in the bag --> end of the game
|
2005-02-05 12:14:56 +01:00
|
|
|
if (bag.nTiles() == 0 && pld.nTiles() == 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// When iCheck is true, we must make sure that there are at least 2 vowels
|
|
|
|
// and 2 consonants in the rack up to the 15th turn, and at least one of
|
|
|
|
// them from the 16th turn.
|
|
|
|
// So before trying to fill the rack, we'd better make sure there is a way
|
|
|
|
// to complete the rack with these constraints...
|
2005-02-05 12:14:56 +01:00
|
|
|
min = 0;
|
|
|
|
if (iCheck)
|
|
|
|
{
|
|
|
|
int oldc, oldv;
|
|
|
|
|
|
|
|
if (bag.nVowels() == 0 || bag.nConsonants() == 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2005-03-29 00:07:22 +02:00
|
|
|
// 2 vowels and 2 consonants are needed up to the 15th turn
|
2005-02-05 12:14:56 +01:00
|
|
|
if (bag.nVowels() > 1 && bag.nConsonants() > 1
|
2005-11-06 00:22:41 +01:00
|
|
|
&& getNTurns() < 15)
|
2005-02-05 12:14:56 +01:00
|
|
|
min = 2;
|
|
|
|
else
|
|
|
|
min = 1;
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// Count the remaining consonants and vowels in the rack
|
|
|
|
vector<Tile> tiles;
|
2005-02-05 12:14:56 +01:00
|
|
|
pld.getOldTiles(tiles);
|
|
|
|
oldc = 0;
|
|
|
|
oldv = 0;
|
|
|
|
for (unsigned int i = 0; i < tiles.size(); i++)
|
|
|
|
{
|
|
|
|
if (tiles[i].isConsonant())
|
|
|
|
oldc++;
|
|
|
|
if (tiles[i].isVowel())
|
|
|
|
oldv++;
|
|
|
|
}
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// RACK_SIZE - nold is the number of letters to add
|
2005-02-26 23:57:34 +01:00
|
|
|
if (min > oldc + RACK_SIZE - nold ||
|
|
|
|
min > oldv + RACK_SIZE - nold)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-03-29 00:07:22 +02:00
|
|
|
// We cannot fill the rack with enough vowels or consonants!
|
2005-02-05 12:14:56 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
// Are we dealing with a normal game or a joker game?
|
|
|
|
if (m_variant == kJOKER)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-03-29 00:07:22 +02:00
|
|
|
// 1) Is there already a joker in the remaining letters of the rack?
|
|
|
|
bool jokerFound = false;
|
|
|
|
vector<Tile> tiles;
|
|
|
|
pld.getOldTiles(tiles);
|
|
|
|
for (unsigned int i = 0; i < tiles.size(); i++)
|
|
|
|
{
|
|
|
|
if (tiles[i].isJoker())
|
|
|
|
{
|
|
|
|
jokerFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) If there was no joker, we add one if possible
|
|
|
|
if (!jokerFound && bag.in(Tile::Joker()))
|
|
|
|
{
|
|
|
|
bag.takeTile(Tile::Joker());
|
|
|
|
pld.addNew(Tile::Joker());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) Complete the rack normally... but without any joker!
|
|
|
|
Tile l;
|
|
|
|
while (bag.nTiles() != 0 && pld.nTiles() != RACK_SIZE)
|
|
|
|
{
|
|
|
|
l = bag.selectRandom();
|
|
|
|
if (!l.isJoker())
|
|
|
|
{
|
|
|
|
bag.takeTile(l);
|
|
|
|
pld.addNew(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Normal game
|
|
|
|
{
|
|
|
|
// Get new tiles from the bag
|
|
|
|
Tile l;
|
|
|
|
while (bag.nTiles() != 0 && pld.nTiles() != RACK_SIZE)
|
|
|
|
{
|
|
|
|
l = bag.selectRandom();
|
|
|
|
bag.takeTile(l);
|
|
|
|
pld.addNew(l);
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iCheck && !pld.checkRack(min))
|
|
|
|
return 2;
|
|
|
|
|
2005-02-09 23:33:56 +01:00
|
|
|
m_players[p]->setCurrentRack(pld);
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Game::helperSetRackManual(int p, bool iCheck, const string &iLetters)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int min;
|
|
|
|
|
2005-02-09 23:33:56 +01:00
|
|
|
PlayedRack pld = m_players[p]->getCurrentRack();
|
2005-02-05 12:14:56 +01:00
|
|
|
pld.reset();
|
|
|
|
|
|
|
|
if (iLetters.size() == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < iLetters.size() && iLetters[i] != '+'; i++)
|
|
|
|
{
|
|
|
|
Tile tile(iLetters[i]);
|
2005-02-17 21:01:59 +01:00
|
|
|
if (tile.isEmpty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pld.addOld(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < iLetters.size() && iLetters[i] == '+')
|
|
|
|
{
|
|
|
|
for (i++; i < iLetters.size(); i++)
|
|
|
|
{
|
|
|
|
Tile tile(iLetters[i]);
|
2005-02-17 21:01:59 +01:00
|
|
|
if (tile.isEmpty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pld.addNew(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rack rack;
|
|
|
|
pld.getRack(rack);
|
2005-03-27 23:45:04 +02:00
|
|
|
if (!rackInBag(rack, m_bag))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
pld.reset();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iCheck)
|
|
|
|
{
|
|
|
|
if (m_bag.nVowels() > 1 && m_bag.nConsonants() > 1
|
2005-11-06 00:22:41 +01:00
|
|
|
&& getNTurns() < 15)
|
2005-02-05 12:14:56 +01:00
|
|
|
min = 2;
|
|
|
|
else
|
|
|
|
min = 1;
|
2005-02-09 23:33:56 +01:00
|
|
|
if (!pld.checkRack(min))
|
|
|
|
return 2;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2005-02-09 23:33:56 +01:00
|
|
|
m_players[p]->setCurrentRack(pld);
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************
|
|
|
|
*********************************************************/
|
|
|
|
|
|
|
|
string Game::getPlayedRack(int num) const
|
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number");
|
|
|
|
return m_history[num]->getPlayedRack().toString();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Game::getPlayedWord(int num) const
|
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number");
|
2005-02-05 12:14:56 +01:00
|
|
|
char c;
|
|
|
|
string s;
|
2005-11-06 00:22:41 +01:00
|
|
|
const Round &r = m_history[num]->getRound();
|
2005-02-05 12:14:56 +01:00
|
|
|
for (int i = 0; i < r.getWordLen(); i++)
|
|
|
|
{
|
|
|
|
c = r.getTile(i).toChar();
|
|
|
|
if (r.isJoker(i))
|
|
|
|
c = tolower(c);
|
|
|
|
s += c;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Game::getPlayedCoords(int num) const
|
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number");
|
|
|
|
return m_history[num]->getRound().getCoord().toString();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Game::getPlayedPoints(int num) const
|
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number");
|
|
|
|
return m_history[num]->getRound().getPoints();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Game::getPlayedBonus(int num) const
|
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number");
|
|
|
|
return m_history[num]->getRound().getBonus();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Game::getPlayedPlayer(int num) const
|
|
|
|
{
|
2005-11-06 00:22:41 +01:00
|
|
|
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number");
|
|
|
|
return m_history[num]->getPlayer();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************
|
|
|
|
*********************************************************/
|
|
|
|
|
2005-11-05 16:48:59 +01:00
|
|
|
string Game::getPlayerRack(int num, bool iShowExtraSigns) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-11-05 16:48:59 +01:00
|
|
|
return getPlayer(num).getCurrentRack().toString(iShowExtraSigns);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Game::getNHumanPlayers() const
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < getNPlayers(); i++)
|
|
|
|
count += (m_players[i]->isHuman() ? 1 : 0);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::addHumanPlayer()
|
|
|
|
{
|
2005-02-17 21:01:59 +01:00
|
|
|
m_players.push_back(new HumanPlayer());
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::addAIPlayer()
|
|
|
|
{
|
2005-02-17 21:01:59 +01:00
|
|
|
m_players.push_back(new AIPercent(0));
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::prevPlayer()
|
|
|
|
{
|
2005-03-27 23:45:04 +02:00
|
|
|
ASSERT(getNPlayers() != 0, "Expected at least one player");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
if (m_currPlayer == 0)
|
|
|
|
m_currPlayer = getNPlayers() - 1;
|
|
|
|
else
|
|
|
|
m_currPlayer--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Game::nextPlayer()
|
|
|
|
{
|
2005-03-27 23:45:04 +02:00
|
|
|
ASSERT(getNPlayers() != 0, "Expected at least one player");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
if (m_currPlayer == getNPlayers() - 1)
|
|
|
|
m_currPlayer = 0;
|
|
|
|
else
|
|
|
|
m_currPlayer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function checks whether it is legal to play the given word at the
|
|
|
|
* given coordinates. If so, the function fills a Round object, also given as
|
|
|
|
* a parameter.
|
|
|
|
* Possible return values:
|
|
|
|
* 0: correct word, the Round can be used by the caller
|
|
|
|
* 1: no dictionary set
|
|
|
|
* 2: invalid coordinates (unreadable or out of the board)
|
|
|
|
* 3: word not present in the dictionary
|
|
|
|
* 4: not enough letters in the rack to play the word
|
|
|
|
* 5: word is part of a longer one
|
|
|
|
* 6: word overwriting an existing letter
|
|
|
|
* 7: invalid crosscheck, or word going out of the board
|
|
|
|
* 8: word already present on the board (no new letter from the rack)
|
|
|
|
* 9: isolated word (not connected to the rest)
|
|
|
|
* 10: first word not horizontal
|
|
|
|
* 11: first word not covering the H8 square
|
|
|
|
*/
|
|
|
|
int Game::checkPlayedWord(const string &iCoord,
|
|
|
|
const string &iWord, Round &oRound)
|
|
|
|
{
|
2005-03-27 23:45:04 +02:00
|
|
|
ASSERT(getNPlayers() != 0, "Expected at least one player");
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
int res;
|
|
|
|
vector<Tile> tiles;
|
|
|
|
Tile t;
|
|
|
|
|
|
|
|
/* Init the round with the given coordinates */
|
|
|
|
oRound.init();
|
2005-11-05 14:56:59 +01:00
|
|
|
oRound.accessCoord().setFromString(iCoord);
|
|
|
|
if (!oRound.getCoord().isValid())
|
2005-02-05 12:14:56 +01:00
|
|
|
return 2;
|
|
|
|
|
|
|
|
/* Check the existence of the word */
|
|
|
|
if (Dic_search_word(*m_dic, iWord.c_str()) == 0)
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
/* Set the word */
|
|
|
|
// TODO: make this a Round_ function (Round_setwordfromchar for example)
|
2005-03-27 23:45:04 +02:00
|
|
|
// or a Tiles_ function (to transform a char* into a vector<Tile>)
|
2005-02-05 12:14:56 +01:00
|
|
|
// Adding a getter on the word could help too...
|
|
|
|
for (unsigned int i = 0; i < iWord.size(); i++)
|
|
|
|
{
|
|
|
|
tiles.push_back(Tile(iWord[i]));
|
|
|
|
}
|
|
|
|
oRound.setWord(tiles);
|
|
|
|
for (unsigned int i = 0; i < iWord.size(); i++)
|
|
|
|
{
|
|
|
|
if (islower(iWord[i]))
|
|
|
|
oRound.setJoker(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the word position, compute its points,
|
|
|
|
* and specify the origin of each letter (board or rack) */
|
2005-11-06 00:22:41 +01:00
|
|
|
res = m_board.checkRound(oRound, getNTurns() == 0);
|
2005-02-05 12:14:56 +01:00
|
|
|
if (res != 0)
|
|
|
|
return res + 4;
|
|
|
|
|
|
|
|
/* Check that the word can be formed with the tiles in the rack:
|
|
|
|
* we first create a copy of the rack, then we remove the tiles
|
|
|
|
* one by one */
|
|
|
|
Rack rack;
|
|
|
|
Player *player = m_players[m_currPlayer];
|
2005-02-09 23:33:56 +01:00
|
|
|
player->getCurrentRack().getRack(rack);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < oRound.getWordLen(); i++)
|
|
|
|
{
|
|
|
|
if (oRound.isPlayedFromRack(i))
|
|
|
|
{
|
|
|
|
if (oRound.isJoker(i))
|
|
|
|
t = Tile::Joker();
|
|
|
|
else
|
|
|
|
t = oRound.getTile(i);
|
|
|
|
|
2005-03-27 23:45:04 +02:00
|
|
|
if (!rack.in(t))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
rack.remove(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|