-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Minimax implementation #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| var minimax = require('../../src/others/minimax.js').minimax; | ||
| const minimaxBuilder = require('../../src/others/minimax.js').minimaxBuilder; | ||
|
|
||
| describe('Minimax with tic tac toe', function () { | ||
|
||
| 'use strict'; | ||
|
|
@@ -14,33 +14,28 @@ describe('Minimax with tic tac toe', function () { | |
| })); | ||
| } | ||
|
|
||
| function minimaxFor(player, state) { | ||
| const getScoreFn = player === 'x' | ||
| ? state => game.getScore(state).x - game.getScore(state).o | ||
| : state => game.getScore(state).o - game.getScore(state).x; | ||
|
|
||
| return minimax( | ||
| state, | ||
| true, | ||
| 5, | ||
| -Infinity, | ||
| Infinity, | ||
| getAllNextStates, | ||
| state => game.isGameOver(state), | ||
| getScoreFn | ||
| ); | ||
| } | ||
| const minimaxForX = minimaxBuilder( | ||
| getAllNextStates, | ||
| state => game.isGameOver(state), | ||
| state => game.getScore(state).x - game.getScore(state).o | ||
| ) | ||
|
|
||
| const minimaxForO = minimaxBuilder( | ||
| getAllNextStates, | ||
| state => game.isGameOver(state), | ||
| state => game.getScore(state).o - game.getScore(state).x | ||
| ) | ||
|
|
||
| it('should be defined', function () { | ||
| expect(minimax).toBeDefined(); | ||
| expect(minimaxBuilder).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should win versus dumb agent as first player', function () { | ||
| let state = game.newState('x'); | ||
|
|
||
| while (!game.isGameOver(state)) { | ||
| if (state.turn === 'x') { | ||
| state = game.nextState(state, minimaxFor(state.turn, state).move); | ||
| state = game.nextState(state, minimaxForX(state, true, 5, -Infinity, Infinity).move); | ||
| } else { | ||
| const move = game.emptyCells(state)[0]; | ||
| state = game.nextState(state, move); | ||
|
|
@@ -56,7 +51,7 @@ describe('Minimax with tic tac toe', function () { | |
|
|
||
| while (!game.isGameOver(state)) { | ||
| if (state.turn === 'o') { | ||
| state = game.nextState(state, minimaxFor(state.turn, state).move); | ||
| state = game.nextState(state, minimaxForO(state, true, 5, -Infinity, Infinity).move); | ||
| } else { | ||
| const move = game.emptyCells(state)[0]; | ||
| state = game.nextState(state, move); | ||
|
|
@@ -72,7 +67,11 @@ describe('Minimax with tic tac toe', function () { | |
| let state = game.newState('x'); | ||
|
|
||
| while (!game.isGameOver(state)) { | ||
| state = game.nextState(state, minimaxFor(state.turn, state).move); | ||
| if (state.turn === 'o') { | ||
| state = game.nextState(state, minimaxForO(state, true, 5, -Infinity, Infinity).move); | ||
| } else { | ||
| state = game.nextState(state, minimaxForX(state, true, 5, -Infinity, Infinity).move); | ||
| } | ||
| } | ||
| expect(game.isGameOver(state)).toBe(true); | ||
| expect(game.getScore(state)).toEqual({x: 0, o: 0}); | ||
|
|
@@ -149,13 +148,10 @@ function ticTacToe() { | |
| } | ||
|
|
||
| function nextState(state, move) { | ||
| const board = state.board; | ||
| const newBoard = state.board.map(row => row.slice()); | ||
| newBoard[move.y][move.x] = state.turn; | ||
| return { | ||
| board: [ | ||
| ...board.slice(0, move.y), | ||
| [...board[move.y].slice(0, move.x), state.turn, ...board[move.y].slice(move.x + 1)], | ||
| ...board.slice(move.y + 1) | ||
| ], | ||
| board: newBoard, | ||
| turn: state.turn === 'x' ? 'o' : 'x', | ||
| }; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.