mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Add minimax algorithm logic
Heuristic and game state definition still missing. No tests yet.
This commit is contained in:
parent
b2427d0e18
commit
33695ce766
58
src/algorithms/ai/Minimax.js
Normal file
58
src/algorithms/ai/Minimax.js
Normal file
@ -0,0 +1,58 @@
|
||||
/**
|
||||
*
|
||||
* @param {*} node description of game state
|
||||
* @param {Number} depth limit to the search depth
|
||||
* @param {Number} player player id, either 0 or 1
|
||||
* @returns game state resulting from the best found move
|
||||
*/
|
||||
function minimax(node, depth, player) {
|
||||
if (depth === 0 || terminal_state(node)) {
|
||||
return this.heuristic(node, player), node; // pretend there is a heuristic function supplies
|
||||
}
|
||||
opt = player == 0 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
|
||||
|
||||
opt_node = undefined;
|
||||
|
||||
for (const child in node.possibleStates()) { // pretend there is node.possibleStates
|
||||
nextPlayer = player === 1 ? 0 : 1;
|
||||
|
||||
value, _ = minimax(move, depth - 1, nextPlayer);
|
||||
|
||||
if (player === 0) {
|
||||
if (value < optimal) {
|
||||
optimal = value;
|
||||
opt_node = child;
|
||||
}
|
||||
} else {
|
||||
if (value > optimal) {
|
||||
optimal = value;
|
||||
opt_node = child;
|
||||
}
|
||||
}
|
||||
}
|
||||
return optimal, opt_node;
|
||||
}
|
||||
|
||||
/* inspired by Python...
|
||||
|
||||
def minimax(self, node: Node, depth: int, player: int):
|
||||
if depth == 0 or self.is_terminal_state(node):
|
||||
return self.h(node, player), node
|
||||
|
||||
opt = math.inf if (player != 0) else -math.inf
|
||||
opt_node = None
|
||||
for child in node.compute_and_get_children():
|
||||
# test all possible moves
|
||||
h, _ = self.minimax(child, depth-1, player ^ 1)
|
||||
if player != 0:
|
||||
if h > opt:
|
||||
opt = h
|
||||
opt_node = child
|
||||
else:
|
||||
if h < opt:
|
||||
opt = h
|
||||
opt_node = child
|
||||
|
||||
return opt, opt_node
|
||||
|
||||
*/
|
9
src/algorithms/ai/__test__/Minimax.test.js
Normal file
9
src/algorithms/ai/__test__/Minimax.test.js
Normal file
@ -0,0 +1,9 @@
|
||||
import Minimax from '../Minimax';
|
||||
|
||||
describe('Minimax', () => {
|
||||
|
||||
if('exists', () => {
|
||||
expect(true);
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user