-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtic_tac_toe_ai.cpp
210 lines (179 loc) · 5.75 KB
/
tic_tac_toe_ai.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
#include <iostream>
#include <vector>
using namespace std;
class TicTacToe {
private:
static const int SIZE = 3;
static const char EMPTY = ' ';
static const char HUMAN = 'X';
static const char COMPUTER = 'O';
vector<vector<char>> board;
bool isHumanTurn;
bool isGameStarted;
public:
TicTacToe() {
board = vector<vector<char>>(SIZE, vector<char>(SIZE, ' '));
isHumanTurn = true;
isGameStarted = false;
initializeBoard();
}
void initializeBoard() {
char cellValue = '1';
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = cellValue++;
}
}
}
void playGame() {
cout << "Welcome to Tic-Tac-Toe!" << endl;
while (!isGameOver()) {
printBoard();
if (isHumanTurn) {
makeHumanMove();
} else {
makeComputerMove();
}
isHumanTurn = !isHumanTurn;
isGameStarted = true;
}
printBoard();
printResult();
}
bool isGameOver() {
return isBoardFull() || hasPlayerWon(HUMAN) || hasPlayerWon(COMPUTER);
}
bool isBoardFull() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (isdigit(board[i][j])) {
return false;
}
}
}
return true;
}
bool hasPlayerWon(char player) {
for (int i = 0; i < SIZE; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
}
for (int j = 0; j < SIZE; j++) {
if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
return true;
}
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
void printBoard() {
cout << "-------------" << endl;
for (int i = 0; i < SIZE; i++) {
cout << "| ";
for (int j = 0; j < SIZE; j++) {
if (isHumanTurn && board[i][j] != HUMAN && board[i][j] != COMPUTER) {
cout << (isGameStarted ? EMPTY : board[i][j]) << " | ";
} else {
cout << board[i][j] << " | ";
}
}
cout << endl << "-------------" << endl;
}
}
void makeHumanMove() {
int position;
do {
cout << "Enter position (1-9): ";
cin >> position;
} while (!isValidMove(position));
int row = (position - 1) / SIZE;
int col = (position - 1) % SIZE;
board[row][col] = HUMAN;
}
bool isValidMove(int position) {
int row = (position - 1) / SIZE;
int col = (position - 1) % SIZE;
if (position < 1 || position > SIZE * SIZE) {
cout << "Invalid move! Try again." << endl;
return false;
}
if (!isdigit(board[row][col])) {
cout << "Cell already occupied! Try again." << endl;
return false;
}
return true;
}
void makeComputerMove() {
vector<int> bestMove = minimax(0, COMPUTER);
int position = bestMove[0] * SIZE + bestMove[1] + 1;
board[bestMove[0]][bestMove[1]] = COMPUTER;
cout << "Computer chose position: " << position << endl;
}
vector<int> minimax(int depth, char player) {
vector<int> bestMove = { -1, -1, 0 };
int bestScore;
if (player == COMPUTER) {
bestScore = -10;
} else {
bestScore = 10;
}
if (isGameOver()) {
int score = evaluateBoard();
return { -1, -1, score };
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (isdigit(board[i][j])) {
board[i][j] = player;
vector<int> currentMove = minimax(depth + 1, (player == COMPUTER) ? HUMAN : COMPUTER);
int currentScore = currentMove[2];
board[i][j] = board[i][j] = ('0' + i * SIZE + j + 1);
if (player == COMPUTER) {
if (currentScore > bestScore) {
bestScore = currentScore;
bestMove[0] = i;
bestMove[1] = j;
}
} else {
if (currentScore < bestScore) {
bestScore = currentScore;
bestMove[0] = i;
bestMove[1] = j;
}
}
}
}
}
bestMove[2] = bestScore;
return bestMove;
}
int evaluateBoard() {
if (hasPlayerWon(COMPUTER)) {
return 1;
} else if (hasPlayerWon(HUMAN)) {
return -1;
} else {
return 0;
}
}
void printResult() {
if (hasPlayerWon(HUMAN)) {
cout << "Congratulations! You won!" << endl;
} else if (hasPlayerWon(COMPUTER)) {
cout << "Computer wins!" << endl;
} else {
cout << "It's a draw!" << endl;
}
}
};
int main() {
TicTacToe game;
game.playGame();
return 0;
}