-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.cpp
232 lines (201 loc) · 6.14 KB
/
tictactoe.cpp
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
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <ctime>
#include "player.h"
#include "exPlayer.h"
#include "ohPlayer.h"
using namespace std;
// global variables
int turn; // whose turn is it?
int moves; // how many moves have already taken place
square **table; // the current board
const int boardSize=10; // the size of the square board (fixed at 10x10)
const int moveTime=2; // number of seconds between moves
const int setupTime=5; // number of seconds for setup
// auxilliary functions
inline int min(int a, int b) {
if (a<b) return a; else return b;
}
inline int max(int a, int b) {
if (a<b) return b; else return a;
}
bool wonQ() {
// %E: Teturns true iff there are five X's or five O's in a row.
int ii,jj;
// check horizontal
int streak=1;
for (ii=0; ii<boardSize; ii++,streak=1)
for (jj=1; jj<boardSize; jj++)
if ((table[ii][jj]!=blank) && (table[ii][jj]==table[ii][jj-1]))
{if (++streak==5) // we have a winner
return true;}
else
streak=1;
//check vertical
streak=1;
for (jj=0; jj<boardSize && streak<5; jj++,streak=1)
for (ii=1; ii<boardSize && streak<5; ii++)
if ((table[ii][jj]!=blank) && (table[ii][jj]==table[ii-1][jj]))
{if (++streak==5) // we have a winner
return true;}
else
streak=1;
//check diagonal
streak=1;
int diff;
for (diff=-boardSize+5; diff<=boardSize-5 && streak<5; diff++, streak=1)
for (jj=max(1-diff,1); jj<min(boardSize-diff,boardSize) && streak<5; jj++)
if ((table[jj][jj+diff]!=blank) && (table[jj][jj+diff]==table[jj-1][jj+diff-1]))
{if (++streak==5) // we have a winner
return true;}
else
streak=1;
//check other diagonal
streak=1;
int sum;
for (sum=4; (sum<2*boardSize-5) && streak<5; sum++, streak=1)
for (jj=max(1,sum-boardSize+2); jj<min(sum+1,boardSize) && streak<5; jj++)
if ((table[jj][sum-jj]!=blank) && (table[jj][sum-jj]==table[jj-1][sum-jj+1]))
{if (++streak==5) // we have a winner
return true;}
else
streak=1;
return false; // i.e. no winner
}
void redrawBoard() {
// %E: Draws the current board position to standard output
int ii,jj;
cout << "Move " << moves << ":" << endl;
for (jj=0; jj<boardSize; jj++) {
for (ii=0; ii<boardSize; ii++)
switch(table[ii][jj]) {
case blank: cout << "-"; break;
case oh: cout << "O"; break;
case ex: cout << "X"; break;
case unusable: cout << "#"; break;
}
cout << endl;
}
cout << endl;
}
inline bool checkBounds(int xx, int yy) {
// %E: Checks that a position is within the board boundaries
if ((xx<0 || xx>boardSize-1) || (yy<0 || yy>boardSize-1))
return false;
return true;
}
void Sig(int i) {
// %E: Catches signals - if one side crashes or runs out of time, the other side wins.
cout << "RUNTIME ERROR - signal " << i << endl;
if (turn == ex) // give the game to whoever was still playing
cout << "Player O wins" << endl;
else
cout << "Player X wins" << endl;
exit(-1);
}
void TimeOver(int i) {
cout << "TIME OUT!" << endl;
if (turn == ex) // give the game to whoever was still playing
cout << "Player O wins" << endl;
else
cout << "Player X wins" << endl;
exit(-1);
}
void getRandomEmpty(int& xx, int &yy) {
// %R: At least one empty square exists on the board
// %E: Finds a random empty square on the board, and puts its coordinates into xx and yy
do {
xx = rand()%boardSize;
yy = rand()%boardSize;
}
while (table[xx][yy]!=blank);
}
int main(int argc, char* argv[]) {
int ii,jj;
// 0. PRE- GAME INITIALIZATIONS
// Set up random number generator
srand(time(NULL));
// Catch all signals (out of time, program crashes, etc.)
signal(SIGALRM,&TimeOver);
signal(SIGABRT,&Sig);
signal(SIGBUS,&Sig);
signal(SIGHUP,&Sig);
signal(SIGILL,&Sig);
signal(SIGINT,&Sig);
signal(SIGKILL,&Sig);
signal(SIGSEGV,&Sig);
signal(SIGSYS,&Sig);
// Initialize table
table = new square*[boardSize];
for (int ii=0; ii<boardSize; ii++) table[ii]=new square[boardSize];
for (ii=0; ii<boardSize; ii++)
for (jj=0; jj<boardSize; jj++)
table[ii][jj]=blank;
// Add up to 4 random X's, O's, and unusables to the board
int randNum=rand()%4; // the number of extra entries
for (ii=0; ii<randNum; ii++) {
int xx,yy;
getRandomEmpty(xx,yy); table[xx][yy]=ex;
getRandomEmpty(xx,yy); table[xx][yy]=oh;
getRandomEmpty(xx,yy); table[xx][yy]=unusable;
}
const int MAXMOVES = boardSize*boardSize - 3*randNum; // i.e. how many free squares are left
// Initialize players
player *A,*B;
// ... set up X
turn = ex;
alarm(setupTime);
A = new exPlayer(table);
alarm(0); // i.e. cancel alarm
// ... set up O
turn = oh;
alarm(setupTime);
B = new ohPlayer(table);
alarm(0); // i.e. cancel alarm
// 1. THE GAME STARTS
turn = ex; // first player to move
redrawBoard(); // display the empty board
do {
int xx=0, yy=0;
moves++;
if (turn==ex) {
// A's move
boardSquare A_move;
int xx,yy;
alarm(moveTime); // allow 2 seconds per move
A_move = A->nextMove(); xx=A_move.xx; yy=A_move.yy;
alarm(0); // cancel alarm
B->otherMove(A_move); // record the move for B
if ((table[xx][yy]==blank) && checkBounds(xx,yy))
table[xx][yy]=ex;
else
{ cout << "Invalid move by A: " << xx << ", " << yy << endl;
exit(-1);}
}
else {
// B's move
boardSquare B_move;
int xx,yy;
alarm(moveTime);
B_move=B->nextMove(); xx=B_move.xx; yy=B_move.yy;
alarm(0);
A->otherMove(B_move); // record the move for A
if ((table[xx][yy]==blank) && checkBounds(xx,yy))
table[xx][yy]=oh;
else
{ cout << "Invalid move by B: " << xx << ", " << yy << endl;
exit(-1);}
}
redrawBoard();
turn=(turn==ex?oh:ex); // change moves
} while (!wonQ() && moves<MAXMOVES);
// 3. RECORD THE WINNER
if (moves==MAXMOVES)
cout << "DRAW!!!!!!!!" << endl;
else
if (turn == ex) // recall that turn was changed in the last iteration
cout << "Player O wins" << endl;
else
cout << "Player X wins" << endl;
}