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

Added useful macros for extracting information from a penguin

parent e4260e7a
No related branches found
No related tags found
No related merge requests found
...@@ -141,48 +141,42 @@ namespace game ...@@ -141,48 +141,42 @@ namespace game
*/ */
void penguin::move_penguin(uint32_t* p, uint16_t rel_move) void penguin::move_penguin(uint32_t* p, uint16_t rel_move)
{ {
uint32_t penguin_copy = (*p) >> 12;
//Direction A //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 //Move direction A
(*p) = (7 * (rel_move +1)) + ((*p) & 63); (*p) = (7 * (rel_move +1)) + ((*p) & 63);
return; return;
} }
rel_move -= penguin_copy & 7; rel_move -= PENGUIN_MOVES_A(*p);
penguin_copy = penguin_copy >> 3; if(PENGUIN_MOVES_B(*p) > rel_move)
if((penguin_copy & 7) > rel_move)
{ {
//Move direction B //Move direction B
(*p) = (-1 * (rel_move +1)) + ((*p) & 63); (*p) = (-1 * (rel_move +1)) + ((*p) & 63);
return; return;
} }
rel_move -= penguin_copy & 7; rel_move -= PENGUIN_MOVES_B(*p);
penguin_copy = penguin_copy >> 3; if(PENGUIN_MOVES_C(*p) > rel_move)
if((penguin_copy & 7) > rel_move)
{ {
//Move direction C //Move direction C
(*p) = (-8 * (rel_move +1)) + ((*p) & 63); (*p) = (-8 * (rel_move +1)) + ((*p) & 63);
return; return;
} }
rel_move -= penguin_copy & 7; rel_move -= PENGUIN_MOVES_C(*p);
penguin_copy = penguin_copy >> 3; if(PENGUIN_MOVES_D(*p) > rel_move)
if((penguin_copy & 7) > rel_move)
{ {
//Move direction D //Move direction D
(*p) = (-7 * (rel_move +1)) + ((*p) & 63); (*p) = (-7 * (rel_move +1)) + ((*p) & 63);
return; return;
} }
rel_move -= penguin_copy & 7; rel_move -= PENGUIN_MOVES_D(*p);
penguin_copy = penguin_copy >> 3; if(PENGUIN_MOVES_E(*p) > rel_move)
if((penguin_copy & 7) > rel_move)
{ {
//Move direction E //Move direction E
(*p) = (1 * (rel_move +1)) + ((*p) & 63); (*p) = (1 * (rel_move +1)) + ((*p) & 63);
return; return;
} }
rel_move -= penguin_copy & 7; rel_move -= PENGUIN_MOVES_E(*p);
penguin_copy = penguin_copy >> 3;
//Move direction F //Move direction F
(*p) = (8 * (rel_move +1)) + ((*p) & 63); (*p) = (8 * (rel_move +1)) + ((*p) & 63);
} }
...@@ -268,39 +262,36 @@ namespace game ...@@ -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 /* 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 */ by the number of moves that he can do */
#define TOT_MOVES(penguin) ((penguin >> 6) & 63) for(i = 0; (i < 3) && (PENGUIN_TOT_MOVES(state.peng_red[i]) <= rel_move); i ++) //While we didn't find the penguin
for(i = 0; (i < 3) && (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]);
rel_move -= 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) // 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]; peng = &state.peng_red[i];
} }
else //If blue else //If blue
{ {
#define TOT_MOVES(penguin) ((penguin >> 6) & 63) for(i = 0; (i < 3) && (PENGUIN_TOT_MOVES(state.peng_blue[i]) <= rel_move); i ++) //While we didn't find the penguin
for(i = 0; (i < 3) && (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]);
rel_move -= TOT_MOVES(state.peng_blue[i]);
peng = &state.peng_blue[i]; peng = &state.peng_blue[i];
} }
// ADD PENGUIN TILE TO THE SCORE // ADD PENGUIN TILE TO THE SCORE
#define POS(penguin) ((penguin) & 63) if((state.one_fish >> PENGUIN_POS(*peng)) & 1) //If there is a one fish on this position
if((state.one_fish >> POS(*peng)) & 1) //If there is a one fish on this position
{ {
if(state.current_player_red) if(state.current_player_red)
state.score_red += 1; state.score_red += 1;
else else
state.score_blue += 1; state.score_blue += 1;
//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.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) if(state.current_player_red)
state.score_red += 2; state.score_red += 2;
else else
state.score_blue += 2; state.score_blue += 2;
//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.two_fish = state.two_fish & ~(((uint64_t) 1) << POS(*peng)); state.two_fish = state.two_fish & ~(((uint64_t) 1) << PENGUIN_POS(*peng));
} }
else else
{ {
...@@ -309,7 +300,7 @@ namespace game ...@@ -309,7 +300,7 @@ namespace game
else else
state.score_blue += 3; state.score_blue += 3;
//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.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 // MOVE THE PENGUIN
......
...@@ -42,6 +42,16 @@ namespace game ...@@ -42,6 +42,16 @@ namespace game
bool canPlay_blue = true; 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> class penguin : public game<penguin_state>
{ {
public: public:
......
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