|
3 | 3 | /* eslint max-params: 0 */ |
4 | 4 |
|
5 | 5 | /** |
6 | | - * Minimax (sometimes MinMax, MM[1] or saddle point[2]) is a decision rule used in artificial intelligence, |
7 | | - * decision theory, game theory, statistics, and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario. |
8 | | - * Optimized with alpha-beta pruning. |
9 | | - * {@link https://en.wikipedia.org/wiki/Minimax} |
10 | | - * {@link https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning} |
11 | | - * |
12 | | - * @public |
13 | | - * @module others/minimax |
14 | | - * |
15 | | - * @example |
16 | | - * |
17 | | - * var miniMax = |
18 | | - * require('path-to-algorithms/src/others/minimax').minimax; |
19 | | - * var result = minimax( |
20 | | - * [1, 2, 3], |
21 | | - * true, |
22 | | - * 5, |
23 | | - * -Infinity, |
24 | | - * Infinity, |
25 | | - * state => ({ move: 0, state: [2, 3, 4] }), |
26 | | - * state => state[1] < 3, |
27 | | - * state => state[1] |
28 | | - * ); |
29 | | - * |
30 | | - * @param {*} state Current game state |
31 | | - * @param {Boolean} maximize Defines if the result should be maximized or minimized |
32 | | - * @param {Number} depth Defines the maximum depth search |
33 | | - * @param {Number} alpha Maximum score that the minimizing player is assured |
34 | | - * @param {Number} beta Minimum score that the maximizing player is assured |
35 | | - * @param {Function} getPossibleNextStatesFn Function which returns all possible next moves with states . |
36 | | - * @param {Function} isGameOverFn Function which returns if game is over. |
37 | | - * @param {Function} getScoreFn Function which returns score. |
38 | | - * @return {{score: Number, move: *}} which contains the minimum coins from the given |
39 | | - * list, required for the change. |
| 6 | + * @param {Function} getPossibleNextStatesFn Function which returns all possible next moves with states . |
| 7 | + * @param {Function} isGameOverFn Function which returns if game is over. |
| 8 | + * @param {Function} getScoreFn Function which returns score. |
| 9 | + * @return {Function} minimax function |
40 | 10 | */ |
41 | | - function minimax( |
42 | | - state, |
43 | | - maximize, |
44 | | - depth, |
45 | | - alpha, |
46 | | - beta, |
| 11 | + function minimaxBuilder( |
47 | 12 | getPossibleNextStatesFn, |
48 | 13 | isGameOverFn, |
49 | 14 | getScoreFn |
50 | 15 | ) { |
51 | | - if (depth === 0 || isGameOverFn(state)) { |
52 | | - const score = getScoreFn(state); |
53 | | - return {score, move: null}; |
54 | | - } |
| 16 | + /** |
| 17 | + * Minimax (sometimes MinMax, MM[1] or saddle point[2]) is a decision rule used in artificial intelligence, |
| 18 | + * decision theory, game theory, statistics, and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario. |
| 19 | + * Optimized with alpha-beta pruning. |
| 20 | + * {@link https://en.wikipedia.org/wiki/Minimax} |
| 21 | + * {@link https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning} |
| 22 | + * |
| 23 | + * @public |
| 24 | + * @module others/minimax |
| 25 | + * |
| 26 | + * @example |
| 27 | + * |
| 28 | + * var miniMax = |
| 29 | + * require('path-to-algorithms/src/others/minimax').minimax; |
| 30 | + * var result = minimax( |
| 31 | + * [1, 2, 3], |
| 32 | + * true, |
| 33 | + * 5, |
| 34 | + * -Infinity, |
| 35 | + * Infinity, |
| 36 | + * state => ({ move: 0, state: [2, 3, 4] }), |
| 37 | + * state => state[1] < 3, |
| 38 | + * state => state[1] |
| 39 | + * ); |
| 40 | + * |
| 41 | + * @param {*} state Current game state |
| 42 | + * @param {Boolean} maximize Defines if the result should be maximized or minimized |
| 43 | + * @param {Number} depth Defines the maximum depth search |
| 44 | + * @param {Number} alpha Maximum score that the minimizing player is assured |
| 45 | + * @param {Number} beta Minimum score that the maximizing player is assured |
| 46 | + * @return {{score: Number, move: *}} which contains the minimum coins from the given |
| 47 | + * list, required for the change. |
| 48 | + */ |
| 49 | + const minimax = ( |
| 50 | + state, |
| 51 | + maximize, |
| 52 | + depth, |
| 53 | + alpha, |
| 54 | + beta |
| 55 | + ) => { |
| 56 | + if (depth === 0 || isGameOverFn(state)) { |
| 57 | + const score = getScoreFn(state); |
| 58 | + return {score, move: null}; |
| 59 | + } |
55 | 60 |
|
56 | | - const possibleMoveResults = getPossibleNextStatesFn(state); |
| 61 | + const possibleMoveResults = getPossibleNextStatesFn(state); |
57 | 62 |
|
58 | | - if (maximize) { |
| 63 | + if (maximize) { |
59 | 64 |
|
60 | | - let maxResult = {score: -Infinity, move: null}; |
| 65 | + let maxResult = {score: -Infinity, move: null}; |
61 | 66 |
|
62 | | - for (const next of possibleMoveResults) { |
63 | | - const result = minimax( |
64 | | - next.state, |
65 | | - false, |
66 | | - depth - 1, |
67 | | - alpha, |
68 | | - beta, |
69 | | - getPossibleNextStatesFn, |
70 | | - isGameOverFn, |
71 | | - getScoreFn |
72 | | - ); |
| 67 | + for (const next of possibleMoveResults) { |
| 68 | + const result = minimax( |
| 69 | + next.state, |
| 70 | + false, |
| 71 | + depth - 1, |
| 72 | + alpha, |
| 73 | + beta, |
| 74 | + ); |
73 | 75 |
|
74 | | - if (result.score > maxResult.score) { |
75 | | - maxResult = {score: result.score, move: next.move}; |
76 | | - } |
| 76 | + if (result.score > maxResult.score) { |
| 77 | + maxResult = {score: result.score, move: next.move}; |
| 78 | + } |
77 | 79 |
|
78 | | - alpha = Math.max(alpha, result.score); |
| 80 | + alpha = Math.max(alpha, result.score); |
79 | 81 |
|
80 | | - if (alpha >= beta) { |
81 | | - break; |
| 82 | + if (alpha >= beta) { |
| 83 | + break; |
| 84 | + } |
82 | 85 | } |
83 | | - } |
84 | 86 |
|
85 | | - return maxResult; |
86 | | - } else { |
87 | | - let minResult = {score: Infinity, move: null}; |
88 | | - |
89 | | - for (const next of possibleMoveResults) { |
90 | | - const result = minimax( |
91 | | - next.state, |
92 | | - true, |
93 | | - depth - 1, |
94 | | - alpha, |
95 | | - beta, |
96 | | - getPossibleNextStatesFn, |
97 | | - isGameOverFn, |
98 | | - getScoreFn |
99 | | - ); |
100 | | - |
101 | | - if (result.score < minResult.score) { |
102 | | - minResult = {score: result.score, move: next.move}; |
103 | | - } |
| 87 | + return maxResult; |
| 88 | + } else { |
| 89 | + let minResult = {score: Infinity, move: null}; |
104 | 90 |
|
105 | | - beta = Math.min(beta, result.score); |
| 91 | + for (const next of possibleMoveResults) { |
| 92 | + const result = minimax( |
| 93 | + next.state, |
| 94 | + true, |
| 95 | + depth - 1, |
| 96 | + alpha, |
| 97 | + beta, |
| 98 | + ); |
106 | 99 |
|
107 | | - if (beta <= alpha) { |
108 | | - break; |
| 100 | + if (result.score < minResult.score) { |
| 101 | + minResult = {score: result.score, move: next.move}; |
| 102 | + } |
| 103 | + |
| 104 | + beta = Math.min(beta, result.score); |
| 105 | + |
| 106 | + if (beta <= alpha) { |
| 107 | + break; |
| 108 | + } |
109 | 109 | } |
110 | | - } |
111 | 110 |
|
112 | | - return minResult; |
| 111 | + return minResult; |
| 112 | + } |
113 | 113 | } |
| 114 | + |
| 115 | + return minimax; |
114 | 116 | } |
115 | 117 |
|
116 | | - exports.minimax = minimax; |
| 118 | + exports.minimaxBuilder = minimaxBuilder; |
117 | 119 |
|
118 | 120 | })(typeof window === 'undefined' ? module.exports : window); |
0 commit comments