Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions solutions/cpp/tictactoe/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ std::pair<int, int> Game::findBestMove() const {
Board tempBoard = board; // Create a copy
tempBoard.makeMove(i, j, player2->getSymbol());

int score = minimax(false, 0);
int score = minimax(tempBoard, false, 0); //pass the temporary boardState
if (score > bestScore) {
bestScore = score;
bestMove = {i, j};
Expand All @@ -120,20 +120,20 @@ std::pair<int, int> Game::findBestMove() const {
return bestMove;
}

int Game::minimax(bool isMax, int depth) const {
if (board.checkWin(player2->getSymbol())) return 10 - depth;
if (board.checkWin(player1->getSymbol())) return depth - 10;
if (board.isFull()) return 0;
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 < board.getSize(); i++) {
for (int j = 0; j < board.getSize(); j++) {
if (board.isEmpty(i, j)) {
Board tempBoard = board; // Create a copy
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(!isMax, depth + 1);
int score = minimax(tempBoard, !isMax, depth + 1); // pass the temporary boardState recursively
bestScore = isMax ? std::max(score, bestScore) : std::min(score, bestScore);
}
}
Expand Down
2 changes: 1 addition & 1 deletion solutions/cpp/tictactoe/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Game {
private:
void computerMove();
std::pair<int, int> findBestMove() const;
int minimax(bool isMax, int depth) const;
int minimax(Board boardState, bool isMax, int depth) const; // pass the board state
};

#endif