PraktikumOthello/board.cc

48 lines
1.1 KiB
C++

#include <board.h>
#include <cstdio>
void OthelloBoard::make_move(unsigned row, unsigned col,
direction dir)
{
if (!position_reachable(row, col, dir))
return;
idx_t row_idx = row; // row loop idx
idx_t col_idx = col; // col loop idx
int inc_row = 0; // step width to increment row idx
int inc_col = 0; // step width to increment col idx
setup_directions(row_idx, col_idx, inc_row, inc_col, dir);
FieldType cur = get(row_idx, col_idx);
while ((cur != _player) && (cur != EMPTY)) {
set(row_idx, col_idx, _player);
col_idx = static_cast<idx_t>((int)col_idx + inc_col);
row_idx = static_cast<idx_t>((int)row_idx + inc_row);
cur = get(row_idx, col_idx);
}
}
bool OthelloBoard::move(unsigned row, unsigned col)
{
_last_player_skipped = false;
if (!validate(row, col)) {
::printf("\033[31;1m -> Invalid move.\033[0m\n");
return false;
}
set(row, col, _player);
make_move(row, col, LEFT);
make_move(row, col, RIGHT);
make_move(row, col, UP);
make_move(row, col, DOWN);
make_move(row, col, UP_LEFT);
make_move(row, col, UP_RIGHT);
make_move(row, col, DOWN_LEFT);
make_move(row, col, DOWN_RIGHT);
return true;
}