Players are not created by the Game object anymore but by the user of the class.

This allows more flexibility, in particular to choose the type of player or its name.
This commit is contained in:
Olivier Teulière 2008-01-28 19:17:33 +00:00
parent 009ac85148
commit 52b7363888
13 changed files with 54 additions and 54 deletions

View file

@ -28,8 +28,8 @@
#include "ai_percent.h"
AIPercent::AIPercent(int iId, float iPercent)
: AIPlayer(iId), m_percent(iPercent)
AIPercent::AIPercent(float iPercent)
: m_percent(iPercent)
{
// Ensure the decimal value of the percentage is between 0 and 1
if (m_percent < 0)

View file

@ -39,7 +39,7 @@ class AIPercent: public AIPlayer
{
public:
/// Constructor, taking the percentage (0.0 <= iPercent <= 1.0)
AIPercent(int iId, float iPercent);
AIPercent(float iPercent);
virtual ~AIPercent() {}
/**

View file

@ -77,7 +77,7 @@ public:
protected:
/// This class is a pure interface, forbid any direct instanciation
AIPlayer(unsigned int iId): Player(iId) {}
AIPlayer() {}
};
#endif

View file

@ -26,7 +26,6 @@
#include "pldrack.h"
#include "results.h"
#include "player.h"
#include "ai_percent.h"
#include "game.h"
#include "game_factory.h"
#include "turn.h"
@ -521,17 +520,13 @@ unsigned int Game::getNHumanPlayers() const
}
void Game::addHumanPlayer()
void Game::addPlayer(Player *iPlayer)
{
ASSERT(iPlayer != NULL, "Invalid player pointer in addPlayer()");
// The ID of the player is its position in the m_players vector
m_players.push_back(new HumanPlayer(getNPlayers()));
}
void Game::addAIPlayer()
{
// TODO: allow other percentages, and even other types of AI
m_players.push_back(new AIPercent(getNPlayers(), 1));
iPlayer->setId(getNPlayers());
m_players.push_back(iPlayer);
}

View file

@ -110,11 +110,14 @@ public:
const Player& getCurrentPlayer() const { return getPlayer(currPlayer()); };
unsigned int getNPlayers() const { return m_players.size(); }
unsigned int getNHumanPlayers() const;
virtual void addHumanPlayer();
// TODO: Ability to specify which kind of AI player is wanted
virtual void addAIPlayer();
unsigned int currPlayer() const { return m_currPlayer; }
/**
* Add a player to the game.
* The Game object takes ownership of the given player
*/
virtual void addPlayer(Player *iPlayer);
/***************
* Game handling
***************/

View file

@ -39,6 +39,7 @@
#include "freegame.h"
#include "duplicate.h"
#include "player.h"
#include "ai_percent.h"
#include "dic.h"
#include "encoding.h"
@ -207,11 +208,13 @@ Game *GameFactory::createFromCmdLine(int argc, char **argv)
for (unsigned int i = 0; i < m_players.size(); ++i)
{
// Human?
Player *player;
if (m_players[i].first)
game->addHumanPlayer();
player = new HumanPlayer;
else
game->addAIPlayer();
const_cast<Player*>(&game->getPlayer(i))->setName(m_players[i].second);
player = new AIPercent(1);
player->setName(m_players[i].second);
game->addPlayer(player);
}
// 6) Set the variant

View file

@ -31,6 +31,7 @@
#include "round.h"
#include "turn.h"
#include "player.h"
#include "ai_percent.h"
#include "game.h"
#include "game_factory.h"
#include "training.h"
@ -261,7 +262,7 @@ Game* Game::gameLoadFormat_15(FILE *fin, const Dictionary& iDic)
if (string(type) == "Human")
{
debug(" add Human player\n");
pGame->addHumanPlayer();
pGame->addPlayer(new HumanPlayer);
}
else if (string(type) == "Computer")
{
@ -273,7 +274,7 @@ Game* Game::gameLoadFormat_15(FILE *fin, const Dictionary& iDic)
else
{
debug(" add Computer player\n");
pGame->addAIPlayer();
pGame->addPlayer(new AIPercent(1));
}
}
else

View file

@ -33,8 +33,8 @@
#include "debug.h"
Player::Player(unsigned int iId)
: m_id(iId), m_score(0)
Player::Player()
: m_id(0), m_score(0)
{
}

View file

@ -39,7 +39,7 @@ class Turn;
class Player
{
public:
explicit Player(unsigned int iId);
explicit Player();
virtual ~Player() {}
// Pseudo RTTI
@ -50,6 +50,9 @@ public:
/// Set the name of the player
void setName(const wstring &iName) { m_name = iName; }
/// Set the ID
void setId(unsigned int iId) { m_id = iId; }
/**************************
* General getters
**************************/
@ -106,7 +109,7 @@ private:
class HumanPlayer: public Player
{
public:
HumanPlayer(int iId): Player(iId) {}
HumanPlayer(): Player() {}
virtual ~HumanPlayer() {}
// Pseudo RTTI

View file

@ -45,7 +45,7 @@ Training::Training(const Dictionary &iDic)
: Game(iDic)
{
// Training mode implicitly uses 1 human player
Game::addHumanPlayer();
Game::addPlayer(new HumanPlayer);
m_players[0]->setName(convertToWc(_("Training")));
}
@ -175,17 +175,11 @@ int Training::playResult(unsigned int n)
}
void Training::addHumanPlayer()
void Training::addPlayer(Player *iPlayer)
{
// We are not supposed to be here...
ASSERT(false, "Trying to add a human player in Training mode");
}
void Training::addAIPlayer()
{
// We are not supposed to be here...
ASSERT(false, "Trying to add a AI player in Training mode");
// Override the default behaviour to do nothing
// except releasing memory
delete iPlayer;
}

View file

@ -64,11 +64,10 @@ public:
int setRack(set_rack_mode iMode, bool iCheck, const wstring &iLetters);
/*************************
* Override the default behaviour of these methods, because in training
* Override the default behaviour of addPlayer(), because in training
* mode we only want a human player
*************************/
virtual void addHumanPlayer();
virtual void addAIPlayer();
virtual void addPlayer(Player *iPlayer);
/*************************
* Functions to access the current search results

View file

@ -30,6 +30,7 @@
#include "freegame.h"
#include "duplicate.h"
#include "player.h"
#include "ai_percent.h"
const QString NewGame::kHUMAN = _q("Human");
@ -111,14 +112,13 @@ Game * NewGame::createGame(const Dictionary &iDic) const
allNames.insert(name);
QString type = m_model->data(m_model->index(num, 1)).toString();
Player *player;
if (type == kHUMAN)
game->addHumanPlayer();
player = new HumanPlayer;
else
game->addAIPlayer();
// FIXME: ugly const_cast, because of the awkward Game API
// (we should have addPlayer(Player &) instead)
const Player *player = &game->getPlayer(game->getNPlayers() - 1);
const_cast<Player *>(player)->setName(qtw(name));
player = new AIPercent(1);
player->setName(qtw(name));
game->addPlayer(player);
}
}
else

View file

@ -41,6 +41,8 @@
#include "training.h"
#include "duplicate.h"
#include "freegame.h"
#include "player.h"
#include "ai_percent.h"
#include "encoding.h"
@ -959,7 +961,7 @@ void main_loop(const Dictionary &iDic)
}
Duplicate *game = GameFactory::Instance()->createDuplicate(iDic);
for (i = 0; i < _wtoi(token); i++)
game->addHumanPlayer();
game->addPlayer(new HumanPlayer);
token = next_token_digit(NULL, delim, &state);
if (token == NULL)
{
@ -967,7 +969,7 @@ void main_loop(const Dictionary &iDic)
break;
}
for (i = 0; i < _wtoi(token); i++)
game->addAIPlayer();
game->addPlayer(new AIPercent(1));
game->start();
loop_duplicate(*game);
GameFactory::Instance()->releaseGame(*game);
@ -985,7 +987,7 @@ void main_loop(const Dictionary &iDic)
}
FreeGame *game = GameFactory::Instance()->createFreeGame(iDic);
for (i = 0; i < _wtoi(token); i++)
game->addHumanPlayer();
game->addPlayer(new HumanPlayer);
token = next_token_digit(NULL, delim, &state);
if (token == NULL)
{
@ -993,7 +995,7 @@ void main_loop(const Dictionary &iDic)
break;
}
for (i = 0; i < _wtoi(token); i++)
game->addAIPlayer();
game->addPlayer(new AIPercent(1));
game->start();
loop_freegame(*game);
GameFactory::Instance()->releaseGame(*game);
@ -1003,8 +1005,8 @@ void main_loop(const Dictionary &iDic)
{
// New duplicate game
Duplicate *game = GameFactory::Instance()->createDuplicate(iDic);
game->addHumanPlayer();
game->addAIPlayer();
game->addPlayer(new HumanPlayer);
game->addPlayer(new AIPercent(1));
game->start();
loop_duplicate(*game);
GameFactory::Instance()->releaseGame(*game);
@ -1014,8 +1016,8 @@ void main_loop(const Dictionary &iDic)
{
// New free game
FreeGame *game = GameFactory::Instance()->createFreeGame(iDic);
game->addHumanPlayer();
game->addAIPlayer();
game->addPlayer(new HumanPlayer);
game->addPlayer(new AIPercent(1));
game->start();
loop_freegame(*game);
GameFactory::Instance()->releaseGame(*game);