-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.cpp
46 lines (39 loc) · 1023 Bytes
/
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
#include "TicTacToe.hpp"
using namespace std;
TicTacToe::TicTacToe(bool AI_is_X) : aiTurn(AI_is_X), computer(AI_is_X ? 1 : 2), human(AI_is_X ? 2 : 1) {}
bool TicTacToe::isGameOver() {
return b.isGameOver();
}
void TicTacToe::printBoard() {
b.print();
}
void TicTacToe::getNextMove() {
int move;
if (aiTurn) {
move = computer.getMove(b);
if (b.isValidMove(move)) {
b.setPiece(move, computer.getPiece());
aiTurn = false;
} else {
cout << "Error: AI made Invalid Move" << endl;
}
} else {
move = human.getMove(b);
if (b.isValidMove(move)) {
b.setPiece(move, human.getPiece());
aiTurn = true;
} else {
cout << "Error: Invalid Move" << endl;
getNextMove();
}
}
}
int TicTacToe::getWinner() {
return b.getWinner();
}
int TicTacToe::getAIPiece() {
return computer.getPiece();
}
int TicTacToe::getConsolePiece() {
return human.getPiece();
}