Minimax is a decision rule used in decision theory, game theory, statistics and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario.
This Minimax module implements the Negamax simplification with alpha–beta pruning.
Copy the content of sources/
folder into your project.
To use this Minimax module, you'll need to instanciate it with your presets (heuristic
and childhood
functions). Then, each time you'll need to get the best move node to play, you'll have to call the right decision rule (maximize
or minimize
) to apply to current move node.
We first start an instance of this Minimax module :
// starts a Minimax instance with your presets
const minimax = new Minimax(heuristic, childhood);
Then, we could use this instance to maximize the next move node to play :
// calls maximize decision rule for current move node limited by given depth
const maximize = minimax.maximize(start, depth);
// gets the best next move node to play
console.log(maximize);
Or we could minimize the next move node to play :
// calls minimize decision rule for current move node limited by given depth
const minimize = minimax.minimize(start, depth);
// gets the worst next move node to play
console.log(minimize);
Creates an instance of this Minimax module.
// starts a Minimax instance with your presets
const minimax = new Minimax(heuristic, childhood);
property | name | type | description |
---|---|---|---|
parameter | heuristic |
function |
scores a move node |
parameter | childhood |
function |
retrieves next move nodes from a move node |
heuristic()
:
The heuristic
function purpose is to score a move node to tell Minimax how to compare them with each other. Since it is related to your application, you must define your own heuristic
function to pass through the constructor
following the documentation below.
property | name | type | description |
---|---|---|---|
parameter | move |
object |
move node |
return | score |
number |
score of the move node |
childhood()
:
The childhood
function purpose is to retrieve the next move nodes from a move node to tell Minimax how to go through the move tree. Since it is related to your application, you must define your own childhood
function to pass through the constructor
following the documentation below.
property | name | type | description |
---|---|---|---|
parameter | move |
object |
move node |
return | moves |
array |
array of move nodes |
Maximizes the chances of current move winning (minimum loss).
// calls maximize decision rule for current move node limited by given depth
const maximize = minimax.maximize(start, depth);
property | name | type | description |
---|---|---|---|
parameter | start |
object |
move node |
parameter | depth |
number |
max depth to reach browsing the move nodes tree |
Minimizes the chances of current move winning (maximum loss).
// calls minimize decision rule for current move node limited by given depth
const minimize = minimax.minimize(start, depth);
property | name | type | description |
---|---|---|---|
parameter | start |
object |
move node |
parameter | depth |
number |
max depth to reach browsing the move nodes tree |