-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.cpp
200 lines (149 loc) · 5.56 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
#include "tictactoe.h"
char const TicTacToe::symbols[2] = {'X', 'O'};
TicTacToe::TicTacToe() {
int selectedSymbol;
int firstMoveTurn;
cout << "Which symbol do you want to select (1: X, 2: 0) -> ";
cin >> selectedSymbol;
cout << "Who will make the first move (Me: 1, Computer: 2) -> ";
cin >> firstMoveTurn;
this->playerSymbol = symbols[selectedSymbol - 1];
this->computerSymbol = selectedSymbol == 1 ? symbols[1] : symbols[0];
this->turn = firstMoveTurn - 1;
for (int i = 0; i < 9; i++)
this->moves[i] = ' ';
this->totalMovement = 0;
this->winner = 0;
}
char TicTacToe::getPlayerSymbol() const {
return this->playerSymbol;
}
char TicTacToe::getComputerSymbol() const {
return this->computerSymbol;
}
int TicTacToe::getTurn() const {
return this->turn;
}
int TicTacToe::getTotalMovement() const {
return this->totalMovement;
}
void TicTacToe::renderTheView() const {
cout << moves[0] << " | " << moves[1] << " | " << moves[2] << endl;
cout << "—————————" << endl;
cout << moves[3] << " | " << moves[4] << " | " << moves[5] << endl;
cout << "—————————" << endl;
cout << moves[6] << " | " << moves[7] << " | " << moves[8] << "\n" << endl;
}
bool TicTacToe::gameIsEnded() {
// Scorless
if (this->totalMovement == 9){
winner = this->WINNER_SCORLESS;
return true;
}
// Horizontal Control
for (int i = 0; i < 7; i += 3) {
if ((this->moves[i] == computerSymbol && this->moves[i + 1] == computerSymbol) && this->moves[i + 2] == computerSymbol) {
this->winner = this->WINNER_COMPUTER;
return true;
}
else if ((this->moves[i] == playerSymbol && this->moves[i + 1] == playerSymbol) && this->moves[i + 2] == playerSymbol) {
this->winner = this->WINNER_PLAYER;
return true;
}
}
// Vertical Control
for (int i = 0; i < 3; i++) {
if ((this->moves[i] == computerSymbol && this->moves[i + 3] == computerSymbol) && this->moves[i + 6] == computerSymbol) {
this->winner = this->WINNER_COMPUTER;
return true;
}
else if ((this->moves[i] == playerSymbol && this->moves[i + 3] == playerSymbol) && this->moves[i + 6] == playerSymbol) {
this->winner = this->WINNER_PLAYER;
return true;
}
}
// Cross Control
if ((this->moves[0] == computerSymbol && this->moves[4] == computerSymbol) && this->moves[8] == computerSymbol) {
this->winner = this->WINNER_COMPUTER;
return true;
}
else if ((this->moves[0] == playerSymbol && this->moves[4] == playerSymbol) && this->moves[8] == playerSymbol) {
this->winner = this->WINNER_PLAYER;
return true;
}
else if ((this->moves[2] == computerSymbol && this->moves[4] == computerSymbol) && this->moves[6] == computerSymbol){
this->winner = this->WINNER_COMPUTER;
return true;
}
else if ((this->moves[2] == playerSymbol && this->moves[4] == playerSymbol) && this->moves[6] == playerSymbol){
this->winner = this->WINNER_PLAYER;
return true;
}
return false;
}
int TicTacToe::whoIsWinner() const {
return this->winner;
}
void TicTacToe::move() {
int movement = 0;
if (this->turn == this->ROLE_PLAYER) {
while (true) {
while (movement < 1 || movement > 9) {
cout << "Please enter an index [1-9] -> ";
cin >> movement;
}
if (this->moves[movement - 1] != ' ') {
cout << "Index number " << movement << " is full!" << endl;
movement = 0;
}
else
break;
}
this->moves[movement - 1] = this->playerSymbol;
this->turn = this->ROLE_COMPUTER;
}
else {
if (this->totalMovement == 0)
movement = 0;
else
movement = this->minimax(0);
this->moves[movement] = this->computerSymbol;
this->turn = this->ROLE_PLAYER;
}
totalMovement++;
}
int TicTacToe::minimax(int height) {
// This condition was created to check if this function is a leaf.
if (this->gameIsEnded()) {
int winner = this->whoIsWinner();
return winner;
}
int returnFlag = height % 2;
int numberOfTotalIndex = sizeof(this->moves) / sizeof(char);
int scores[numberOfTotalIndex];
for (int i = 0; i < numberOfTotalIndex; i++)
scores[i] = -2;
for (int i = 0; i < numberOfTotalIndex; i++) {
if (this->moves[i] == ' ') {
if (returnFlag == this->MAX)
this->moves[i] = this->computerSymbol;
else
this->moves[i] = this->playerSymbol;
this->totalMovement += 1;
scores[i] = this->minimax(height + 1);
moves[i] = ' ';
this->totalMovement -= 1;
}
}
// These conditions were created because this function is not a leaf.
if (height == 0)
return distance(scores, max_element(scores, scores + numberOfTotalIndex)); // It gives an index of maximum value found in scores array
else if (returnFlag == this->MAX)
return *max_element(scores, scores + numberOfTotalIndex); // It gives an maximum value found in scores array
else {
if (distance(scores, find(scores, scores + numberOfTotalIndex, -1)) < numberOfTotalIndex) // If this condition is true, it means the scores array contains the value -1.
return -1;
else
return 0;
}
}