From 4c56636aef0cf5806c326a89507b48bf5bf6eea5 Mon Sep 17 00:00:00 2001 From: Antoine Fraboulet Date: Mon, 26 Dec 2005 22:58:58 +0000 Subject: [PATCH] - use History class - back is still buggy --- game/game.cpp | 88 +++++++++------------------------------------------ game/game.h | 18 ++++------- 2 files changed, 21 insertions(+), 85 deletions(-) diff --git a/game/game.cpp b/game/game.cpp index 292d5f0..444954e 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -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; diff --git a/game/game.h b/game/game.h index 1a546e3..16c2fb0 100644 --- a/game/game.h +++ b/game/game.h @@ -26,6 +26,7 @@ #include #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 m_history; + History m_history; int m_points;