The dev goes on
[vic.git] / src / game_container.cpp
blobe19ce2904715aee975d1ca3e7140871860df8896
1 #include "game_container.h"
2 #include <cstdlib>
4 GameContainer::GameContainer (Game* game, IGameRules* rules) :
5 game (game),
6 players(new std::list<Player*>),
7 rules (rules),
8 started (false),
9 ended (false)
13 GameContainer::~GameContainer ()
15 for (std::list<Player*>::iterator iter = players->begin();
16 iter != players->end(); ++iter) { delete(*iter); }
17 delete(players);
20 bool GameContainer::gameEnded () const
22 return ended;
25 int GameContainer::addPlayer (Player* player)
27 if (!started && player != NULL) {
28 players->push_back(player);
29 return 0;
31 return -1;
34 void GameContainer::start ()
36 int END_GAME = 1;
38 int result = 0;
39 std::list<Player*>::iterator iter;
41 started = true;
42 while (result != END_GAME && players->size() > 0) {
43 if (rules->gameEnded(*game)) {
44 result = END_GAME;
47 for (iter = players->begin();
48 iter != players->end() && result != END_GAME; ++iter) {
49 result = onTurnBegin(*iter);
50 result = doPlayerTurn(*iter);
51 result = onTurnEnd(*iter);
53 game->setTurn(game->getTurn()+1);
56 ended = true;
59 int GameContainer::doPlayerTurn (Player* player)
61 ICommand* cmd;
63 while ((cmd = player->nextCommand()) != NULL) {
64 if (cmd->execute(game) != 0) {
65 cmd->revert(game);
68 return 0;
71 int GameContainer::onTurnBegin (Player* player)
73 return 0;
76 int GameContainer::onTurnEnd (Player* player)
78 return 0;