- 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()
{
for (unsigned int i = 0; i < m_history.size(); i++)
{
delete m_history[i];
}
for (int i = 0; i < getNPlayers(); 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)
{
char buff[4096];
@ -312,15 +301,16 @@ void Game::save(ostream &out) const
out << decal << "===|==========|=================|=====|=====|===|======" << endl;
// Print the game itself
for (int i = 0; i < getNTurns(); i++)
for (int i = 0; i < m_history.getSize(); i++)
{
string word = getPlayedWord(i);
string coord = getPlayedCoords(i);
const Turn& t = m_history.getTurn(i);
string word = t.getRound().getWord();
string coord = t.getRound().getCoord().toString();
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(),
coord.c_str(), getPlayedPoints(i),
getPlayedPlayer(i), getPlayedBonus(i) ? '*' : ' ');
coord.c_str(), t.getRound().getPoints(),
t.getPlayer(), t.getRound().getBonus() ? '*' : ' ');
out << decal << line << endl;
}
@ -347,9 +337,8 @@ int Game::helperPlayRound(const Round &iRound)
*/
// History of the game
m_history.push_back(new Turn(m_history.size(), m_currPlayer,
getPlayer(m_currPlayer).getLastRack(),
iRound));
m_history.setCurrentRack(getCurrentPlayer().getLastRack());
m_history.playRound(m_currPlayer, m_history.getSize(), iRound);
m_points += iRound.getPoints();
@ -430,11 +419,11 @@ int Game::back(int n)
for (i = 0; i < n; i++)
{
if (m_history.size())
if (m_history.getSize())
{
prevPlayer();
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 */
player->addPoints(- lastround.getPoints());
@ -453,7 +442,7 @@ int Game::back(int n)
}
}
delete &lastround;
m_history.pop_back();
m_history.removeLastTurn();
}
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
if (bag.nVowels() > 1 && bag.nConsonants() > 1
&& getNTurns() < 15)
&& m_history.getSize() < 15)
min = 2;
else
min = 1;
@ -700,7 +689,7 @@ int Game::helperSetRackManual(int p, bool iCheck, const string &iLetters)
if (iCheck)
{
if (m_bag.nVowels() > 1 && m_bag.nConsonants() > 1
&& getNTurns() < 15)
&& m_history.getSize() < 15)
min = 2;
else
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
{
@ -868,7 +810,7 @@ int Game::checkPlayedWord(const string &iCoord,
/* Check the word position, compute its points,
* 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)
return res + 4;

View file

@ -26,6 +26,7 @@
#include <iostream>
#include "bag.h"
#include "board.h"
#include "history.h"
class Player;
class PlayedRack;
@ -124,19 +125,15 @@ public:
* 3 : the rack cannot be completed (Game_*_setrackrandom only)
*************************/
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.
* The int parameter should be 0 <= int < getNTurns()
*/
int getNTurns() const { return m_history.size(); }
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;
const History& getHistory() { return m_history; }
/**
* Methods to access players.
@ -147,15 +144,12 @@ public:
virtual void addHumanPlayer();
// TODO: Ability to specify which kind of AI player is wanted
virtual void addAIPlayer();
string getPlayerRack(int, bool = false) const;
int currPlayer() const { return m_currPlayer; }
/**
* Game handling
*/
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 endTurn() = 0;
@ -184,7 +178,7 @@ protected:
* History of the game.
* The vector is indexed by the number of turns in the game
*/
vector<Turn*> m_history;
History m_history;
int m_points;