-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.c
119 lines (110 loc) · 3.15 KB
/
game.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
/*********************************************************
* game.c
* Nine-Board Tic-Tac-Toe Game Engine
* COMP3411/9414/9814 Artificial Intelligence
* Alan Blair, CSE, UNSW
*/
#include <stdio.h>
#include "common.h"
#include "game.h"
char sb[5] = "XO.xo";
int sg[2] = {1,-1};
/*********************************************************
Reset all squares to empty
*/
void reset_board( int board[10][10] )
{
int b,c;
for( b = 0; b <= 9; b++ ) {
for( c = 1; c <= 9; c++ ) {
board[b][c] = EMPTY;
}
}
}
/*********************************************************
Print one row of the board
*/
void print_board_row(
FILE *fp,
int bd[10][10],
int a, int b, int c,
int i, int j, int k
)
{
fprintf(fp," %c %c %c |",sb[bd[a][i]],sb[bd[a][j]],sb[bd[a][k]]);
fprintf(fp," %c %c %c |",sb[bd[b][i]],sb[bd[b][j]],sb[bd[b][k]]);
fprintf(fp," %c %c %c\n",sb[bd[c][i]],sb[bd[c][j]],sb[bd[c][k]]);
fflush(fp);
}
/*********************************************************
Print the entire board
*/
void print_board(
FILE *fp,
int board[10][10],
int board_num,
int prev_move
)
{
board[board_num][prev_move] += 3; // upper case
print_board_row( fp,board,1,2,3,1,2,3 );
print_board_row( fp,board,1,2,3,4,5,6 );
print_board_row( fp,board,1,2,3,7,8,9 );
fprintf(fp," ------+-------+------\n");
print_board_row( fp,board,4,5,6,1,2,3 );
print_board_row( fp,board,4,5,6,4,5,6 );
print_board_row( fp,board,4,5,6,7,8,9 );
fprintf(fp," ------+-------+------\n");
print_board_row( fp,board,7,8,9,1,2,3 );
print_board_row( fp,board,7,8,9,4,5,6 );
print_board_row( fp,board,7,8,9,7,8,9 );
fprintf( fp, "\n" );
board[board_num][prev_move] -= 3; // lower case
}
/*********************************************************
Return TRUE if the game has been won on this sub-board
*/
int gamewon( int p, int bb[10] )
{
return( ( bb[1] == p && bb[2] == p && bb[3] == p )
||( bb[4] == p && bb[5] == p && bb[6] == p )
||( bb[7] == p && bb[8] == p && bb[9] == p )
||( bb[1] == p && bb[4] == p && bb[7] == p )
||( bb[2] == p && bb[5] == p && bb[8] == p )
||( bb[3] == p && bb[6] == p && bb[9] == p )
||( bb[1] == p && bb[5] == p && bb[9] == p )
||( bb[3] == p && bb[5] == p && bb[7] == p ));
}
/*********************************************************
Return TRUE if this sub-board is full
*/
int full_board( int bb[] )
{
int c=1;
while( c <= 9 && bb[c] != EMPTY ) {
c++;
}
return( c == 10 );
}
/*********************************************************
Make specified move on board and return game status
*/
int make_move(
int player,
int m,
int move[],
int board[10][10]
)
{
if( board[move[m-1]][move[m]] != EMPTY ) {
return( ILLEGAL_MOVE );
}
board[move[m-1]][move[m]] = player;
if( gamewon( player,board[move[m-1]] )) {
return( WIN );
}
if( full_board( board[move[m]] )) {
return( DRAW );
}
return STILL_PLAYING;
}