Skip to content
Snippets Groups Projects
Commit 986cf224 authored by Elnath's avatar Elnath
Browse files

State is entirely printed to json

parent 2ecb0ccf
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,7 @@ namespace game ...@@ -17,7 +17,7 @@ namespace game
obstacles |= ((uint64_t) 1) << (state.p2_blue & 63); obstacles |= ((uint64_t) 1) << (state.p2_blue & 63);
obstacles |= ((uint64_t) 1) << (state.p3_blue & 63); obstacles |= ((uint64_t) 1) << (state.p3_blue & 63);
obstacles |= ((uint64_t) 1) << (state.p4_blue & 63); obstacles |= ((uint64_t) 1) << (state.p4_blue & 63);
state.nb_moves_red = 0; state.nb_moves_red = 0;
state.nb_moves_blue = 0; state.nb_moves_blue = 0;
state.nb_moves_red += update_moves(&state.p1_red, obstacles); state.nb_moves_red += update_moves(&state.p1_red, obstacles);
...@@ -28,7 +28,7 @@ namespace game ...@@ -28,7 +28,7 @@ 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 //Change player if the other one can play
if(state.current_player_red && state.nb_moves_blue > 0) if(state.current_player_red && state.nb_moves_blue > 0)
state.current_player_red = false; state.current_player_red = false;
...@@ -45,10 +45,10 @@ namespace game ...@@ -45,10 +45,10 @@ namespace game
{ {
return state; return state;
} }
void penguin::set_state(const penguin_state& s) void penguin::set_state(const penguin_state& s)
{ {
state = s; state = s;
} }
bool penguin::end_of_game() const bool penguin::end_of_game() const
...@@ -61,7 +61,7 @@ namespace game ...@@ -61,7 +61,7 @@ namespace game
if (player == RED) return state.score_red > state.score_blue; if (player == RED) return state.score_red > state.score_blue;
return state.score_blue > state.score_red; return state.score_blue > state.score_red;
} }
bool penguin::lost(std::uint8_t player) const bool penguin::lost(std::uint8_t player) const
{ {
if(player == RED) return state.score_red < state.score_blue; if(player == RED) return state.score_red < state.score_blue;
...@@ -76,7 +76,7 @@ namespace game ...@@ -76,7 +76,7 @@ namespace game
uint8_t penguin::current_player() const uint8_t penguin::current_player() const
{ {
return state.current_player_red ? RED : BLUE; return state.current_player_red ? RED : BLUE;
} }
int penguin::value(uint8_t player) const int penguin::value(uint8_t player) const
{ {
...@@ -94,7 +94,7 @@ namespace game ...@@ -94,7 +94,7 @@ namespace game
//return 9 - state.total_moves; //return 9 - state.total_moves;
return 0; return 0;
} }
/* The penguin that will move if we want to play the #move_number move in the list of possible moves. /* The penguin that will move if we want to play the #move_number move in the list of possible moves.
* What this function does: * What this function does:
* Find the penguin that will move. * Find the penguin that will move.
...@@ -113,7 +113,7 @@ namespace game ...@@ -113,7 +113,7 @@ namespace game
return p; return p;
} }
move_number -= (state.p1_red >> 6) & 63; move_number -= (state.p1_red >> 6) & 63;
if(((state.p2_red >> 6) & 63) > move_number) if(((state.p2_red >> 6) & 63) > move_number)
{ {
uint32_t* p = &state.p2_red; uint32_t* p = &state.p2_red;
...@@ -122,7 +122,7 @@ namespace game ...@@ -122,7 +122,7 @@ namespace game
return p; return p;
} }
move_number -= (state.p2_red >> 6) & 63; move_number -= (state.p2_red >> 6) & 63;
if(((state.p3_red >> 6) & 63) > move_number) if(((state.p3_red >> 6) & 63) > move_number)
{ {
uint32_t* p = &state.p3_red; uint32_t* p = &state.p3_red;
...@@ -131,7 +131,7 @@ namespace game ...@@ -131,7 +131,7 @@ namespace game
return p; return p;
} }
move_number -= (state.p3_red >> 6) & 63; move_number -= (state.p3_red >> 6) & 63;
uint32_t* p = &state.p4_red; uint32_t* p = &state.p4_red;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0 (*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6; (*p) = (*p) | ((uint32_t) move_number) << 6;
...@@ -147,7 +147,7 @@ namespace game ...@@ -147,7 +147,7 @@ namespace game
return p; return p;
} }
move_number -= (state.p1_blue >> 6) & 63; move_number -= (state.p1_blue >> 6) & 63;
if(((state.p2_blue >> 6) & 63) > move_number) if(((state.p2_blue >> 6) & 63) > move_number)
{ {
uint32_t* p = &state.p2_blue; uint32_t* p = &state.p2_blue;
...@@ -156,7 +156,7 @@ namespace game ...@@ -156,7 +156,7 @@ namespace game
return p; return p;
} }
move_number -= (state.p2_blue >> 6) & 63; move_number -= (state.p2_blue >> 6) & 63;
if(((state.p3_blue >> 6) & 63) > move_number) if(((state.p3_blue >> 6) & 63) > move_number)
{ {
uint32_t* p = &state.p3_blue; uint32_t* p = &state.p3_blue;
...@@ -165,14 +165,14 @@ namespace game ...@@ -165,14 +165,14 @@ namespace game
return p; return p;
} }
move_number -= (state.p3_blue >> 6) & 63; move_number -= (state.p3_blue >> 6) & 63;
uint32_t* p = &state.p4_blue; uint32_t* p = &state.p4_blue;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0 (*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6; (*p) = (*p) | ((uint32_t) move_number) << 6;
return p; return p;
} }
} }
void penguin::move_penguin(uint32_t* p) void penguin::move_penguin(uint32_t* p)
{ {
uint8_t move_number = ((*p) >> 6) & 63; //Move number for the current penguin uint8_t move_number = ((*p) >> 6) & 63; //Move number for the current penguin
...@@ -221,7 +221,7 @@ namespace game ...@@ -221,7 +221,7 @@ namespace game
//Move direction F //Move direction F
(*p) = (8 * move_number +1) + ((*p) & 63); (*p) = (8 * move_number +1) + ((*p) & 63);
} }
int penguin::update_moves(uint32_t* p, uint64_t obstacles) int penguin::update_moves(uint32_t* p, uint64_t obstacles)
{ {
#define IsFree(i) (((obstacles >> (i)) & 1) == 0) #define IsFree(i) (((obstacles >> (i)) & 1) == 0)
...@@ -230,7 +230,7 @@ namespace game ...@@ -230,7 +230,7 @@ namespace game
int i = pos; int i = pos;
uint32_t nbmoves = 0; uint32_t nbmoves = 0;
uint32_t total_moves = 0; uint32_t total_moves = 0;
//Direction A //Direction A
while(((i+7) < 60) && (i%15 != 0) && IsFree(i+7)) while(((i+7) < 60) && (i%15 != 0) && IsFree(i+7))
{ {
...@@ -283,18 +283,18 @@ namespace game ...@@ -283,18 +283,18 @@ namespace game
nbmoves++; total_moves++; nbmoves++; total_moves++;
} }
(*p) = (*p) | nbmoves << 27; (*p) = (*p) | nbmoves << 27;
(*p) = (*p) | total_moves << 6; (*p) = (*p) | total_moves << 6;
return total_moves; return total_moves;
} }
//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 //Find which penguin will move
uint32_t* p = penguin_that_moves(m); uint32_t* p = penguin_that_moves(m);
uint8_t position = (*p) & 63; uint8_t position = (*p) & 63;
//Find the value of the tile the penguin is on and update score //Find the value of the tile the penguin is on and update score
if ((state.one_fish >> position) & 1) if ((state.one_fish >> position) & 1)
{ {
...@@ -307,7 +307,7 @@ namespace game ...@@ -307,7 +307,7 @@ namespace game
} }
else if ((state.two_fish >> position) & 1) else if ((state.two_fish >> position) & 1)
{ {
if(current_player() == RED) if(current_player() == RED)
state.score_red += 2; state.score_red += 2;
else else
...@@ -324,12 +324,12 @@ namespace game ...@@ -324,12 +324,12 @@ namespace game
//We replace this tile with an empty one (0 in the bitboard) //We replace this tile with an empty one (0 in the bitboard)
state.three_fish = state.three_fish & ~(((uint64_t) 1) << position); state.three_fish = state.three_fish & ~(((uint64_t) 1) << position);
} }
//Move the current penguin //Move the current penguin
move_penguin(p); move_penguin(p);
cout << "Penguin p :" << *p << endl; cout << "Penguin p :" << *p << endl;
//Update moves on all penguins //Update moves on all penguins
uint64_t obstacles = (~(state.one_fish | state.two_fish | state.three_fish)); uint64_t obstacles = (~(state.one_fish | state.two_fish | state.three_fish));
obstacles |= ((uint64_t) 1) << (state.p1_red & 63); obstacles |= ((uint64_t) 1) << (state.p1_red & 63);
...@@ -340,9 +340,9 @@ namespace game ...@@ -340,9 +340,9 @@ namespace game
obstacles |= ((uint64_t) 1) << (state.p2_blue & 63); obstacles |= ((uint64_t) 1) << (state.p2_blue & 63);
obstacles |= ((uint64_t) 1) << (state.p3_blue & 63); obstacles |= ((uint64_t) 1) << (state.p3_blue & 63);
obstacles |= ((uint64_t) 1) << (state.p4_blue & 63); obstacles |= ((uint64_t) 1) << (state.p4_blue & 63);
cout << obstacles << endl; cout << obstacles << endl;
state.nb_moves_red = 0; state.nb_moves_red = 0;
state.nb_moves_blue = 0; state.nb_moves_blue = 0;
state.nb_moves_red += update_moves(&state.p1_red, obstacles); state.nb_moves_red += update_moves(&state.p1_red, obstacles);
...@@ -353,21 +353,21 @@ namespace game ...@@ -353,21 +353,21 @@ 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 //Change player if the other one can play
if(state.current_player_red && state.nb_moves_blue > 0) if(state.current_player_red && state.nb_moves_blue > 0)
state.current_player_red = false; state.current_player_red = false;
else if(state.current_player_red == false && state.nb_moves_red > 0) else if(state.current_player_red == false && state.nb_moves_red > 0)
state.current_player_red = true; state.current_player_red = true;
} }
string penguin::player_to_string(uint8_t player) const string penguin::player_to_string(uint8_t player) const
{ {
return player == RED ? "Red" : "Blue"; return player == RED ? "Red" : "Blue";
} }
string penguin::move_to_string(uint16_t m) const string penguin::move_to_string(uint16_t m) const
{ {
return std::to_string(m); return std::to_string(m);
...@@ -388,7 +388,7 @@ namespace game ...@@ -388,7 +388,7 @@ namespace game
json["bitboards"]["onefish"] = state.one_fish; json["bitboards"]["onefish"] = state.one_fish;
json["bitboards"]["twofish"] = state.two_fish; json["bitboards"]["twofish"] = state.two_fish;
json["bitboards"]["threefish"] = state.three_fish; json["bitboards"]["threefish"] = state.three_fish;
json["penguins"]["red"][0] = state.p1_red; json["penguins"]["red"][0] = state.p1_red;
json["penguins"]["red"][1] = state.p2_red; json["penguins"]["red"][1] = state.p2_red;
json["penguins"]["red"][2] = state.p3_red; json["penguins"]["red"][2] = state.p3_red;
...@@ -397,13 +397,15 @@ namespace game ...@@ -397,13 +397,15 @@ namespace game
json["penguins"]["blue"][1] = state.p2_blue; json["penguins"]["blue"][1] = state.p2_blue;
json["penguins"]["blue"][2] = state.p3_blue; json["penguins"]["blue"][2] = state.p3_blue;
json["penguins"]["blue"][3] = state.p4_blue; json["penguins"]["blue"][3] = state.p4_blue;
json["score"]["red"] = state.score_red; json["score"]["red"] = state.score_red;
json["score"]["blue"] = state.score_blue; json["score"]["blue"] = state.score_blue;
json["possible_moves"]["red"] = state.nb_moves_red; json["possible_moves"]["red"] = state.nb_moves_red;
json["possible_moves"]["blue"] = state.nb_moves_blue; json["possible_moves"]["blue"] = state.nb_moves_blue;
json["current_player"] = state.current_player_red ? "Red" : "Blue";
return json; return json;
} }
...@@ -423,7 +425,7 @@ namespace game ...@@ -423,7 +425,7 @@ namespace game
{ {
return 0; return 0;
} }
ostream& operator<<(ostream& os, const penguin& pen) ostream& operator<<(ostream& os, const penguin& pen)
{ {
os << pen.to_string(); os << pen.to_string();
......
...@@ -39,7 +39,10 @@ if __name__ == "__main__": ...@@ -39,7 +39,10 @@ if __name__ == "__main__":
for i in range(4): for i in range(4):
drawState.drawPenguin(state["penguins"]["blue"][i]) drawState.drawPenguin(state["penguins"]["blue"][i])
#PLAY NEXT MOVE #PLAY NEXT MOVE
move = input("Enter next move: ") if state["current_player"] == "Red":
move = input("Enter Red move: ")
else:
move = input("Enter Blue move: ")
program.stdin.write(move+"\n") program.stdin.write(move+"\n")
program.stdin.flush() program.stdin.flush()
except KeyboardInterrupt: except KeyboardInterrupt:
......
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