#include "game.h" #include #include #ifdef DEBUG #include #endif #include "../common/set.h" Game::p Game::create(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4) { Game::p p(new Game(player_1, player_2, player_3, player_4)); p->start(); return p; } Game::~Game() { std::cout << "Game destroyed." << std::endl; delete ruleset; } Game::Game(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4) { players.push_back(player_1); players.push_back(player_2); players.push_back(player_3); players.push_back(player_4); } void Game::handle_ready() { std::cout << "Still waiting for " << waiting_players << "." << std::endl; if(--waiting_players) { return; } std::cout << "All ready!" << std::endl; round_start(); } void Game::round_start() { players[0]->round_start(); players[1]->round_start(); players[2]->round_start(); players[3]->round_start(); ruleset = new RuleSet::Standard(); ruleset->round_start(); round_update(); } void Game::round_update() { State gamestate = ruleset->round_update(); // Send the updates state to the players players[0]->round_state(gamestate); players[1]->round_state(gamestate); players[2]->round_state(gamestate); players[3]->round_state(gamestate); // Only implemented discard so far, so only current player that needs to be able to do a action. int smart = 0; for(Actions::iterator it = gamestate.possible_actions.begin(); it != gamestate.possible_actions.end(); ++it) { //smart = smart | (1 << it->player); } int num_player_actions = 0; if(smart & 1) { num_player_actions++; players[0]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1), gamestate.possible_actions); } if(smart & 2) { num_player_actions++; players[1]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1), gamestate.possible_actions); } if(smart & 4) { num_player_actions++; players[2]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1), gamestate.possible_actions); } if(smart & 8) { num_player_actions++; players[3]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1), gamestate.possible_actions); } if(num_player_actions == 0) { round_update(); } } void Game::handle_action(Action action) { //players[action.player]->kill_action(); bool done = ruleset->round_action(action); if(done) { round_update(); } // Sjekk action, sjekk om endelig action er avgjort // Oppdater state // Evt. round_end() } void Game::round_end() { // Flere runder? round_start() // Ferdig? game_end() players[0]->round_end(); players[1]->round_end(); players[2]->round_end(); players[3]->round_end(); #ifdef DEBUG time_t current_time = std::time(0); std::cout << std::ctime(¤t_time) << " Round is over..." << std::endl; #endif } void Game::start() { std::cout << "Started a game with " << players[0]->nick() << ", " << players[1]->nick() << ", " << players[2]->nick() << " and " << players[3]->nick() << "." << std::endl; players[0]->id = 0; players[1]->id = 1; players[2]->id = 2; players[3]->id = 3; waiting_players = 4; players[0]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players); players[1]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players); players[2]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players); players[3]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players); }