-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
256 lines (210 loc) · 5.68 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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
enum { NOUGHTS, CROSSES, BORDER, EMPTY };
enum { HUMANWIN, COMPWIN, DRAW };
const int Directions[4] = { 1, 5, 4, 6 };
const int ConvertTo25[9] = {
6, 7, 8,
11,12,13,
16,17,18
};
const int InMiddle = 4;
const int Corners[4] = { 0, 2, 6, 8 };
int GetNumForDir(int startSq, const int dir, const int *board, const int us) {
int found = 0;
while (board[startSq] != BORDER) {
if(board[startSq] != us) {
break;
}
found++;
startSq += dir;
}
return found;
}
int FindThreeInARow(const int *board, const int ourindex, const int us) {
int DirIndex = 0;
int Dir = 0;
int threeCount = 1;
for(DirIndex = 0; DirIndex < 4; ++DirIndex) {
Dir = Directions[DirIndex];
threeCount += GetNumForDir(ourindex + Dir, Dir, board, us);
threeCount += GetNumForDir(ourindex + Dir * -1, Dir * -1, board, us);
if(threeCount == 3) {
break;
}
threeCount = 1;
}
return threeCount;
}
void InitialiseBoard(int *board) {
int index = 0;
for(index = 0; index < 25; ++index) {
board[index] = BORDER;
}
// converts from iteration index to actual play squares 0>6,1>7,2>8 etc.
for(index = 0; index < 9; ++index) {
board[ConvertTo25[index]] = EMPTY;
}
}
int HasEmpty(const int *board) {
int index = 0;
for(index = 0; index < 9; ++index) {
if( board[ConvertTo25[index]] == EMPTY ) return 1;
}
return 0;
}
void MakeMove(int *board, const int sq, const int side) {
board[sq] = side;
}
int GetNextBest(const int *board) {
int ourMove = ConvertTo25[InMiddle];
if (board[ourMove] == EMPTY) {
return ourMove;
}
int index = 0;
ourMove = -1;
for(index = 0; index < 4; index++) {
ourMove = ConvertTo25[Corners[index]];
if (board[ourMove] == EMPTY) {
break;
}
ourMove = -1;
}
return ourMove;
}
int GetWinningMove(int *board, const int side) {
int ourMove = -1;
int index = 0;
int winFound = 0;
for(index = 0; index < 9; ++index) {
if( board[ConvertTo25[index]] == EMPTY) {
ourMove = ConvertTo25[index];
board[ourMove] = side;
if(FindThreeInARow(board, ourMove, side) == 3) {
winFound = 1;
}
board[ourMove] = EMPTY;
if(winFound == 1) {
break;
}
ourMove = -1;
}
}
return ourMove;
}
int GetComputerMove(int *board, const int side) {
int index = 0;
int numFree = 0;
int availableMoves[9];
int randMove = 0;
randMove = GetWinningMove(board, side);
if(randMove != -1) {
return randMove;
}
randMove = GetWinningMove(board, side ^ 1);
if(randMove != -1) {
return randMove;
}
randMove = GetNextBest(board);
if(randMove != -1) {
return randMove;
}
randMove = 0;
for(index = 0; index < 9; ++index) {
if( board[ConvertTo25[index]] == EMPTY) {
availableMoves[numFree++] = ConvertTo25[index];
}
}
randMove = (rand() % numFree);
return availableMoves[randMove];
}
int GetHumanMove(const int *board) {
char userInput[4];
int moveOk = 0;
int move = -1;
while(moveOk == 0) {
printf("Please enter a move from 1 to 9:");
fgets(userInput, 3, stdin);
fflush(stdin);
if(strlen(userInput) != 2) {
printf("Invalid strlen()\n");
continue;
}
if ( sscanf(userInput, "%d", &move) != 1 ) {
move = -1;
printf("Invalid sscanf()\n");
continue;
}
if ( move < 1 || move > 9 ) {
move = -1;
printf("Invalid range\n");
continue;
}
move--; // zero indexing
if ( board[ConvertTo25[move]] != EMPTY ) {
move=-1;
printf("Square not available\n");
continue;
}
moveOk = 1;
}
printf("Making move...%d\n", (move+1));
return ConvertTo25[move];
}
void PrintBoard(const int *board) {
int index = 0;
char pceChars[] = "OX|-";
printf("\n\nBoard:\n\n");
for(index = 0; index < 9; ++index) {
if (index!=0 && index%3==0) {
printf("\n\n");
}
printf("%4c",pceChars[board[ConvertTo25[index]]]);
}
printf("\n");
}
void RunGame() {
int GameOver = 0;
int Side = NOUGHTS;
int LastMoveMade = 0;
int board[25];
InitialiseBoard(&board[0]);
PrintBoard(&board[0]);
while(!GameOver) {
if(Side==NOUGHTS) {
/// get move from human, make move on board, change side
LastMoveMade = GetHumanMove(&board[0]);
MakeMove(&board[0], LastMoveMade, Side);
Side=CROSSES;
} else {
LastMoveMade = GetComputerMove(&board[0], Side);
MakeMove(&board[0], LastMoveMade, Side);
Side=NOUGHTS;
PrintBoard(&board[0]);
}
// if three in a row exists, Game is over
if( FindThreeInARow(board, LastMoveMade, Side ^ 1) == 3) {
printf("Game over!\n");
GameOver = 1;
if(Side==NOUGHTS){
printf("Computer wins\n");
} else {
printf("Human wins\n");
}
}
// if no more moves, game is drawn
if(!HasEmpty(board)) {
printf("Game over!\n");
GameOver = 1;
printf("It's a draw\n");
}
}
PrintBoard(&board[0]);
}
int main() {
srand(time(NULL));
RunGame();
return 0;
}