diff --git a/AI/src/game/penguin.cpp b/AI/src/game/penguin.cpp index 7e1a7e026a87fb899ffda2bf8f9d0b803e5b2c9b..6848ce67dce6deefcd7df5d1dd7e5bcb545a6e40 100644 --- a/AI/src/game/penguin.cpp +++ b/AI/src/game/penguin.cpp @@ -141,48 +141,42 @@ namespace game */ void penguin::move_penguin(uint32_t* p, uint16_t rel_move) { - uint32_t penguin_copy = (*p) >> 12; //Direction A - if((penguin_copy & 7) > rel_move) //If the penguin total moves in this direction are greater than the move we want to do for it (not equal because moves start at 0) + if(PENGUIN_MOVES_A(*p) > rel_move) //If the penguin total moves in this direction are greater than the move we want to do for it (not equal because moves start at 0) { //Move direction A (*p) = (7 * (rel_move +1)) + ((*p) & 63); return; } - rel_move -= penguin_copy & 7; - penguin_copy = penguin_copy >> 3; - if((penguin_copy & 7) > rel_move) + rel_move -= PENGUIN_MOVES_A(*p); + if(PENGUIN_MOVES_B(*p) > rel_move) { //Move direction B (*p) = (-1 * (rel_move +1)) + ((*p) & 63); return; } - rel_move -= penguin_copy & 7; - penguin_copy = penguin_copy >> 3; - if((penguin_copy & 7) > rel_move) + rel_move -= PENGUIN_MOVES_B(*p); + if(PENGUIN_MOVES_C(*p) > rel_move) { //Move direction C (*p) = (-8 * (rel_move +1)) + ((*p) & 63); return; } - rel_move -= penguin_copy & 7; - penguin_copy = penguin_copy >> 3; - if((penguin_copy & 7) > rel_move) + rel_move -= PENGUIN_MOVES_C(*p); + if(PENGUIN_MOVES_D(*p) > rel_move) { //Move direction D (*p) = (-7 * (rel_move +1)) + ((*p) & 63); return; } - rel_move -= penguin_copy & 7; - penguin_copy = penguin_copy >> 3; - if((penguin_copy & 7) > rel_move) + rel_move -= PENGUIN_MOVES_D(*p); + if(PENGUIN_MOVES_E(*p) > rel_move) { //Move direction E (*p) = (1 * (rel_move +1)) + ((*p) & 63); return; } - rel_move -= penguin_copy & 7; - penguin_copy = penguin_copy >> 3; + rel_move -= PENGUIN_MOVES_E(*p); //Move direction F (*p) = (8 * (rel_move +1)) + ((*p) & 63); } @@ -268,39 +262,36 @@ namespace game { /* We search for the first penguin that can make the move. If a penguin can't, we will decrese the move number by the number of moves that he can do */ - #define TOT_MOVES(penguin) ((penguin >> 6) & 63) - for(i = 0; (i < 3) && (TOT_MOVES(state.peng_red[i]) <= rel_move); i ++) //While we didn't find the penguin - rel_move -= TOT_MOVES(state.peng_red[i]); + for(i = 0; (i < 3) && (PENGUIN_TOT_MOVES(state.peng_red[i]) <= rel_move); i ++) //While we didn't find the penguin + rel_move -= PENGUIN_TOT_MOVES(state.peng_red[i]); // Now i is the number of the penguin that will move and rel_move is the move relative to this penguin (because we decreased it) peng = &state.peng_red[i]; } else //If blue { - #define TOT_MOVES(penguin) ((penguin >> 6) & 63) - for(i = 0; (i < 3) && (TOT_MOVES(state.peng_blue[i]) <= rel_move); i ++) //While we didn't find the penguin - rel_move -= TOT_MOVES(state.peng_blue[i]); + for(i = 0; (i < 3) && (PENGUIN_TOT_MOVES(state.peng_blue[i]) <= rel_move); i ++) //While we didn't find the penguin + rel_move -= PENGUIN_TOT_MOVES(state.peng_blue[i]); peng = &state.peng_blue[i]; } // ADD PENGUIN TILE TO THE SCORE - #define POS(penguin) ((penguin) & 63) - if((state.one_fish >> POS(*peng)) & 1) //If there is a one fish on this position + if((state.one_fish >> PENGUIN_POS(*peng)) & 1) //If there is a one fish on this position { if(state.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) << POS(*peng)); + state.one_fish = state.one_fish & ~(((uint64_t) 1) << PENGUIN_POS(*peng)); } - else if((state.two_fish >> POS(*peng)) & 1) + else if((state.two_fish >> PENGUIN_POS(*peng)) & 1) { if(state.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) << POS(*peng)); + state.two_fish = state.two_fish & ~(((uint64_t) 1) << PENGUIN_POS(*peng)); } else { @@ -309,7 +300,7 @@ namespace game else state.score_blue += 3; //We replace this tile with an empty one (0 in the bitboard) - state.one_fish = state.one_fish & ~(((uint64_t) 1) << POS(*peng)); + state.one_fish = state.one_fish & ~(((uint64_t) 1) << PENGUIN_POS(*peng)); } // MOVE THE PENGUIN diff --git a/AI/src/game/penguin.hpp b/AI/src/game/penguin.hpp index c8aa7a76e80357006e98a817baf09c1679315621..33456409ba4d2d45abdb7f13cd8b110cd28a83a9 100644 --- a/AI/src/game/penguin.hpp +++ b/AI/src/game/penguin.hpp @@ -42,6 +42,16 @@ namespace game bool canPlay_blue = true; }; + // Useful macros to get values out of penguins + #define PENGUIN_POS(penguin) ((penguin) & 63) + #define PENGUIN_TOT_MOVES(penguin) (((penguin) >> 6) & 63) + #define PENGUIN_MOVES_A(penguin) (((penguin) >> 12) & 7) + #define PENGUIN_MOVES_B(penguin) (((penguin) >> 15) & 7) + #define PENGUIN_MOVES_C(penguin) (((penguin) >> 18) & 7) + #define PENGUIN_MOVES_D(penguin) (((penguin) >> 21) & 7) + #define PENGUIN_MOVES_E(penguin) (((penguin) >> 24) & 7) + #define PENGUIN_MOVES_F(penguin) (((penguin) >> 27) & 7) + class penguin : public game<penguin_state> { public: