-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
178 lines (156 loc) · 3.75 KB
/
game.cpp
File metadata and controls
178 lines (156 loc) · 3.75 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
#include <iostream>
#include <sstream>
#include <iomanip>
#include "game.h"
using namespace std;
Game::Game() {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
void Game::printBoard() {
cout << "-------------------";
for(int i = 0; i < 3; i++) {
cout << '\n' << "|";
for(int j = 0; j < 3; j++) {
cout << setw(3) << board[i][j] << setw(3) << " |";
}
}
cout << '\n' << "-------------------" << '\n';
}
bool Game::gameOver() {
if(checkWin(HUMAN)) return true;
else if(checkWin(AI)) return true;
bool emptySpace = false;
for(int i = 0; i < 3; i++) {
if(board[i][0] == '-' || board[i][1] == '-' || board[i][2] == '-')
emptySpace = true;
}
return !emptySpace;
}
bool Game::checkWin(Player player) {
char playerChar;
if(player == HUMAN) playerChar = human;
else playerChar = ai;
for(int i = 0; i < 3; i++) {
// Check horizontals
if(board[i][0] == playerChar && board[i][1] == playerChar
&& board[i][2] == playerChar)
return true;
// Check verticals
if(board[0][i] == playerChar && board[1][i] == playerChar
&& board[2][i] == playerChar)
return true;
}
// Check diagonals
if (board[0][0] == playerChar && board[1][1] == playerChar
&& board[2][2] == playerChar) {
return true;
} else if (board[0][2] == playerChar && board[1][1] == playerChar
&& board[2][0] == playerChar) {
return true;
}
return false;
}
int Game::score() {
if(checkWin(HUMAN)) { return 10; }
else if(checkWin(AI)) { return -10; }
return 0; // draw
}
Move Game::minimax(char AIboard[3][3]) {
int bestMoveScore = 100; // -100 is arbitrary
Move bestMove;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(AIboard[i][j] == '-') {
AIboard[i][j] = ai;
int tempMoveScore = maxSearch(AIboard);
if(tempMoveScore <= bestMoveScore) {
bestMoveScore = tempMoveScore;
bestMove.x = i;
bestMove.y = j;
}
AIboard[i][j] = '-';
}
}
}
return bestMove;
}
int Game::maxSearch(char AIboard[3][3]) {
if(gameOver()) return score();
Move bestMove;
int bestMoveScore = -1000;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(AIboard[i][j] == '-') {
AIboard[i][j] = human;
int tempMoveScore = minSearch(AIboard);
if(tempMoveScore >= bestMoveScore) {
bestMoveScore = tempMoveScore;
bestMove.x = i;
bestMove.y = j;
}
AIboard[i][j] = '-';
}
}
}
return bestMoveScore;
}
int Game::minSearch(char AIboard[3][3]) {
if(gameOver()) return score();
Move bestMove;
int bestMoveScore = 1000;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(AIboard[i][j] == '-') {
AIboard[i][j] = ai;
int tempMove = maxSearch(AIboard);
if(tempMove <= bestMoveScore) {
bestMoveScore = tempMove;
bestMove.x = i;
bestMove.y = j;
}
AIboard[i][j] = '-';
}
}
}
return bestMoveScore;
}
void Game::getHumanMove() {
int x, y = -1; // arbitrary assignment to init loop
while(x < 0 || x > 2 || y < 0 || y > 2) {
// Loop until a valid move is entered
cout << "Enter your move in coordinate form, ex: (1,3)." << endl;
cout << "Your Move: ";
char c;
string restofline;
cin >> c >> c;
x = c - '0' - 1;
cin >> c >> c;
y = c - '0' - 1;
getline(cin, restofline); // get garbage chars after move
}
board[x][y] = human;
}
void Game::play_stand_alone() {
int turn = 0;
printBoard();
while(!checkWin(HUMAN) && !checkWin(AI) && !gameOver()) {
// human move
if(turn % 2 == 0) {
getHumanMove();
if(checkWin(HUMAN)) cout << "Human Player Wins" << endl;
turn++;
printBoard();
} else {
cout << endl << "Computer Player Move:" << endl;
Move AImove = minimax(board);
board[AImove.x][AImove.y] = ai;
if(checkWin(AI)) cout << "Computer Player Wins" << endl;
turn++;
printBoard();
}
}
}