#include "game.h" #include #include 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; } 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(); // Sett opp runden game_state = make_shared(); east_action = false; west_action = false; south_action = false; north_action = false; num_player_actions = 0; // Simulates drawing 4, 4 ,4 for each player for (int player_num = 0; player_num < 4; player_num++) { for (int i = 0; i < 3; i++) { for (int y = 0; y < 4; y++) { game_state->players[player_num].hand.push_back(wall.take_one()); } } } // Simulates the second part of drawing with only 1 tile for (int player_num = 0; player_num < 4; player_num++) { game_state->players[player_num].hand.push_back(wall.take_one()); } // Gives an extra tile to the first player (east) game_state->players[0].hand.push_back(wall.take_one()); // Set the action that east can do. Action discard; discard.type = Action::Discard; game_state->possible_actions.push_back(discard); east_action = true; num_player_actions++; round_update(); } void Game::round_update() { // Send RoundState, inkl. liste av mulige actions. players[0]->round_state(game_state); players[1]->round_state(game_state); players[2]->round_state(game_state); players[3]->round_state(game_state); // Kall player->get_action(handle_action) for hver spiller som har actions if (east_action) { players[0]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1)); } if (west_action) { players[1]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1)); } if (south_action) { players[2]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1)); } if (north_action) { players[3]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1)); } } void Game::handle_action(Action action) { // Sjekk action, sjekk om endelig action er avgjort // Oppdater state // Evt. round_end() if (num_player_actions == 0) { round_update(); } } void Game::round_end() { // Flere runder? round_start() // Ferdig? game_end() } void Game::start() { std::cout << "Started a game with " << players[0]->nick() << ", " << players[1]->nick() << ", " << players[2]->nick() << " and " << players[3]->nick() << "." << std::endl; 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); }