-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect4.c
296 lines (270 loc) · 6.53 KB
/
connect4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Connect 4: a simple text based implementation */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* number of columns in the game */
#define WIDTH 7
/* number of slots in each column */
#define HEIGHT 6
/* number in row required for victory */
#define STRAIGHT 4
/* sign that a cell is still empty */
#define EMPTY ' '
/* the two colours used in the game */
#define RED 'R'
#define YELLOW 'Y'
/* horizontal size of each cell in the display grid */
#define WGRID 5
/* vertical size of each cell in the display grid */
#define HGRID 3
#define RSEED 876545678
typedef char c4_t[HEIGHT][WIDTH];
void print_config(c4_t);
void init_empty(c4_t);
int do_move(c4_t, int, char);
void undo_move(c4_t, int);
int get_move(c4_t);
int move_possible(c4_t);
char winner_found(c4_t);
int rowformed(c4_t, int r, int c);
int explore(c4_t, int r_fix, int c_fix, int r_off, int c_off);
int suggest_move(c4_t board, char colour);
/* Initialise the playing array to empty cells */
void
init_empty(c4_t board) {
int r, c;
for (r=HEIGHT-1; r>=0; r--) {
for (c=0; c<WIDTH; c++) {
board[r][c] = EMPTY;
}
}
printf("Welcome to connect-4 \n\n");
}
/* Apply the specified move to the board
*/
int
do_move(c4_t board, int c, char colour) {
int r=0;
/* first, find the next empty slot in that column */
while ((r<HEIGHT) && (board[r][c-1]!=EMPTY)) {
r += 1;
}
if (r==HEIGHT) {
/* no move is possible */
return 0;
}
/* otherwise, do the assignment */
board[r][c-1] = colour;
return 1;
}
/* Remove the top token from the specified column c
*/
void
undo_move(c4_t board, int c) {
int r=0;
/* first, find the next empty slot in that column, but be
* careful not to run over the top of the array
*/
while ((r<HEIGHT) && board[r][c-1] != EMPTY) {
r += 1;
}
/* then do the assignment, assuming that r>=1 */
board[r-1][c-1] = EMPTY;
return;
}
/* Check board to see if it is full or not */
int
move_possible(c4_t board) {
int c;
/* check that a move is possible */
for (c=0; c<WIDTH; c++) {
if (board[HEIGHT-1][c] == EMPTY) {
/* this move is possible */
return 1;
}
}
/* if here and loop is finished, and no move possible */
return 0;
}
/* Read the next column number, and check for legality
*/
int
get_move(c4_t board) {
int c;
/* check that a move is possible */
if (!move_possible(board)) {
return EOF;
}
/* one is, so ask for user input */
printf("Enter column number: ");
if (scanf("%d", &c) != 1) {
return EOF;
}
/* and keep asking until a valid move is entered */
while ((c<=0) || (c>WIDTH) || (board[HEIGHT-1][c-1]!=EMPTY)) {
printf("That move is not possible. ");
printf("Enter column number: ");
if (scanf("%d", &c) != 1) {
return EOF;
}
}
/* now have a valid move */
return c;
}
/* Print out the current configuration of the board
*/
void
print_config(c4_t board) {
int r, c, i, j;
/* lots of complicated detail in here, mostly this function
* is an exercise in attending to detail and working out the
* exact layout that is required.
*/
printf("\n");
/* print cells starting from the top, each cell is spread over
* several rows
*/
for (r=HEIGHT-1; r>=0; r--) {
for (i=0; i<HGRID; i++) {
printf("\t|");
/* next two loops step across one row */
for (c=0; c<WIDTH; c++) {
for (j=0; j<WGRID; j++) {
printf("%c", board[r][c]);
}
printf("|");
}
printf("\n");
}
}
/* now print the bottom line */
printf("\t+");
for (c=0; c<WIDTH; c++) {
for (j=0; j<WGRID; j++) {
printf("-");
}
printf("+");
}
printf("\n");
/* and the bottom legend */
printf("\t ");
for (c=0; c<WIDTH; c++) {
for (j=0; j<(WGRID-1)/2; j++) {
printf(" ");
}
printf("%1d ", c+1);
for (j=0; j<WGRID-1-(WGRID-1)/2; j++) {
printf(" ");
}
}
printf("\n\n");
}
/* Is there a winning position on the current board?
*/
char
winner_found(c4_t board) {
int r, c;
/* check exhaustively from every position on the board
* to see if there is a winner starting at that position.
* could probably short-circuit some of the computatioin,
* but hey, the computer has plenty of time to do the tesing
*/
for (r=0; r<HEIGHT; r++) {
for (c=0; c<WIDTH; c++) {
if ((board[r][c]!=EMPTY) && rowformed(board,r,c)) {
return board[r][c];
}
}
}
/* ok, went right through all positions and if we are still
* here then there isn't a solution to be found
*/
return EMPTY;
}
/* Is there a row in any direction starting at [r][c]?
*/
int
rowformed(c4_t board, int r, int c) {
return
explore(board, r, c, +1, 0) ||
explore(board, r, c, -1, 0) ||
explore(board, r, c, 0, +1) ||
explore(board, r, c, 0, -1) ||
explore(board, r, c, -1, -1) ||
explore(board, r, c, -1, +1) ||
explore(board, r, c, +1, -1) ||
explore(board, r, c, +1, +1);
}
/* Nitty-gritty detail of looking for a set of straight-line
* items all the same colour. Need to be very careful not to step
* over the edge of the array
*/
int
explore(c4_t board, int r_fix, int c_fix, int r_off, int c_off) {
int r_lim, c_lim;
int r, c, i;
r_lim = r_fix + (STRAIGHT-1)*r_off;
c_lim = c_fix + (STRAIGHT-1)*c_off;
/* can we go in the specified direction?
*/
if (r_lim<0 || r_lim>=HEIGHT || c_lim<0 || c_lim>=WIDTH) {
/* no, not enough space */
return 0;
}
/* can, so check the colours for all the same */
for (i=1; i<STRAIGHT; i++) {
r = r_fix + i*r_off;
c = c_fix + i*c_off;
if (board[r][c] != board[r_fix][c_fix]) {
/* found one different, so cannotbe a row */
return 0;
}
}
/* by now, a straight row all the same colour has been found */
return 1;
}
/* Try to find a good move for the specified colour
*/
int
suggest_move(c4_t board, char colour) {
int c;
/* look for a winning move for colour */
for (c=0; c<WIDTH; c++) {
/* temporarily move in column c... */
if (do_move(board, c+1, colour)) {
/* ... and check to see the outcome */
if (winner_found(board) == colour) {
/* it is good, so unassign and return c */
undo_move(board, c+1);
return c+1;
} else {
undo_move(board, c+1);
}
}
}
/* ok, no winning move, look for a blocking move */
if (colour == RED) {
colour = YELLOW;
} else {
colour = RED;
}
for (c=0; c<WIDTH; c++) {
/* temporarily move in column c... */
if (do_move(board, c+1, colour)) {
/* ... and check to see the outcome */
if (winner_found(board) == colour) {
/* it is good, so unassign and return c */
undo_move(board, c+1);
return c+1;
} else {
undo_move(board, c+1);
}
}
}
/* no moves found? then pick at random... */
c = rand()%WIDTH;
while (board[HEIGHT-1][c]!=EMPTY) {
c = rand()%WIDTH;
}
return c+1;
}