Last active
September 20, 2016 20:21
-
-
Save Y-Taras/e2e7cf74a5b262d211a8ba25cf460d6b to your computer and use it in GitHub Desktop.
changed version
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
var board = ['x', 'o', 'e', 'e', 'o', 'e', 'e', 'e', 'e']; | |
var signPlayer = 'o'; | |
var signAI = (signPlayer === 'x') ? 'o' : 'x'; | |
game = { | |
over: function(board) { | |
for (var i = 0; i < board.length; i += 3) { | |
if (board[i] === board[i + 1] && board[i + 1] === board[i + 2]) { | |
return board[i] !== 'e' ? board[i] : false; | |
} | |
} | |
for (var j = 0; j < board.length; j++) { | |
if (board[j] === board[j + 3] && board[j + 3] === board[j + 6]) { | |
return board[j] !== 'e' ? board[j] : false; | |
} | |
} | |
if ((board[4] === board[0] && board[4] === board[8]) || | |
(board[4] === board[2] && board[4] === board[6])) { | |
return board[4] !== 'e' ? board[4] : false; | |
} | |
var element; | |
if (board.every(function(element) { | |
return element !== 'e'; | |
})) { | |
return true; | |
} | |
}, | |
winner: function(board) { | |
return game.over(board); | |
}, | |
possible_moves: function(board, sign) { | |
var testBoard = [], | |
nextBoard; | |
for (var i = 0; i < board.length; i++) { | |
nextBoard = board.slice(); | |
if (nextBoard[i] === 'e') { | |
nextBoard[i] = sign; | |
testBoard.push(nextBoard); | |
} | |
} | |
return testBoard; | |
} | |
} | |
function moveScore(board) { | |
if (game.winner(board) === signPlayer) { | |
return -10; | |
} else if (game.winner(board) === signAI) { | |
return +10; | |
} else { | |
return 0; | |
//Game is a draw | |
} | |
} | |
var depth = 0; | |
function max(board) { | |
if (game.over(board)) { | |
return board; | |
} | |
depth++; | |
var newGame = []; | |
var bestMove = []; | |
var score; | |
var best_score = -Infinity; | |
var movesArray = game.possible_moves(board, signAI); | |
for (var i = 0; i < movesArray.length; i++) { | |
newGame = movesArray[i].slice(); | |
score = moveScore(min(newGame)); | |
if (score > best_score) { | |
best_score = score; | |
bestMove = newGame; | |
} | |
} | |
return bestMove; | |
} | |
function min(board) { | |
if (game.over(board)) { | |
return board; | |
} | |
depth++; | |
var newGame = []; | |
var worstMove = []; | |
var score; | |
var worst_score = +Infinity; | |
var movesArray = game.possible_moves(board, signPlayer); | |
for (var i = 0; i < movesArray.length; i++) { | |
newGame = movesArray[i].slice(); | |
score = moveScore(max(newGame)); | |
if (score < worst_score) { | |
worst_score = score; | |
worstMove = newGame; | |
} | |
} | |
return worstMove; | |
} | |
var board = max(board); | |
console.log(moveScore(board), board); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
['e', 'e', 'e',
'e', 'o', 'e',
'e', 'e', 'e'];
['x', 'e', 'e',
'e', 'o', 'e',
'e', 'e', 'e'];
['x', 'o', 'e',
'e', 'o', 'e',
'e', 'e', 'e'];
['x', 'o', 'x',
'e', 'o', 'e',
'e', 'e', 'e'];
['x', 'o', 'x',
'e', 'o', 'e',
'e', 'o', 'e'];