8 #ifndef EMP_GAME_MANCALA_H 9 #define EMP_GAME_MANCALA_H 14 #include <unordered_map> 16 #include "../base/array.h" 17 #include "../base/assert.h" 18 #include "../base/vector.h" 19 #include "../tools/math.h" 33 bool side_A_empty =
true;
34 bool side_B_empty =
true;
36 for (
size_t i = 0; i < 6; i++) {
37 if (boardA[i] > 0) { side_A_empty =
false; }
38 if (boardB[i] > 0) { side_B_empty =
false; }
41 over = ( (is_A_turn && side_A_empty) || (!is_A_turn && side_B_empty));
47 Mancala(
bool A_first=
true) : boardA(), boardB(), over(false), is_A_turn(true) {
53 for (
size_t i = 0; i < 6; i++) { boardA[i] = 4; boardB[i] = 4; }
54 boardA[6] = boardB[6] = 0;
59 size_t GetA(
size_t i)
const {
return boardA[i]; }
60 size_t GetB(
size_t i)
const {
return boardB[i]; }
71 std::unordered_map<int, double>
AsInput(
size_t player_id)
const {
72 std::unordered_map<int, double> input_map;
73 size_t offset = (player_id == 0) ? 0 : 7;
74 for (
size_t i = 0; i < 7; i++) {
75 input_map[(int)(i+offset)] = (double) boardA[i];
76 input_map[(int)(i+7-offset)] = (double) boardB[i];
91 size_t stone_count = cur_board[cell];
92 size_t cur_cell = cell;
96 while (stone_count > 0) {
97 cur_cell = (cur_cell+1) % 13;
98 if (cur_cell < 7) cur_board[cur_cell]++;
99 else other_board[cur_cell-7]++;
112 if (cur_cell < 6 && cur_board[cur_cell] == 1) {
113 size_t clear_pos = 5 - cur_cell;
114 cur_board[6] += other_board[clear_pos];
115 other_board[clear_pos] = 0;
118 is_A_turn = (size_t) !is_A_turn;
138 if (move >= 6 ||
GetCurSide()[move] == 0) {
return false; }
145 for (
size_t i = 0; i < 6; i++) {
153 for (
size_t i = 5; i < 5; i--) {
154 os << boardB[i] <<
" ";
157 os << boardB[6] <<
" " << boardA[6] <<
std::endl;
159 for (
size_t i = 0; i < 6; i++) {
160 os << boardA[i] <<
" ";
165 void Print(std::ostream & os=std::cout) {
166 std::cout <<
"+---<<<---F-----E-----D-----C-----B-----A---<<<---+ Player B";
167 if (is_A_turn ==
false) std::cout <<
" ***";
170 <<
"| (" << std::setw(2) << boardB[5]
171 <<
" ) (" << std::setw(2) << boardB[4]
172 <<
" ) (" << std::setw(2) << boardB[3]
173 <<
" ) (" << std::setw(2) << boardB[2]
174 <<
" ) (" << std::setw(2) << boardB[1]
175 <<
" ) (" << std::setw(2) << boardB[0]
177 std::cout <<
"v [" << std::setw(2) << boardB[6]
179 << std::setw(2) << boardA[6] <<
" ] ^\n";
180 std::cout <<
"| (" << std::setw(2) << boardA[0]
181 <<
" ) (" << std::setw(2) << boardA[1]
182 <<
" ) (" << std::setw(2) << boardA[2]
183 <<
" ) (" << std::setw(2) << boardA[3]
184 <<
" ) (" << std::setw(2) << boardA[4]
185 <<
" ) (" << std::setw(2) << boardA[5]
188 <<
"+--->>>---A-----B-----C-----D-----E-----F--->>>---+ Player A";
189 if (is_A_turn ==
true) std::cout <<
" ***";
199 for (
size_t i = 0; i < 7; i++) {
207 for (
size_t i = 0; i < 7; i++) {
214 if (player == 0)
return (
double)
ScoreA();
215 else if (player == 1)
return (
double)
ScoreB();
side_t & GetCurSide()
Definition: Mancala.h:68
size_t move_t
Definition: Mancala.h:45
bool IsMoveValid(size_t move) const
Definition: Mancala.h:136
const side_t & GetOtherSide() const
Definition: Mancala.h:65
bool IsDone() const
Definition: Mancala.h:134
std::unordered_map< int, double > AsInput(size_t player_id) const
Definition: Mancala.h:71
void Reset(bool A_first=true)
Definition: Mancala.h:52
void PrintSmall(std::ostream &os=std::cout)
Definition: Mancala.h:151
void push_back(PB_Ts &&...args)
Definition: vector.h:189
emp::vector< move_t > GetMoveOptions()
Definition: Mancala.h:143
size_t ScoreB() const
Definition: Mancala.h:205
~Mancala()
Definition: Mancala.h:50
size_t GetCurPlayer() const
Definition: Mancala.h:193
const side_t & GetSideB() const
Definition: Mancala.h:63
const side_t & GetCurSide() const
Definition: Mancala.h:64
static const PrintStr endl("<br>")
Pre-define emp::endl to insert a "<br>" and thus acting like a newline.
size_t GetB(size_t i) const
Definition: Mancala.h:60
bool IsTurnB() const
Definition: Mancala.h:195
side_t & GetSideB()
Definition: Mancala.h:67
side_t & GetOtherSide()
Definition: Mancala.h:69
bool DoMove(move_t cell)
Definition: Mancala.h:83
If we are in emscripten, make sure to include the header.
Definition: array.h:37
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
size_t ScoreA() const
Definition: Mancala.h:197
void Print(std::ostream &os=std::cout)
Definition: Mancala.h:165
void SetBoard(side_t a, side_t b)
Definition: Mancala.h:129
size_t GetA(size_t i) const
Definition: Mancala.h:59
side_t & GetSideA()
Definition: Mancala.h:66
#define emp_assert(...)
Definition: assert.h:199
const side_t & GetSideA() const
Definition: Mancala.h:62
bool IsTurnA() const
Definition: Mancala.h:194
bool DoMove(size_t player, move_t cell)
Definition: Mancala.h:124
double GetScore(size_t player)
Definition: Mancala.h:213
Mancala(bool A_first=true)
Definition: Mancala.h:47