Minor factorization, and avoided another copy of PlayedRack

This commit is contained in:
Olivier Teulière 2005-11-06 01:05:06 +00:00
parent d64e22dc6b
commit 4cef120cce
3 changed files with 31 additions and 29 deletions

View file

@ -220,7 +220,7 @@ int Duplicate::endTurnForReal()
return 1; return 1;
} }
PlayedRack pld = m_players[imax]->getCurrentRack(); const PlayedRack& pld = m_players[imax]->getCurrentRack();
/* All the players have the same rack */ /* All the players have the same rack */
for (i = 0; i < getNPlayers(); i++) for (i = 0; i < getNPlayers(); i++)
{ {

View file

@ -60,10 +60,17 @@ Game::~Game()
} }
const Player& Game::getPlayer(int num) const const Player& Game::getPlayer(int iNum) const
{ {
ASSERT(0 <= num && num < (int)m_players.size(), "Wrong player number"); ASSERT(0 <= iNum && iNum < (int)m_players.size(), "Wrong player number");
return *(m_players[num]); return *(m_players[iNum]);
}
const Turn& Game::getTurn(int iNum) const
{
ASSERT(0 <= iNum && iNum < (int)m_history.size(), "Wrong turn number");
return *(m_history[iNum]);
} }
@ -272,7 +279,7 @@ Game * Game::load(FILE *fin, const Dictionary &iDic)
// the game was saved while a human was to play. // the game was saved while a human was to play.
for (int i = 0; i < pGame->getNPlayers(); i++) for (int i = 0; i < pGame->getNPlayers(); i++)
{ {
if (pGame->m_players[i]->isHuman()) if (pGame->getPlayer(i).isHuman())
{ {
pGame->m_currPlayer = i; pGame->m_currPlayer = i;
break; break;
@ -292,7 +299,7 @@ void Game::save(ostream &out) const
for (int i = 0; i < getNPlayers(); i++) for (int i = 0; i < getNPlayers(); i++)
{ {
out << "Player " << i << ": "; out << "Player " << i << ": ";
if (m_players[i]->isHuman()) if (getPlayer(i).isHuman())
out << "Human" << endl; out << "Human" << endl;
else else
out << "Computer" << endl; out << "Computer" << endl;
@ -323,7 +330,7 @@ void Game::save(ostream &out) const
out << endl; out << endl;
for (int i = 0; i < getNPlayers(); i++) for (int i = 0; i < getNPlayers(); i++)
{ {
string rack = m_players[i]->getCurrentRack().toString(); string rack = getPlayer(i).getCurrentRack().toString();
out << "Rack " << i << ": " << rack << endl; out << "Rack " << i << ": " << rack << endl;
} }
} }
@ -341,7 +348,7 @@ 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.push_back(new Turn(m_history.size(), m_currPlayer,
m_players[m_currPlayer]->getLastRack(), getPlayer(m_currPlayer).getLastRack(),
iRound)); iRound));
m_points += iRound.getPoints(); m_points += iRound.getPoints();
@ -373,12 +380,12 @@ int Game::helperPlayRound(const Round &iRound)
// There is a big design problem here, but i am unsure what is // There is a big design problem here, but i am unsure what is
// the best way to fix it. // the best way to fix it.
vector<Tile> tiles; vector<Tile> tiles;
m_players[m_currPlayer]->getCurrentRack().getAllTiles(tiles); getPlayer(m_currPlayer).getCurrentRack().getAllTiles(tiles);
for (unsigned int j = 0; j < tiles.size(); j++) for (unsigned int j = 0; j < tiles.size(); j++)
{ {
bag.replaceTile(tiles[j]); bag.replaceTile(tiles[j]);
} }
m_players[m_currPlayer]->getLastRack().getAllTiles(tiles); getPlayer(m_currPlayer).getLastRack().getAllTiles(tiles);
for (unsigned int j = 0; j < tiles.size(); j++) for (unsigned int j = 0; j < tiles.size(); j++)
{ {
bag.takeTile(tiles[j]); bag.takeTile(tiles[j]);
@ -473,7 +480,7 @@ void Game::realBag(Bag &ioBag) const
/* In freegame mode, replace the letters from all the racks */ /* In freegame mode, replace the letters from all the racks */
for (int i = 0; i < getNPlayers(); i++) for (int i = 0; i < getNPlayers(); i++)
{ {
m_players[i]->getCurrentRack().getAllTiles(tiles); getPlayer(i).getCurrentRack().getAllTiles(tiles);
for (unsigned int j = 0; j < tiles.size(); j++) for (unsigned int j = 0; j < tiles.size(); j++)
{ {
ioBag.takeTile(tiles[j]); ioBag.takeTile(tiles[j]);
@ -484,7 +491,7 @@ void Game::realBag(Bag &ioBag) const
{ {
/* In training or duplicate mode, replace the rack of the current /* In training or duplicate mode, replace the rack of the current
* player only */ * player only */
m_players[m_currPlayer]->getCurrentRack().getAllTiles(tiles); getPlayer(m_currPlayer).getCurrentRack().getAllTiles(tiles);
for (unsigned int j = 0; j < tiles.size(); j++) for (unsigned int j = 0; j < tiles.size(); j++)
{ {
ioBag.takeTile(tiles[j]); ioBag.takeTile(tiles[j]);
@ -513,7 +520,7 @@ int Game::helperSetRackRandom(int p, bool iCheck, set_rack_mode mode)
int nold, min; int nold, min;
// Make a copy of the player's rack // Make a copy of the player's rack
PlayedRack pld = m_players[p]->getCurrentRack(); PlayedRack pld = getPlayer(p).getCurrentRack();
nold = pld.nOld(); nold = pld.nOld();
// Create a copy of the bag in which we can do everything we want, // Create a copy of the bag in which we can do everything we want,
@ -651,7 +658,7 @@ int Game::helperSetRackManual(int p, bool iCheck, const string &iLetters)
unsigned int i; unsigned int i;
int min; int min;
PlayedRack pld = m_players[p]->getCurrentRack(); PlayedRack pld = getPlayer(p).getCurrentRack();
pld.reset(); pld.reset();
if (iLetters.size() == 0) if (iLetters.size() == 0)
@ -711,17 +718,15 @@ int Game::helperSetRackManual(int p, bool iCheck, const string &iLetters)
string Game::getPlayedRack(int num) const string Game::getPlayedRack(int num) const
{ {
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number"); return getTurn(num).getPlayedRack().toString();
return m_history[num]->getPlayedRack().toString();
} }
string Game::getPlayedWord(int num) const string Game::getPlayedWord(int num) const
{ {
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number");
char c; char c;
string s; string s;
const Round &r = m_history[num]->getRound(); const Round &r = getTurn(num).getRound();
for (int i = 0; i < r.getWordLen(); i++) for (int i = 0; i < r.getWordLen(); i++)
{ {
c = r.getTile(i).toChar(); c = r.getTile(i).toChar();
@ -735,29 +740,25 @@ string Game::getPlayedWord(int num) const
string Game::getPlayedCoords(int num) const string Game::getPlayedCoords(int num) const
{ {
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number"); return getTurn(num).getRound().getCoord().toString();
return m_history[num]->getRound().getCoord().toString();
} }
int Game::getPlayedPoints(int num) const int Game::getPlayedPoints(int num) const
{ {
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number"); return getTurn(num).getRound().getPoints();
return m_history[num]->getRound().getPoints();
} }
int Game::getPlayedBonus(int num) const int Game::getPlayedBonus(int num) const
{ {
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number"); return getTurn(num).getRound().getBonus();
return m_history[num]->getRound().getBonus();
} }
int Game::getPlayedPlayer(int num) const int Game::getPlayedPlayer(int num) const
{ {
ASSERT(0 <= num && num < getNTurns(), "Wrong turn number"); return getTurn(num).getPlayer();
return m_history[num]->getPlayer();
} }
/********************************************************* /*********************************************************
@ -773,7 +774,7 @@ int Game::getNHumanPlayers() const
{ {
int count = 0; int count = 0;
for (int i = 0; i < getNPlayers(); i++) for (int i = 0; i < getNPlayers(); i++)
count += (m_players[i]->isHuman() ? 1 : 0); count += (getPlayer(i).isHuman() ? 1 : 0);
return count; return count;
} }

View file

@ -87,7 +87,8 @@ public:
const Board& getBoard() const { return m_board; } const Board& getBoard() const { return m_board; }
const Bag& getBag() const { return m_bag; } const Bag& getBag() const { return m_bag; }
const Player& getPlayer(int iIndex) const; const Player& getPlayer(int iNum) const;
const Turn& getTurn(int iNum) const;
/** /**
* Saved games handling. * Saved games handling.
@ -122,7 +123,7 @@ 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;
typedef enum {RACK_ALL, RACK_NEW} set_rack_mode; enum set_rack_mode {RACK_ALL, RACK_NEW};
/** /**
* Methods to access already played words. * Methods to access already played words.