simple-chess-step-2
by lhartikk
HTML
<script src="https://cdn.rawgit.com/lhartikk/simple-chess-ai/master/lib/js/chess.js"></script>
<script src="https://chessboardjs.com/js/chessboard.js"></script>
<!-- Used to get images -->
<base href="http://chessboardjs.com/" />
<h3 class="board">
Evaluation function
</h3>
<div id="board" class="board"></div>
<br>
<div class="info">
<div id="move-history" class="move-history">
</div>
</div>
CSS
.board {
width: 400px;
margin: auto
}
.info {
width: 400px;
margin: auto;
}
.move-history {
max-height: 100px;
overflow-y: scroll;
}
/*!
* chessboard.js $version$
*
* Copyright 2013 Chris Oakman
* Released under the MIT license
* https://github.com/oakmac/chessboardjs/blob/master/LICENSE
*
* Date: $date$
*/
/* clearfix */
.clearfix-7da63 {
clear: both;
}
/* board */
.board-b72b1 {
border: 2px solid #404040;
-moz-box-sizing: content-box;
box-sizing: content-box;
}
/* square */
.square-55d63 {
float: left;
position: relative;
/* disable any native browser highlighting */
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* white square */
.white-1e1d7 {
background-color: #f0d9b5;
color: #b58863;
}
/* black square */
.black-3c85d {
background-color: #b58863;
color: #f0d9b5;
}
/* highlighted square */
.highlight1-32417, .highlight2-9c5d2 {
-webkit-box-shadow: inset 0 0 3px 3px yellow;
-moz-box-shadow: inset 0 0 3px 3px yellow;
box-shadow: inset 0 0 3px 3px yellow;
}
/* notation */
.notation-322f9 {
cursor: default;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
position: absolute;
}
.alpha-d2270 {
bottom: 1px;
right: 3px;
}
.numeric-fc462 {
top: 2px;
left: 2px;
}
JavaScript
var board,
game = new Chess();
/*The "AI" part starts here */
var calculateBestMove =function(game) {
var newGameMoves = game.ugly_moves();
var bestMove = null;
//use any negative large number
var bestValue = -9999;
for(var i = 0; i < newGameMoves.length; i++) {
var newGameMove = newGameMoves[i];
game.ugly_move(newGameMove);
//take the negative as AI plays as black
var boardValue = -evaluateBoard(game.board())
game.undo();
if(boardValue > bestValue) {
bestValue = boardValue;
bestMove = newGameMove
}
}
return bestMove;
};
var evaluateBoard = function (board) {
var totalEvaluation = 0;
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 8; j++) {
totalEvaluation = totalEvaluation + getPieceValue(board[i][j]);
}
}
return totalEvaluation;
};
var getPieceValue = function (piece) {
if (piece === null) {
return 0;
}
var getAbsoluteValue = function (piece) {
if (piece.type === 'p') {
return 10;
} else if (piece.type === 'r') {
return 50;
} else if (piece.type === 'n') {
return 30;
} else if (piece.type === 'b') {
return 30 ;
} else if (piece.type === 'q') {
return 90;
} else if (piece.type === 'k') {
return 900;
}
throw "Unknown piece type: " + piece.type;
};
var absoluteValue = getAbsoluteValue(piece, piece.color === 'w');
return piece.color === 'w' ? absoluteValue : -absoluteValue;
};
/* board visualization and games state handling starts here*/
var onDragStart = function (source, piece, position, orientation) {
if (game.in_checkmate() === true || game.in_draw() === true ||
piece.search(/^b/) !== -1) {
return false;
}
};
var makeBestMove = function () {
var bestMove = getBestMove(game);
...