1 /* ttt.c - client-server tic-tac-toe game
3 * Copyright © 2005 Carl Worth
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Author: Carl Worth <cworth@cworth.org>
23 #include "ttt-board.h"
25 /* Initialize an empty board. */
27 ttt_board_init (ttt_board_t *board)
30 for (i = 0; i < TTT_BOARD_MAX_CELLS; i++)
32 board->cells[i] = TTT_CELL_EMPTY;
36 /* Initialize a board from its string representation.
38 * Returns: TTT_BOARD_SUCCESS or TTT_BOARD_INVALID_BOARD if the board
39 * string could not be properly parsed.
42 ttt_board_init_from_string (ttt_board_t *board,
48 /* Return the string representation of a board.
50 * The return value is a malloc()ed string that should be free()ed
51 * when no longer needed.
53 * Errors: If out-of-memory occurs, this function will not return.
56 ttt_board_to_string (ttt_board_t *board)
63 board->cells[0], board->cells[1], board->cells[2],
64 board->cells[3], board->cells[4], board->cells[5],
65 board->cells[6], board->cells[7], board->cells[8]);
69 /* Write a string representation of a board to the provided file.
71 * Errors: If out-of-memory or a file IO error occurs, this function
75 ttt_board_write (ttt_board_t *board, FILE *file)
79 s = ttt_board_to_string (board);
81 xfwrite (s, 1, strlen (s) + 1, file);
86 /* Makes a new move in the given board.
88 * The move value is a number from 0 to 9 giving the position to be
95 * Returns: 1 if a move isa made succesfully. O if a move is invalid
96 * (the given cell is not empty).
98 * Bugs: Currently this function always places X for all moves. So,
99 * yeah, we'll need to fix that.
103 int ttt_board_make_move (ttt_board_t *board, int move)
107 if (board->cells[move] == '_')
109 board->cells[move] = 'X';