- use History class

- back is still buggy
This commit is contained in:
Antoine Fraboulet 2005-12-26 22:58:58 +00:00
parent 4881a63f75
commit 4c56636aef
2 changed files with 21 additions and 85 deletions

View file

@ -49,10 +49,6 @@ Game::Game(const Dictionary &iDic):
Game::~Game() Game::~Game()
{ {
for (unsigned int i = 0; i < m_history.size(); i++)
{
delete m_history[i];
}
for (int i = 0; i < getNPlayers(); i++) for (int i = 0; i < getNPlayers(); i++)
{ {
delete m_players[i]; delete m_players[i];
@ -67,13 +63,6 @@ const Player& Game::getPlayer(int iNum) const
} }
const Turn& Game::getTurn(int iNum) const
{
ASSERT(0 <= iNum && iNum < (int)m_history.size(), "Wrong turn number");
return *(m_history[iNum]);
}
Game * Game::load(FILE *fin, const Dictionary &iDic) Game * Game::load(FILE *fin, const Dictionary &iDic)
{ {
char buff[4096]; char buff[4096];
@ -312,15 +301,16 @@ void Game::save(ostream &out) const
out << decal << "===|==========|=================|=====|=====|===|======" << endl; out << decal << "===|==========|=================|=====|=====|===|======" << endl;
// Print the game itself // Print the game itself
for (int i = 0; i < getNTurns(); i++) for (int i = 0; i < m_history.getSize(); i++)
{ {
string word = getPlayedWord(i); const Turn& t = m_history.getTurn(i);
string coord = getPlayedCoords(i); string word = t.getRound().getWord();
string coord = t.getRound().getCoord().toString();
sprintf(line, "%2d | %8s | %s%s | %3s | %3d | %1d | %c", sprintf(line, "%2d | %8s | %s%s | %3s | %3d | %1d | %c",
i + 1, getPlayedRack(i).c_str(), word.c_str(), i + 1, t.getPlayedRack().toString().c_str(), word.c_str(),
string(15 - word.size(), ' ').c_str(), string(15 - word.size(), ' ').c_str(),
coord.c_str(), getPlayedPoints(i), coord.c_str(), t.getRound().getPoints(),
getPlayedPlayer(i), getPlayedBonus(i) ? '*' : ' '); t.getPlayer(), t.getRound().getBonus() ? '*' : ' ');
out << decal << line << endl; out << decal << line << endl;
} }
@ -347,9 +337,8 @@ int Game::helperPlayRound(const Round &iRound)
*/ */
// History of the game // History of the game
m_history.push_back(new Turn(m_history.size(), m_currPlayer, m_history.setCurrentRack(getCurrentPlayer().getLastRack());
getPlayer(m_currPlayer).getLastRack(), m_history.playRound(m_currPlayer, m_history.getSize(), iRound);
iRound));
m_points += iRound.getPoints(); m_points += iRound.getPoints();
@ -430,11 +419,11 @@ int Game::back(int n)
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
if (m_history.size()) if (m_history.getSize())
{ {
prevPlayer(); prevPlayer();
player = m_players[m_currPlayer]; player = m_players[m_currPlayer];
const Round &lastround = m_history.back()->getRound(); const Round &lastround = m_history.getPreviousTurn().getRound();
/* Remove the points of this round */ /* Remove the points of this round */
player->addPoints(- lastround.getPoints()); player->addPoints(- lastround.getPoints());
@ -453,7 +442,7 @@ int Game::back(int n)
} }
} }
delete &lastround; delete &lastround;
m_history.pop_back(); m_history.removeLastTurn();
} }
else else
{ {
@ -570,7 +559,7 @@ int Game::helperSetRackRandom(int p, bool iCheck, set_rack_mode mode)
} }
// 2 vowels and 2 consonants are needed up to the 15th turn // 2 vowels and 2 consonants are needed up to the 15th turn
if (bag.nVowels() > 1 && bag.nConsonants() > 1 if (bag.nVowels() > 1 && bag.nConsonants() > 1
&& getNTurns() < 15) && m_history.getSize() < 15)
min = 2; min = 2;
else else
min = 1; min = 1;
@ -700,7 +689,7 @@ int Game::helperSetRackManual(int p, bool iCheck, const string &iLetters)
if (iCheck) if (iCheck)
{ {
if (m_bag.nVowels() > 1 && m_bag.nConsonants() > 1 if (m_bag.nVowels() > 1 && m_bag.nConsonants() > 1
&& getNTurns() < 15) && m_history.getSize() < 15)
min = 2; min = 2;
else else
min = 1; min = 1;
@ -716,53 +705,6 @@ int Game::helperSetRackManual(int p, bool iCheck, const string &iLetters)
/********************************************************* /*********************************************************
*********************************************************/ *********************************************************/
string Game::getPlayedRack(int num) const
{
return getTurn(num).getPlayedRack().toString();
}
string Game::getPlayedWord(int num) const
{
char c;
string s;
const Round &r = getTurn(num).getRound();
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
{
return getTurn(num).getRound().getCoord().toString();
}
int Game::getPlayedPoints(int num) const
{
return getTurn(num).getRound().getPoints();
}
int Game::getPlayedBonus(int num) const
{
return getTurn(num).getRound().getBonus();
}
int Game::getPlayedPlayer(int num) const
{
return getTurn(num).getPlayer();
}
/*********************************************************
*********************************************************/
string Game::getPlayerRack(int num, bool iShowExtraSigns) const string Game::getPlayerRack(int num, bool iShowExtraSigns) const
{ {
@ -868,7 +810,7 @@ int Game::checkPlayedWord(const string &iCoord,
/* Check the word position, compute its points, /* Check the word position, compute its points,
* and specify the origin of each letter (board or rack) */ * and specify the origin of each letter (board or rack) */
res = m_board.checkRound(oRound, getNTurns() == 0); res = m_board.checkRound(oRound, m_history.getSize() == 0);
if (res != 0) if (res != 0)
return res + 4; return res + 4;

View file

@ -26,6 +26,7 @@
#include <iostream> #include <iostream>
#include "bag.h" #include "bag.h"
#include "board.h" #include "board.h"
#include "history.h"
class Player; class Player;
class PlayedRack; class PlayedRack;
@ -124,19 +125,15 @@ public:
* 3 : the rack cannot be completed (Game_*_setrackrandom only) * 3 : the rack cannot be completed (Game_*_setrackrandom only)
*************************/ *************************/
static const int RACK_SIZE; static const int RACK_SIZE;
enum set_rack_mode {RACK_ALL, RACK_NEW}; enum set_rack_mode {RACK_ALL, RACK_NEW, RACK_MANUAL};
int setRack(int player, set_rack_mode mode, bool check, const string& str);
string getPlayerRack(int, bool = false) const;
/** /**
* Methods to access already played words. * Methods to access already played words.
* The int parameter should be 0 <= int < getNTurns() * The int parameter should be 0 <= int < getNTurns()
*/ */
int getNTurns() const { return m_history.size(); } const History& getHistory() { return m_history; }
string getPlayedRack(int) const;
string getPlayedWord(int) const;
string getPlayedCoords(int num) const;
int getPlayedPoints(int) const;
int getPlayedBonus(int) const;
int getPlayedPlayer(int) const;
/** /**
* Methods to access players. * Methods to access players.
@ -147,15 +144,12 @@ public:
virtual void addHumanPlayer(); virtual void addHumanPlayer();
// TODO: Ability to specify which kind of AI player is wanted // TODO: Ability to specify which kind of AI player is wanted
virtual void addAIPlayer(); virtual void addAIPlayer();
string getPlayerRack(int, bool = false) const;
int currPlayer() const { return m_currPlayer; } int currPlayer() const { return m_currPlayer; }
/** /**
* Game handling * Game handling
*/ */
virtual int start() = 0; virtual int start() = 0;
virtual int setRackRandom(int, bool, set_rack_mode) = 0;
virtual int play(const string &iCoord, const string &iWord) = 0; virtual int play(const string &iCoord, const string &iWord) = 0;
virtual int endTurn() = 0; virtual int endTurn() = 0;
@ -184,7 +178,7 @@ protected:
* History of the game. * History of the game.
* The vector is indexed by the number of turns in the game * The vector is indexed by the number of turns in the game
*/ */
vector<Turn*> m_history; History m_history;
int m_points; int m_points;