-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathGame.cpp
More file actions
143 lines (116 loc) · 3.98 KB
/
Game.cpp
File metadata and controls
143 lines (116 loc) · 3.98 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
#include "Game.hpp"
#include <iostream>
#include <limits>
#include <cstdlib>
#include <ctime>
Game::Game(int boardSize) : board(boardSize), gameOver(false) {
player1 = nullptr;
player2 = nullptr;
currentPlayer = nullptr;
std::srand(static_cast<unsigned int>(std::time(nullptr)));
}
Game::~Game() {
delete player1;
delete player2;
}
void Game::initializePlayers(const std::string& p1Name, const std::string& p2Name) {
player1 = new Player(p1Name, 'X');
player2 = new Player(p2Name, 'O', false); // Computer player
currentPlayer = player1;
}
void Game::play() {
while (!gameOver) {
board.display();
if (currentPlayer->isHumanPlayer()) {
int row, col;
std::cout << currentPlayer->getName() << "'s turn (symbol: "
<< currentPlayer->getSymbol() << ")" << std::endl;
std::cout << "Enter row (0-" << board.getSize()-1 << "): ";
std::cin >> row;
std::cout << "Enter column (0-" << board.getSize()-1 << "): ";
std::cin >> col;
makeMove(row, col);
} else {
std::cout << "Computer's turn..." << std::endl;
computerMove();
}
}
displayResult();
}
void Game::makeMove(int row, int col) {
if (!board.isValidPosition(row, col)) {
std::cout << "Invalid position!" << std::endl;
return;
}
if (!board.isEmpty(row, col)) {
std::cout << "Position already taken!" << std::endl;
return;
}
board.makeMove(row, col, currentPlayer->getSymbol());
if (board.checkWin(currentPlayer->getSymbol())) {
gameOver = true;
return;
}
if (board.isFull()) {
gameOver = true;
currentPlayer = nullptr;
return;
}
switchPlayer();
}
void Game::switchPlayer() {
currentPlayer = (currentPlayer == player1) ? player2 : player1;
}
void Game::displayResult() const {
board.display();
if (currentPlayer) {
std::cout << currentPlayer->getName() << " wins!" << std::endl;
} else {
std::cout << "It's a draw!" << std::endl;
}
}
bool Game::isGameOver() const {
return gameOver;
}
Player* Game::getCurrentPlayer() const {
return currentPlayer;
}
void Game::computerMove() {
auto [row, col] = findBestMove();
makeMove(row, col);
}
std::pair<int, int> Game::findBestMove() const {
int bestScore = std::numeric_limits<int>::min();
std::pair<int, int> bestMove = {0, 0};
for (int i = 0; i < board.getSize(); i++) {
for (int j = 0; j < board.getSize(); j++) {
if (board.isEmpty(i, j)) {
Board tempBoard = board; // Create a copy
tempBoard.makeMove(i, j, player2->getSymbol());
int score = minimax(tempBoard, false, 0); //pass the temporary boardState
if (score > bestScore) {
bestScore = score;
bestMove = {i, j};
}
}
}
}
return bestMove;
}
int Game::minimax(Board boardState, bool isMax, int depth) const {
if (boardState.checkWin(player2->getSymbol())) return 10 - depth;
if (boardState.checkWin(player1->getSymbol())) return depth - 10;
if (boardState.isFull()) return 0;
int bestScore = isMax ? std::numeric_limits<int>::min() : std::numeric_limits<int>::max();
for (int i = 0; i < boardState.getSize(); i++) {
for (int j = 0; j < boardState.getSize(); j++) {
if (boardState.isEmpty(i, j)) {
Board tempBoard = boardState; // Create a copy
tempBoard.makeMove(i, j, isMax ? player2->getSymbol() : player1->getSymbol());
int score = minimax(tempBoard, !isMax, depth + 1); // pass the temporary boardState recursively
bestScore = isMax ? std::max(score, bestScore) : std::min(score, bestScore);
}
}
}
return bestScore;
}