]> git.cworth.org Git - ttt/blob - src/ttt-board.c
Deleted debugging code and changed return errors to fit protocol
[ttt] / src / ttt-board.c
1 /* ttt.c - client-server tic-tac-toe game
2  *
3  * Copyright © 2005 Carl Worth
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  *
19  * Author: Carl Worth <cworth@cworth.org>
20  */
21
22 #include <stdio.h>
23 #include "ttt-board.h"
24
25 /* Initialize an empty board. */
26 void
27 ttt_board_init (ttt_board_t *board)
28 {
29     int i;
30     for (i = 0; i < TTT_BOARD_MAX_CELLS; i++)
31     {
32         board->cells[i] = TTT_CELL_EMPTY;
33     }
34 }
35
36 /* Initialize a board from its string representation.
37  *
38  * Returns: TTT_BOARD_SUCCESS or TTT_BOARD_INVALID_BOARD if the board
39  * string could not be properly parsed.
40  */
41 void
42 ttt_board_init_from_string (ttt_board_t *board,
43                             const char  *s)
44 {
45     /* XXX: NYI */
46 }
47
48 /* Return the string representation of a board.
49  *
50  * The return value is a malloc()ed string that should be free()ed
51  * when no longer needed.
52  *
53  * Errors: If out-of-memory occurs, this function will not return.
54  */
55 char *
56 ttt_board_to_string (ttt_board_t *board)
57 {
58     char *s;
59     xasprintf (&s, "\r\n" 
60                "%c|%c|%c\r\n"
61                "%c|%c|%c\r\n"
62                "%c|%c|%c",
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]); 
66     return s;
67 }
68
69 /* Write a string representation of a board to the provided file.
70  *
71  * Errors: If out-of-memory or a file IO error occurs, this function
72  * will not return.
73  */
74 void
75 ttt_board_write (ttt_board_t *board, FILE *file)
76 {
77     char *s;
78
79     s = ttt_board_to_string (board);
80
81     xfwrite (s, 1, strlen (s) + 1, file);
82
83     free (s);
84 }
85
86 /* Makes a new move in the given board.
87  *
88  * The move value is a number from 0 to 9 giving the position to be
89  * moved as follows:
90  * 
91  *  0|1|2
92  *  3|4|5
93  *  6|7|8
94  *
95  * Returns: 1 if a move is made succesfully. O if a move is invalid
96  * (the given cell is not empty).
97  *
98  * Bugs: Currently this function always places X for all moves. So,
99  * yeah, we'll need to fix that.
100  */
101
102  
103 ttt_error_t ttt_board_make_move (ttt_board_t *board, int move)
104 {                                    
105     if (board->cells[move] == '_')
106     {
107         board->cells[move] = 'X';
108         return(TTT_ERROR_NONE);
109     }
110     else
111     {
112         return(TTT_ERROR_NOT_VALID_MOVE);
113     }
114 }