-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
81 lines (69 loc) · 2.09 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>chessboardjs Example #5001 - Play Random Computer</title>
<link rel="stylesheet" href="css/chessboard-1.0.0.min.css">
</head>
<body>
<!--- Begin Example HTML ------------------------------------------------------>
<div id="myBoard" style="width: 90vw"></div>
<!--- End Example HTML -------------------------------------------------------->
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/chessboard-1.0.0.js"></script>
<script src="js/danschess.js"></script>
<script>
// --- Begin Example JS --------------------------------------------------------
// NOTE: this example uses the chess.js library:
// https://github.com/jhlywa/chess.js
var board = null
var game = new Chess()
function onDragStart (source, piece, position, orientation) {
// do not pick up pieces if the game is over
if (game.game_over()) return false
// only pick up pieces for White
if (piece.search(/^b/) !== -1) return false
}
function makeRandomMove () {
var possibleMoves = game.moves()
// game over
if (possibleMoves.length === 0) return
var randomIdx = Math.floor(Math.random() * possibleMoves.length)
game.move(possibleMoves[randomIdx])
board.position(game.fen())
}
function onDrop (source, target) {
// see if the move is legal
var move = game.move({
from: source,
to: target,
promotion: 'q' // NOTE: always promote to a queen for example simplicity
})
// illegal move
if (move === null) return 'snapback'
// make random legal move for black
window.setTimeout(makeRandomMove, 250)
}
// update the board position after the piece snap
// for castling, en passant, pawn promotion
function onSnapEnd () {
board.position(game.fen())
}
var config = {
draggable: true,
position: 'start',
onDragStart: onDragStart,
onDrop: onDrop,
onSnapEnd: onSnapEnd
}
board = Chessboard('myBoard', config)
// --- End Example JS ----------------------------------------------------------
</script>
<style>
body {
touch-action: none;
}
</style>
</body>
</html>