Skip to content
Snippets Groups Projects
Commit d6ad720a authored by Bariatti Francesco's avatar Bariatti Francesco
Browse files

When a player can not play, a boolean is set, possible moves is set to 1 and...

When a player can not play, a boolean is set, possible moves is set to 1  and the game accepts whatever value as a move.
parent b833b13c
No related branches found
No related tags found
No related merge requests found
...@@ -28,12 +28,6 @@ namespace game ...@@ -28,12 +28,6 @@ namespace game
state.nb_moves_blue += update_moves(&state.p2_blue, obstacles); state.nb_moves_blue += update_moves(&state.p2_blue, obstacles);
state.nb_moves_blue += update_moves(&state.p3_blue, obstacles); state.nb_moves_blue += update_moves(&state.p3_blue, obstacles);
state.nb_moves_blue += update_moves(&state.p4_blue, obstacles); state.nb_moves_blue += update_moves(&state.p4_blue, obstacles);
//Change player if the other one can play
if(state.current_player_red && state.nb_moves_blue > 0)
state.current_player_red = false;
else if(state.current_player_red == false && state.nb_moves_red > 0)
state.current_player_red = true;
} }
shared_ptr<game<penguin_state>> penguin::do_copy() const shared_ptr<game<penguin_state>> penguin::do_copy() const
...@@ -53,7 +47,7 @@ namespace game ...@@ -53,7 +47,7 @@ namespace game
bool penguin::end_of_game() const bool penguin::end_of_game() const
{ {
return state.nb_moves_red == 0 && state.nb_moves_blue == 0; return state.canPlay_red == false && state.canPlay_blue == false;
} }
bool penguin::won(std::uint8_t player) const bool penguin::won(std::uint8_t player) const
...@@ -290,42 +284,45 @@ namespace game ...@@ -290,42 +284,45 @@ namespace game
//Play the mth move in the possible moves list. //Play the mth move in the possible moves list.
void penguin::play(uint16_t m) void penguin::play(uint16_t m)
{ {
//Find which penguin will move if ((state.current_player_red == true && state.canPlay_red) || ((state.current_player_red == false) && state.canPlay_blue))
uint32_t* p = penguin_that_moves(m);
uint8_t position = (*p) & 63;
//Find the value of the tile the penguin is on and update score
if ((state.one_fish >> position) & 1)
{
if(current_player() == RED)
state.score_red += 1;
else
state.score_blue += 1;
//We replace this tile with an empty one (0 in the bitboard)
state.one_fish = state.one_fish & ~(((uint64_t) 1) << position);
}
else if ((state.two_fish >> position) & 1)
{ {
//Find which penguin will move
uint32_t* p = penguin_that_moves(m);
uint8_t position = (*p) & 63;
if(current_player() == RED) //Find the value of the tile the penguin is on and update score
state.score_red += 2; if ((state.one_fish >> position) & 1)
else {
state.score_blue += 2; if(current_player() == RED)
//We replace this tile with an empty one (0 in the bitboard) state.score_red += 1;
state.two_fish = state.two_fish & ~(((uint64_t) 1) << position); else
} state.score_blue += 1;
else //We replace this tile with an empty one (0 in the bitboard)
{ state.one_fish = state.one_fish & ~(((uint64_t) 1) << position);
if(current_player() == RED) }
state.score_red += 3; else if ((state.two_fish >> position) & 1)
{
if(current_player() == RED)
state.score_red += 2;
else
state.score_blue += 2;
//We replace this tile with an empty one (0 in the bitboard)
state.two_fish = state.two_fish & ~(((uint64_t) 1) << position);
}
else else
state.score_blue += 3; {
//We replace this tile with an empty one (0 in the bitboard) if(current_player() == RED)
state.three_fish = state.three_fish & ~(((uint64_t) 1) << position); state.score_red += 3;
} else
state.score_blue += 3;
//We replace this tile with an empty one (0 in the bitboard)
state.three_fish = state.three_fish & ~(((uint64_t) 1) << position);
}
//Move the current penguin //Move the current penguin
move_penguin(p); move_penguin(p);
}
//Update moves on all penguins of the next player //Update moves on all penguins of the next player
uint64_t obstacles = (~(state.one_fish | state.two_fish | state.three_fish)); uint64_t obstacles = (~(state.one_fish | state.two_fish | state.three_fish));
...@@ -343,35 +340,45 @@ namespace game ...@@ -343,35 +340,45 @@ namespace game
if (state.current_player_red) //Red just played if (state.current_player_red) //Red just played
{ {
state.nb_moves_blue += update_moves(&state.p1_blue, obstacles); if(state.canPlay_blue)
state.nb_moves_blue += update_moves(&state.p2_blue, obstacles);
state.nb_moves_blue += update_moves(&state.p3_blue, obstacles);
state.nb_moves_blue += update_moves(&state.p4_blue, obstacles);
if (state.nb_moves_blue == 0) //If Blue can not move we update moves for red
{ {
state.nb_moves_red += update_moves(&state.p1_red, obstacles); state.nb_moves_blue += update_moves(&state.p1_blue, obstacles);
state.nb_moves_red += update_moves(&state.p2_red, obstacles); state.nb_moves_blue += update_moves(&state.p2_blue, obstacles);
state.nb_moves_red += update_moves(&state.p3_red, obstacles); state.nb_moves_blue += update_moves(&state.p3_blue, obstacles);
state.nb_moves_red += update_moves(&state.p4_red, obstacles); state.nb_moves_blue += update_moves(&state.p4_blue, obstacles);
if (state.nb_moves_blue == 0)
{
state.canPlay_blue = false;
state.nb_moves_blue = 1; //We create an artificial move so that the mcts works
}
} }
else else
{ state.current_player_red = false; } {
state.nb_moves_blue = 1;
}
state.current_player_red = false;
} }
else //Blue just played else //Blue just played
{ {
state.nb_moves_red += update_moves(&state.p1_red, obstacles); if(state.canPlay_red)
state.nb_moves_red += update_moves(&state.p2_red, obstacles);
state.nb_moves_red += update_moves(&state.p3_red, obstacles);
state.nb_moves_red += update_moves(&state.p4_red, obstacles);
if (state.nb_moves_red == 0)
{ {
state.nb_moves_blue += update_moves(&state.p1_blue, obstacles); state.nb_moves_red += update_moves(&state.p1_red, obstacles);
state.nb_moves_blue += update_moves(&state.p2_blue, obstacles); state.nb_moves_red += update_moves(&state.p2_red, obstacles);
state.nb_moves_blue += update_moves(&state.p3_blue, obstacles); state.nb_moves_red += update_moves(&state.p3_red, obstacles);
state.nb_moves_blue += update_moves(&state.p4_blue, obstacles); state.nb_moves_red += update_moves(&state.p4_red, obstacles);
if (state.nb_moves_red == 0)
{
state.canPlay_red = false;
state.nb_moves_red = 1;
}
} }
else else
{ state.current_player_red = true; } {
state.nb_moves_red = 1;
}
state.current_player_red = true;
} }
} }
...@@ -419,8 +426,8 @@ namespace game ...@@ -419,8 +426,8 @@ namespace game
json_state["current_player"] = state.current_player_red ? "Red" : "Blue"; json_state["current_player"] = state.current_player_red ? "Red" : "Blue";
json_state["nb_moves"]["red"] = state.nb_moves_red; json_state["can_play"]["red"] = state.canPlay_red;
json_state["nb_moves"]["blue"] = state.nb_moves_blue; json_state["can_play"]["blue"] = state.canPlay_blue;
return json_state; return json_state;
} }
......
...@@ -27,13 +27,16 @@ namespace game ...@@ -27,13 +27,16 @@ namespace game
uint32_t p3_blue = 43; uint32_t p3_blue = 43;
uint32_t p4_blue = 49; uint32_t p4_blue = 49;
int score_red = 6; int score_red = 0;
int score_blue = 3; int score_blue = 0;
bool current_player_red = false; //True if red must play now bool current_player_red = true; //True if red must play now. Red always starts
int nb_moves_red = 0; //Number of moves the red player can play int nb_moves_red = 0; //Number of moves the red player can play
int nb_moves_blue = 0; int nb_moves_blue = 0;
bool canPlay_red = true;
bool canPlay_blue = true;
}; };
class penguin : public game<penguin_state> class penguin : public game<penguin_state>
......
...@@ -69,7 +69,7 @@ namespace mcts ...@@ -69,7 +69,7 @@ namespace mcts
void test_mcts_two_players<Game>::play(Game g) void test_mcts_two_players<Game>::play(Game g)
{ {
// ProfilerStart("theturk.prof"); // ProfilerStart("theturk.prof");
auto the_turk = make_mcts_two_players(g, 20000, 0.4, 8); auto the_turk = make_mcts_two_players(g, 2000, 0.4, 8);
std::cout << "play one game" << std::endl; std::cout << "play one game" << std::endl;
std::cout << "who's first? (h)uman/(c)omputer "; std::cout << "who's first? (h)uman/(c)omputer ";
std::string ans; std::string ans;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment