-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic minimax search implemented, not tested
- Loading branch information
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef MINIMAX_SEARCH_H | ||
#define MINIMAX_SEARCH_H | ||
|
||
#include <string> | ||
#include "Move.h" | ||
#include "BitBoard.h" | ||
|
||
class MinimaxSearch | ||
{ | ||
private: | ||
static const int maxDepth = 5; | ||
|
||
double minValue(BitBoard board, int currentDepth); | ||
double maxValue(BitBoard board, int currentDepth); | ||
|
||
public: | ||
Move minimaxDecision(BitBoard &board); | ||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <string> | ||
#include <limits> | ||
#include <algorithm> | ||
#include "BitBoard.h" | ||
#include "Move.h" | ||
#include "MinimaxSearch.h" | ||
|
||
/* Private Functions */ | ||
|
||
double MinimaxSearch::minValue(BitBoard board, int currentDepth) | ||
{ | ||
if (currentDepth == maxDepth) return board.utility(board.player()); | ||
double v = std::numeric_limits<double>::lowest(); | ||
for (Move move : board.actions()) { | ||
v = std::min(v, maxValue(board.result(move), ++currentDepth)); | ||
} | ||
return v; | ||
} | ||
|
||
double MinimaxSearch::maxValue(BitBoard board, int currentDepth) | ||
{ | ||
if (currentDepth == maxDepth) return board.utility(board.player()); | ||
double v = std::numeric_limits<double>::max(); | ||
for (Move move : board.actions()) { | ||
v = std::max(v, minValue(board.result(move), ++currentDepth)); | ||
} | ||
return v; | ||
} | ||
|
||
/* Public Functions */ | ||
|
||
Move MinimaxSearch::minimaxDecision(BitBoard &board) | ||
{ | ||
Move bestMove; | ||
double bestMoveValue{0.0}; | ||
|
||
for (Move move : board.actions()) { | ||
if (bestMove.isEmpty()) { | ||
bestMove = move; | ||
bestMoveValue = minValue(board.result(bestMove), 0); | ||
} else { | ||
double nextMoveValue = minValue(board.result(move), 0); | ||
if (bestMoveValue < nextMoveValue) { | ||
bestMove = move; | ||
bestMoveValue = nextMoveValue; | ||
} | ||
} | ||
} | ||
return bestMove; | ||
} | ||
|