JSFiddle - React, Tailwind, and code Playground
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">
Random moves
</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();
return newGameMoves[Math.floor(Math.random() * newGameMoves.length)];
};
/* 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);
game.ugly_move(bestMove);
board.position(game.fen());
renderMoveHistory(game.history());
if (game.game_over()) {
alert('Game over');
}
};
var getBestMove = function (game) {
if (game.game_over()) {
alert('Game over');
}
var bestMove = calculateBestMove(game);
return bestMove;
};
var renderMoveHistory = function (moves) {
var historyElement = $('#move-history').empty();
historyElement.empty();
for (var i = 0; i < moves.length; i = i + 2) {
historyElement.append('<span>' + moves[i] + ' ' + ( moves[i + 1] ? moves[i + 1] : ' ') + '</span><br>')
}
historyElement.scrollTop(historyElement[0].scrollHeight);
};
var onDrop = function (source, target) {
var move = game.move({
from: source,
to: target,
promotion: 'q'
});
removeGreySquares();
if (move === null) {
return 'snapback';
}
renderMoveHistory(game.history());
window.setTimeout(makeBestMove, 250);
};
var onSnapEnd = function () {
board.position(game.fen());
};
var onMouseoverSquare = function(square, piece) {
var moves = game.moves({
square: square,
verbose: true
});
if (moves.length === 0) return;
greySquare(square);
for (var i = 0; i < moves.length; i++) {
greySquare(moves[i].to);
}
};
var onMouseoutSquare = function(square, piece) {
...