X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-board.c;h=bafa0fd56fae79a0873daee57cf06152b4f93f2a;hp=27fb19ee939e82df9921bf173b949135b90c98b4;hb=81fa711bc04271a8c0a0926e6c198f91e797f59a;hpb=add37ecc4ee6485d9f91f95f1c983abf867da2c6 diff --git a/src/ttt-board.c b/src/ttt-board.c index 27fb19e..bafa0fd 100644 --- a/src/ttt-board.c +++ b/src/ttt-board.c @@ -19,21 +19,20 @@ * Author: Carl Worth */ +#include #include "ttt-board.h" - -struct _ttt_board { - int cells[TTT_BOARD_MAX_CELLS]; -}; +#include "ttt-error.h" /* Initialize an empty board. */ void ttt_board_init (ttt_board_t *board) { - int i; - for (i = 0; i < TTT_BOARD_MAX_CELLS; i++) + int i; + for (i = 0; i < TTT_BOARD_MAX_CELLS; i++) { - board->cells[i] = 0; + board->cells[i] = TTT_CELL_EMPTY; } + board->current_player = TTT_CELL_X; } /* Initialize a board from its string representation. @@ -45,9 +44,21 @@ void ttt_board_init_from_string (ttt_board_t *board, const char *s) { - /* XXX: NYI */ + int i, j; + j = 0; + for (i = 0; i < strlen (s); i++) + { + if ((s[i] == 'X') | + (s[i] == 'O') | + (s[i] == '_')) + { + board->cells[j] = s[i]; + j++; + } + } } + /* Return the string representation of a board. * * The return value is a malloc()ed string that should be free()ed @@ -58,8 +69,15 @@ ttt_board_init_from_string (ttt_board_t *board, char * ttt_board_to_string (ttt_board_t *board) { - /* XXX: NYI */ - return NULL; + char *s; + xasprintf (&s, "\r\n" + "%c|%c|%c\r\n" + "%c|%c|%c\r\n" + "%c|%c|%c", + board->cells[0], board->cells[1], board->cells[2], + board->cells[3], board->cells[4], board->cells[5], + board->cells[6], board->cells[7], board->cells[8]); + return s; } /* Write a string representation of a board to the provided file. @@ -79,3 +97,83 @@ ttt_board_write (ttt_board_t *board, FILE *file) free (s); } +/* Makes a new move in the given board. + * + * The move value is a number from 0 to 9 giving the position to be + * moved as follows: + * + * 0|1|2 + * 3|4|5 + * 6|7|8 + * + * Returns: TTT_ERROR_NONE if a move is made + * succesfully. TTT_ERROR_NOT_VALID_MOVE if a move is invalid (the + * given cell is not empty). TTT_ERROR_NOT_GRID if the move is + * impossible (user input 'a' or '9' etc.) + */ + +ttt_error_t ttt_board_make_move (ttt_board_t *board, int move) +{ + if (move < 0 || move > 8) + return TTT_ERROR_NOT_GRID; + + if (board->cells[move] != TTT_CELL_EMPTY) + return TTT_ERROR_NOT_VALID_MOVE; + + board->cells[move] = board->current_player; + + if (board->current_player == TTT_CELL_X) + board->current_player = TTT_CELL_O; + else + board->current_player = TTT_CELL_X; + return TTT_ERROR_NONE; +} + +/* Checks to see if a player has won the game and should be called + * after every move. + * + * Returns: TTT_CELL_X if X's win. TTT_CELL_O if O's + * win. TTT_CELL_EMPTY if no win. The game can then check the value + * of the return and declare the winner. + */ + +ttt_cell_t ttt_board_is_won (ttt_board_t *board) +{ + int i; + + /* Loop over rows and columns looking for a win. */ + for (i = 0; i < 3; i++) + { + /* Check the current column */ + if (board->cells[i] != TTT_CELL_EMPTY && + board->cells[i] == board->cells[i + 3] && + board->cells[i] == board->cells[i + 6]) + { + return board->cells[i]; + } + + /* Check the current row */ + if (board->cells[i * 3] != TTT_CELL_EMPTY && + board->cells[i * 3] == board->cells[(i * 3) + 1] && + board->cells[i * 3] == board->cells[(i * 3) + 2]) + { + return board->cells[i * 3]; + } + } + + if (board->cells[0] != TTT_CELL_EMPTY && + board->cells[0] == board->cells[4] && + board->cells[0] == board->cells[8]) + { + return board->cells[0]; + } + + if (board->cells[2] != TTT_CELL_EMPTY && + board->cells[2] == board->cells[4] && + board->cells[2] == board->cells[6]) + { + return board->cells[2]; + } + + return TTT_CELL_EMPTY; +}