-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic4.cpp
More file actions
288 lines (264 loc) · 6.45 KB
/
Copy pathtic4.cpp
File metadata and controls
288 lines (264 loc) · 6.45 KB
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
//---------------------------------------------------
// Purpose: Implementation of the Tic4 class
//
// Author: Patrick Karangwa
//---------------------------------------------------
#include "tic4.h"
//---------------------------------------------------
// Constructor function
//---------------------------------------------------
Tic4::Tic4()
{
ClearBoard();
}
//---------------------------------------------------
// Destructor function
//---------------------------------------------------
Tic4::~Tic4()
{
// Intentionally empty
}
//---------------------------------------------------
// Clear the Tic4 board
//---------------------------------------------------
void Tic4::ClearBoard()
{
// Initialize Tic4 board
for (int c = 0; c < SIZE; c++)
for (int r = 0; r < SIZE; r++)
board[r][c] = ' ';
board_count = 0;
}
//---------------------------------------------------
// Set value of board at location [row][column]
//---------------------------------------------------
bool Tic4::SetBoard(const int row, const int col, const char player)
{
// Error checking
if ((row < 0) || (row >= SIZE) ||
(col < 0) || (col >= SIZE) ||
(board[row][col] != ' '))
return false;
// Set value of board
board[row][col] = player;
board_count++;
return true;
}
//---------------------------------------------------
// Get value of board at location [row][column]
//---------------------------------------------------
bool Tic4::GetBoard(const int row, const int col, char & player)
{
// Error checking
if ((row < 0) || (row >= SIZE) ||
(col < 0) || (col >= SIZE))
return false;
// Get value of board
player = board[row][col];
return true;
}
//---------------------------------------------------
// Check to see if player has won the game
//---------------------------------------------------
bool Tic4::CheckWin(const char player)
{
// Check all the rows
for (int r = 0; r < SIZE; r++)
{
// Count player pieces
int count = 0;
for (int c = 0; c < SIZE; c++)
if (board[r][c] == player)
count++;
if (count == SIZE)
return true;
}
// Check all the cols
for (int c = 0; c < SIZE; c++)
{
// Count player pieces
int count = 0;
for (int r = 0; r < SIZE; r++)
if (board[r][c] == player)
count++;
if (count == SIZE)
return true;
}
// Check first diagonal
int count = 0;
for (int r = 0; r < SIZE; r++)
{
int c = r;
if (board[r][c] == player)
count++;
}
if (count == SIZE)
return true;
// Check second diagonal
count = 0;
for (int r = 0; r < SIZE; r++)
{
int c = SIZE-1-r;
if (board[r][c] == player)
count++;
}
if (count == SIZE)
return true;
// The player did not win
return false;
}
//---------------------------------------------------
// Print the Tic4 board
//---------------------------------------------------
void Tic4::PrintBoard()
{
// Draw column numbers
cout << "\n ";
for (int c = 0; c < SIZE; c++)
cout << c << " ";
cout << "\n";
// Print the Tic4 board
for (int r = 0; r < SIZE; r++)
{
// Draw dashed line
cout << " +";
for (int c = 0; c < SIZE; c++)
cout << "---+";
cout << "\n";
// Draw board contents
cout << " " << r << " | ";
for (int c = 0; c < SIZE; c++)
cout << board[r][c] << " | ";
cout << "\n";
}
// Draw dashed line
cout << " +";
for (int c = 0; c < SIZE; c++)
cout << "---+";
cout << "\n\n";
}
//---------------------------------------------------
// Check if board is full
//---------------------------------------------------
bool Tic4::IsFull()
{
return (board_count == SIZE * SIZE);
}
//---------------------------------------------------
// Simple AI method
//---------------------------------------------------
bool Tic4::SimpleAI(const char player)
{
cout << "Simple AI." << endl;
for(int r = 0; r < SIZE; r++)
{
for(int c = 0; c < SIZE; c++)
{
if(board[r][c] == ' ')
{
board[r][c] = player;
board_count++;
cout << r << " " << c << endl;
return true;
}
}
}
return false;
}
//---------------------------------------------------
// Random AI method
//---------------------------------------------------
bool Tic4::RandomAI(const char player)
{
cout << "Random AI." << endl;
for(int r = 0; r < SIZE; r++)
{
for(int c = 0; c < SIZE; c++)
{
srand(time(NULL));
r = random() % SIZE;
c = random() % SIZE;
if(board[r][c] == ' ')
{
board[r][c] = player;
board_count++;
cout << r << " " << c << endl;
return true;
}
}
}
return false;
}
//---------------------------------------------------
// Clever AI method
//---------------------------------------------------
bool Tic4::CleverAI(const char player)
{
cout << "Clever AI." << endl;
int r, c;
//Checking the first diagonal
for(r = 0; r < SIZE; r++)
{
c = r;
if(board[r][c] == ' ')
{
board[r][c] = player;
board_count++;
cout << "First Diagonal: " << endl;
cout << r << " " << c << endl;
return true;
}
}
//Checking the second diagonal
for(r = 0; r < SIZE; r++)
{
c = SIZE-1-r;
if(board[r][c] == ' ')
{
board[r][c] = player;
board_count++;
cout << "Second Diagonal: " << endl;
cout << r << " " << c << endl;
return true;
}
}
//Scanning the rows and columns
for(r = 0; r < SIZE; r++)
{
for(c =0; c < SIZE; c++)
{
if(board[r][c] == ' ')
{
board[r][c] = player;
board_count++;
cout << "Rows and columns: " << endl;
cout << r << " " << c << endl;
return true;
}
}
}
return false;
}
//---------------------------------------------------
// Fancy AI method
//---------------------------------------------------
bool Tic4::FancyAI(const char player)
{
srand(time(NULL));
if((random() % 3) == 0)
{
SimpleAI(player);
return true;
}
else if((random() % 3) == 1)
{
RandomAI(player);
return true;
}
else if((random() % 3) == 2)
{
CleverAI(player);
return true;
}
return false;
}