-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTT.cpp
221 lines (182 loc) · 5.76 KB
/
TTT.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
#include "TTT.h"
int main(){
bool playAgain = true;
while(playAgain){
vector<vector<string>> board;
board.resize(3);
string empSym = "e";
for (int i = 0; i < 3; i++){
board[i].resize(3);
for (int j = 0; j < 3; j++){
board[i][j] = empSym;
}
}
string player1Sym = "X";
string player2Sym = "O";
bool isPlayer1Turn = true;
bool winning = false;
string currSym = player1Sym;
int x, y;
//board[row][col]
while(true){
printBoard(board);
cout << endl;
if(isPlayer1Turn){
cout << "Enter your coordinates" << endl;
cin >> x >> y;
cout << endl;
while(!isValidSquare(x, y, empSym, board)){
cout << "Invalid input." << endl;
cout << "Enter your coordinates" << endl;
cin >> x >> y;
cout << endl;
}
}
else{
tuple<int, int, int> temp = minimax(true, 0, "O", empSym, board);
x = get<1>(temp);
y = get<2>(temp);
}
board[x][y] = currSym;
if(hasWon(currSym, board)){
winning = true;
break;
}
if(boardIsFull(empSym, board)){
break;
}
isPlayer1Turn = !isPlayer1Turn;
if(isPlayer1Turn){
currSym = player1Sym;
}
else{
currSym = player2Sym;
}
}
printBoard(board);
cout << endl;
if(winning){
if(isPlayer1Turn){
cout << ("Player 1 has won!") << endl;
}
else{
cout << ("Player 2 has won!") << endl;
}
}
else{
cout << ("You've tied!") << endl;
}
string yesNo;
cout << "Would you like to play again? y/n" << endl;
cin >> yesNo;
if(yesNo == "n"){
playAgain = false;
}
}
};
tuple <int, int, int> minimax(bool isMaxing, int depth, string symbol, string emptySym, vector<vector<string>> &board){
tuple <int, int, int> bestSquare;
if(isMaxing){
int maxScore = INT_MIN;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if(board[i][j] == emptySym){
board[i][j] = symbol;
if(hasWon(symbol, board)){
board[i][j] = emptySym;
return tuple <int, int, int> (10 - depth, i, j);
}
if(boardIsFull(emptySym, board)){
board[i][j] = emptySym;
return tuple <int, int, int> (0, i, j);
}
tuple<int, int, int> temp = minimax(!isMaxing, depth + 1, "X", emptySym, board);
board[i][j] = emptySym;
if(get<0>(temp) > maxScore){
get<1>(bestSquare) = i;
get<2>(bestSquare) = j;
maxScore = get<0>(temp);
}
}
}
}
get<0>(bestSquare) = maxScore;
}
else{
int minScore = INT_MAX;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if(board[i][j] == emptySym){
board[i][j] = symbol;
if(hasWon(symbol, board)){
board[i][j] = emptySym;
return tuple <int, int, int> (-10 + depth, i, j);
}
if(boardIsFull(emptySym, board)){
board[i][j] = emptySym;
return tuple <int, int, int> (0, i, j);
}
tuple<int, int, int> temp = minimax(!isMaxing, depth + 1, "O", emptySym, board);
board[i][j] = emptySym;
if(get<0>(temp) < minScore){
get<1>(bestSquare) = i;
get<2>(bestSquare) = j;
minScore = get<0>(temp);
}
}
}
}
get<0>(bestSquare) = minScore;
}
return bestSquare;
}
bool hasWon(string sym, vector<vector<string>> &board){
for(int i = 0; i < 3; i++){
//Check if cols are filled
if(board[i][0] == sym && board[i][1] == sym && board[i][2] == sym){
return true;
}
//Check if rows are filled
if(board[0][i] == sym && board[1][i] == sym && board[2][i] == sym){
return true;
}
}
//Check down diagonal
if(board[0][0] == sym && board[1][1] == sym && board[2][2] == sym){
return true;
}
//Check up diagonal
if(board[0][2] == sym && board[1][1] == sym && board[2][0] == sym){
return true;
}
return false;
}
bool boardIsFull(string emptySym,vector<vector<string>> &board){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if(board[i][j] == emptySym){
return false;
}
}
}
return true;
}
bool isValidSquare(int row, int col, string emptySym, vector<vector<string>> &board){
//In bounds
if(row < 0 || col < 0 || row >= 3 || col >= 3){
return false;
}
//Not already filled
if(board[row][col] != emptySym){
return false;
}
return true;
}
void printBoard(vector<vector<string>> &board){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
cout << board[i][j] << " ";
}
cout << endl;
}
}