Debian Codegraph @

src/board.c

raw
1 /* netToe Version 1.5.1
2 *
3 * Copyright 2013, 2014 Mats Erik Andersson <[email protected]>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdlib.h>
26 #ifdef HAVE_STRING_H
27 # include <string.h>
28 #endif
29 #ifdef HAVE_STRINGS_H
30 # include <strings.h> /* strncasecmp */
31 #endif
32
33 #define NO_EXTERNALS
34 #include "board.h"
35
36 char c11, c12, c13;
37 char c21, c22, c23;
38 char c31, c32, c33;
39
40 char *C11, *C12, *C13;
41 char *C21, *C22, *C23;
42 char *C31, *C32, *C33;
43
44 /* Board vector for unified calculations. */
45 char * board[9];
46
47 /* The eight possible winning lines expressed as indices.
48 *
49 * These give the first and last positions, the middle
50 * position is directly retreived as the mean of those.
51 *
52 * A graphical translation is easy:
53 *
54 * 0 | 1 | 2
55 * ---------
56 * 3 | 4 | 5 <====> board[ 0 1 2 3 4 5 6 7 8 ]
57 * ---------
58 * 6 | 7 | 8
59 *
60 * */
61 struct endpoints winning_line[8] =
62 {
63 { 0, 2 }, { 3, 5 }, { 6, 8 }, /* horizontals */
64 { 0, 6 }, { 1, 7 }, { 2, 8 }, /* verticals */
65 { 0, 8 }, { 2, 6 } /* diagonals */
66 };
67
68 static void switch_char_pointers(char ** a, char ** b) {
69 char * tmp = *a;
70
71 *a = *b;
72 *b = tmp;
73 }
74
75 /* Transform board position to array index.
76 * Here 1 <= row,col <= 3.
77 */
78 static int pos_to_ind(char row, char col)
79 {
80 return 3 * (row - 1) + (col - 1);
81 }
82
83 void init_matrix (void)
84 {
85 c11 = ' ', c12 = ' ', c13 = ' ';
86 c21 = ' ', c22 = ' ', c23 = ' ';
87 c31 = ' ', c32 = ' ', c33 = ' ';
88 C11 = &c11, C12 = &c12, C13 = &c13;
89 C21 = &c21, C22 = &c22, C23 = &c23;
90 C31 = &c31, C32 = &c32, C33 = &c33;
91
92 /* Randomize internal board view.
93 * An initial srandom(time(0)) was applied
94 * in misc.c.
95 */
96 if ( random() % 2 ) {
97 /* Vertical mirroring. */
98 switch_char_pointers(&C11, &C31);
99 switch_char_pointers(&C12, &C32);
100 switch_char_pointers(&C13, &C33);
101 }
102 if ( random() % 2 ) {
103 /* Horizontal mirroring. */
104 switch_char_pointers(&C11, &C13);
105 switch_char_pointers(&C21, &C23);
106 switch_char_pointers(&C31, &C33);
107 }
108 if ( random() % 2 ) {
109 /* Diagonal mirroring. */
110 switch_char_pointers(&C11, &C33);
111 switch_char_pointers(&C12, &C23);
112 switch_char_pointers(&C21, &C32);
113 }
114
115 /* Set up the board array. Used in AI-mode 3. */
116 board[pos_to_ind(1,1)] = C11;
117 board[pos_to_ind(1,2)] = C12;
118 board[pos_to_ind(1,3)] = C13;
119 board[pos_to_ind(2,1)] = C21;
120 board[pos_to_ind(2,2)] = C22;
121 board[pos_to_ind(2,3)] = C23;
122 board[pos_to_ind(3,1)] = C31;
123 board[pos_to_ind(3,2)] = C32;
124 board[pos_to_ind(3,3)] = C33;
125 }
126
127 int game_check (void)
128 {
129 int n;
130
131 for (n = 0; n < 8; n++)
132 {
133 struct endpoints line = winning_line[n];
134
135 if ( *board[line.first] == *board[line.last]
136 && *board[line.first] == *board[(line.first + line.last) / 2] )
137 {
138 /* Disregard empty lines. */
139 if (*board[line.first] != 'X' && *board[line.first] != 'O')
140 continue;
141
142 /* Identical pawns in a winning line! */
143 return ((*board[line.first] == 'X') ? GAME_IS_X_WIN
144 : GAME_IS_O_WIN);
145 }
146 }
147
148 /* Is the board exhausted? */
149 if ( (c11 != ' ') && (c12 != ' ') && (c13 != ' ')
150 && (c21 != ' ') && (c22 != ' ') && (c23 != ' ')
151 && (c31 != ' ') && (c32 != ' ') & (c33 != ' ') )
152 {
153 return GAME_IS_DRAW;
154 }
155
156 return GAME_IS_ALIVE;
157 }
158
159 struct good_move {
160 char * string;
161 char * position;
162 } good_moves[] = {
163 { "a1", &c11 },
164 { "a2", &c12 },
165 { "a3", &c13 },
166 { "b1", &c21 },
167 { "b2", &c22 },
168 { "b3", &c23 },
169 { "c1", &c31 },
170 { "c2", &c32 },
171 { "c3", &c33 },
172 #ifndef HAVE_STRNCASECMP
173 { "A1", &c11 },
174 { "A2", &c12 },
175 { "A3", &c13 },
176 { "B1", &c21 },
177 { "B2", &c22 },
178 { "B3", &c23 },
179 { "C1", &c31 },
180 { "C2", &c32 },
181 { "C3", &c33 },
182 #endif /* !HAVE_STRNCASECMP */
183 { NULL, NULL },
184 };
185
186 int attempt_move (const char *move, char pawn)
187 {
188 struct good_move *p;
189
190 for (p = good_moves; p->string && p->position; p++)
191 {
192 #ifdef HAVE_STRNCASECMP
193 if (!strncasecmp (move, p->string, strlen (p->string)))
194 #else /* !HAVE_STRNCASECMP */
195 if (!strncmp (move, p->string, strlen (p->string)))
196 #endif
197 {
198 if (*p->position == ' ')
199 {
200 *p->position = pawn;
201 return VALID_MOVE;
202 }
203 }
204 }
205
206 return INVALID_MOVE;
207 }