X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=dvonn-board.c;h=4d09e13be58f0ce25f2ee048bca2ea11391ee2b5;hb=1e29ece3252138d372b47499dd4c77f01413c7fc;hp=35789e7d13a238748dd3b07e5a95aed218ed0d95;hpb=c63c776c52cb865bc510b1feb9d17abe47086e5c;p=dvonn diff --git a/dvonn-board.c b/dvonn-board.c index 35789e7..4d09e13 100644 --- a/dvonn-board.c +++ b/dvonn-board.c @@ -63,6 +63,7 @@ dvonn_board_init (dvonn_board_t *board) for (y = 0; y < DVONN_BOARD_Y_SIZE; y++) { board->cells[x][y].type = DVONN_CELL_EMPTY; board->cells[x][y].height = 0; + board->cells[x][y].contains_red = FALSE; } } @@ -76,7 +77,9 @@ dvonn_board_init (dvonn_board_t *board) } } + board->phase = DVONN_PHASE_PLACEMENT; board->player = DVONN_PLAYER_WHITE; + board->moves = 0; } static dvonn_bool_t @@ -123,16 +126,63 @@ dvonn_board_next_player (dvonn_board_t *board) board->player = DVONN_PLAYER_BLACK; } +int +dvonn_board_place (dvonn_board_t *board, + int x, int y, + char **error) +{ + if (board->phase != DVONN_PHASE_PLACEMENT) { + *error = "Cannot place outside of placement phase"; + return FALSE; + } + + if (board->cells[x][y].type != DVONN_CELL_EMPTY) { + *error = "Cannot place on an occupied space"; + return FALSE; + } + + if (board->moves < 3) { + board->cells[x][y].type = DVONN_CELL_RED; + board->cells[x][y].contains_red = TRUE; + } else if (board->moves % 2) { + board->cells[x][y].type = DVONN_CELL_BLACK; + } else { + board->cells[x][y].type = DVONN_CELL_WHITE; + } + + board->cells[x][y].height = 1; + + board->moves++; + + if (board->moves == 49) { + board->phase = DVONN_PHASE_MOVEMENT; + board->moves = 0; + } + + return TRUE; +} + int dvonn_board_move (dvonn_board_t *board, int x1, int y1, int x2, int y2, char **error) { + if (board->phase != DVONN_PHASE_MOVEMENT) { + *error = "Cannot move outside of placement phase"; + return FALSE; + } + if (! dvonn_board_move_legal (board, x1, y1, x2, y2, error)) return FALSE; - /* XXX: Need to execute the move here. */ + board->cells[x2][y2].height += board->cells[x1][y1].height; + board->cells[x2][y2].type = board->cells[x1][y1].type; + board->cells[x2][y2].contains_red |= board->cells[x1][y1].contains_red; + + board->cells[x1][y1].type = DVONN_CELL_EMPTY; + board->cells[x1][y1].height = 0; + board->cells[x1][y1].contains_red = FALSE; dvonn_board_next_player (board);