From dafda3d524e6ef31de92a74779cec631878b0263 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 27 Nov 2010 07:22:27 +0100 Subject: Using CyclicInt for PlayerNum. --- server/game.cpp | 41 +++++++++++++++++++++-------------------- server/game.h | 5 ++++- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/server/game.cpp b/server/game.cpp index 5319e1b..3fd7961 100644 --- a/server/game.cpp +++ b/server/game.cpp @@ -97,21 +97,22 @@ void Game::round_update_draw() { Actions possible_actions = players[current_player].get_actions_draw(); // Construct and send state to each client. - for(int i = 0; i < 4; i++) { + PlayerNum player = 0; + do { Player::State state[4]; - state[0] = players[i].get_state(); - state[1] = players[(i + 1) % 4].get_state_filtered(); - state[2] = players[(i + 2) % 4].get_state_filtered(); - state[3] = players[(i + 3) % 4].get_state_filtered(); + state[0] = players[player].get_state(); + state[1] = players[player + 1].get_state_filtered(); + state[2] = players[player + 2].get_state_filtered(); + state[3] = players[player + 3].get_state_filtered(); Actions a; - if(i == current_player) { + if(player == current_player) { a = possible_actions; } - players[i].client->round_state(state[0], state[1], state[2], state[3], Tiles(), a); - } + players[player].client->round_state(state[0], state[1], state[2], state[3], Tiles(), a); + } while(++player); // Await action from client. players[current_player].client->get_action(boost::bind(&Game::handle_action_draw, shared_from_this(), _1), possible_actions); @@ -123,23 +124,24 @@ void Game::round_update_discard() { std::map possible_actions; // Construct and send state to each client. - for(int i = 0; i < 4; i++) { + PlayerNum player = 0; + do { Player::State state[4]; - state[0] = players[i].get_state(); - state[1] = players[(i + 1) % 4].get_state_filtered(); - state[2] = players[(i + 2) % 4].get_state_filtered(); - state[3] = players[(i + 3) % 4].get_state_filtered(); + state[0] = players[player].get_state(); + state[1] = players[player + 1].get_state_filtered(); + state[2] = players[player + 2].get_state_filtered(); + state[3] = players[player + 3].get_state_filtered(); Actions a; - if(i != current_player) { - if(a = players[i].get_actions_discard(discarded_tile)) { - possible_actions[i] = a; + if(player != current_player) { + if(a = players[player].get_actions_discard(discarded_tile)) { + possible_actions[player] = a; } } - players[i].client->round_state(state[0], state[1], state[2], state[3], Tiles(), a); - } + players[player].client->round_state(state[0], state[1], state[2], state[3], Tiles(), a); + } while(++player); preceding_action = Action::Pass; if(possible_actions.empty()) { @@ -224,8 +226,7 @@ void Game::handle_action_discard(Action action, int player) { switch(preceding_action.type) { case Action::Pass: { // Tile not claimed, next player draws. - current_player = (current_player + 1) % 4; - players[current_player].draw(wall.take_one()); + players[++current_player].draw(wall.take_one()); round_update_draw(); } break; diff --git a/server/game.h b/server/game.h index 5fd85fd..1e293f6 100644 --- a/server/game.h +++ b/server/game.h @@ -9,6 +9,7 @@ #include "wall.h" #include "client.h" #include "../common/action.h" +#include "../common/cyclicint.h" class Game : public boost::enable_shared_from_this { public: @@ -19,6 +20,8 @@ class Game : public boost::enable_shared_from_this { ~Game(); private: + typedef CyclicInt<4> PlayerNum; + class Player { public: Client::p client; @@ -76,7 +79,7 @@ class Game : public boost::enable_shared_from_this { Player players[4]; - int current_player; + PlayerNum current_player; int awaiting_players; -- cgit v1.2.3