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

Changed implementation of penguin_that_moves for penguin arrays. Deleted the...

Changed implementation of penguin_that_moves for penguin arrays. Deleted the function and integrated the code in play function
parent 137863a0
No related branches found
No related tags found
No related merge requests found
...@@ -135,84 +135,6 @@ namespace game ...@@ -135,84 +135,6 @@ namespace game
return state.current_player_red ? state.nb_moves_red : state.nb_moves_blue; return state.current_player_red ? state.nb_moves_red : state.nb_moves_blue;
} }
/* The penguin that will move if we want to play the #move_number move in the list of possible moves.
* What this function does:
* Find the penguin that will move.
* Change its total number of moves so that it is relative to that penguin's possible moves and not relative to ALL the penguins' moves.
* Return a pointer to that penguin
* */
uint32_t* penguin::penguin_that_moves(uint16_t move_number)
{
if(state.current_player_red)
{
if(((state.p1_red >> 6) & 63) > move_number)
{
uint32_t* p = &state.p1_red;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6;
return p;
}
move_number -= (state.p1_red >> 6) & 63;
if(((state.p2_red >> 6) & 63) > move_number)
{
uint32_t* p = &state.p2_red;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6;
return p;
}
move_number -= (state.p2_red >> 6) & 63;
if(((state.p3_red >> 6) & 63) > move_number)
{
uint32_t* p = &state.p3_red;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6;
return p;
}
move_number -= (state.p3_red >> 6) & 63;
uint32_t* p = &state.p4_red;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6;
return p;
}
else
{
if(((state.p1_blue >> 6) & 63) > move_number)
{
uint32_t* p = &state.p1_blue;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6;
return p;
}
move_number -= (state.p1_blue >> 6) & 63;
if(((state.p2_blue >> 6) & 63) > move_number)
{
uint32_t* p = &state.p2_blue;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6;
return p;
}
move_number -= (state.p2_blue >> 6) & 63;
if(((state.p3_blue >> 6) & 63) > move_number)
{
uint32_t* p = &state.p3_blue;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6;
return p;
}
move_number -= (state.p3_blue >> 6) & 63;
uint32_t* p = &state.p4_blue;
(*p) = (*p) & 0xFFFFF03F; //Reset move number for the penguin to 0
(*p) = (*p) | ((uint32_t) move_number) << 6;
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
...@@ -331,13 +253,36 @@ namespace game ...@@ -331,13 +253,36 @@ 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)
{ {
//CAN WE PLAY?
// We check if we can effectively play: if yes, the move is parsed and player, otherwise we do nothing (the move can be whatever, we won't look at it, so the player actually skip the turn)
if ((state.current_player_red == true && state.canPlay_red) || ((state.current_player_red == false) && state.canPlay_blue)) if ((state.current_player_red == true && state.canPlay_red) || ((state.current_player_red == false) && state.canPlay_blue))
{ {
//Find which penguin will move //WHICH PENGUIN WILL MOVE?
uint32_t* p = penguin_that_moves(m); uint32_t* peng; // The penguin that will move
uint8_t position = (*p) & 63; uint16_t rel_move = m; // Move number relative to this penguin
int i = 0;
if(state.current_player_red)
{
/* 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]);
// 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]);
peng = &state.peng_blue[i];
}
// ADD PENGUIN TILE TO THE SCORE
//Find the value of the tile the penguin is on and update score /*
//TODO: implements methods with arrays
if ((state.one_fish >> position) & 1) if ((state.one_fish >> position) & 1)
{ {
if(current_player() == RED) if(current_player() == RED)
...@@ -369,6 +314,7 @@ namespace game ...@@ -369,6 +314,7 @@ namespace game
//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
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "game.hpp" #include "game.hpp"
#include "json.hpp" #include "json.hpp"
using json = nlohmann::json; using json = nlohmann::json;
#include <random> #include <random>
#include <array> #include <array>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
...@@ -13,10 +13,10 @@ namespace game ...@@ -13,10 +13,10 @@ namespace game
{ {
struct penguin_state struct penguin_state
{ {
uint64_t one_fish = 1152921504606846975; //Position of one-fish tiles (bitboard) uint64_t one_fish = 1152921504606846975; //Position of one-fish tiles (bitboard)
uint64_t two_fish = 0; //Position of two-fish tiles (bitboard) uint64_t two_fish = 0; //Position of two-fish tiles (bitboard)
uint64_t three_fish = 0; //Position of three-fish tiles (bitboard) uint64_t three_fish = 0; //Position of three-fish tiles (bitboard)
//Penguins //Penguins
uint32_t peng_red[4] = {0, 1, 6, 7}; uint32_t peng_red[4] = {0, 1, 6, 7};
uint32_t peng_blue[4] = {59, 58, 53, 54}; uint32_t peng_blue[4] = {59, 58, 53, 54};
...@@ -29,19 +29,19 @@ namespace game ...@@ -29,19 +29,19 @@ namespace game
uint32_t p2_blue = 58; uint32_t p2_blue = 58;
uint32_t p3_blue = 53; uint32_t p3_blue = 53;
uint32_t p4_blue = 54; uint32_t p4_blue = 54;
int score_red = 0; int score_red = 0;
int score_blue = 0; int score_blue = 0;
bool current_player_red = true; //True if red must play now. Red always starts 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_red = true;
bool canPlay_blue = true; bool canPlay_blue = true;
}; };
class penguin : public game<penguin_state> class penguin : public game<penguin_state>
{ {
public: public:
...@@ -68,17 +68,16 @@ namespace game ...@@ -68,17 +68,16 @@ namespace game
std::shared_ptr<game<penguin_state>> do_copy() const; std::shared_ptr<game<penguin_state>> do_copy() const;
std::uint64_t hash(std::uint16_t m) const; std::uint64_t hash(std::uint16_t m) const;
std::uint64_t hash() const; std::uint64_t hash() const;
private: private:
//TODO: modify/delete functions //TODO: modify/delete functions
penguin_state state; penguin_state state;
uint32_t* penguin_that_moves(uint16_t move_number);
void move_penguin(uint32_t* p); void move_penguin(uint32_t* p);
int update_moves(uint32_t* p, uint64_t obstacles); int update_moves(uint32_t* p, uint64_t obstacles);
const uint8_t RED = 0; const uint8_t RED = 0;
const uint8_t BLUE = 1; const uint8_t BLUE = 1;
}; };
std::ostream& operator<<(std::ostream& os, const penguin& pen); std::ostream& operator<<(std::ostream& os, const penguin& pen);
} }
......
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