-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
282 lines (229 loc) · 7.23 KB
/
main.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Game variables
var game = new Chess()
var $status = $('#status')
var $fen = $('#fen')
var $pgn = $('#pgn')
var board = null
var whiteSquareGrey = '#a9a9a9'
var blackSquareGrey = '#696969'
// Function to remove highlight from squares
function removeGreySquares () {
$('#myBoard .square-55d63').css('background', '')
}
// Function to highlight squares
function greySquare (square) {
var $square = $('#myBoard .square-' + square)
var background = whiteSquareGrey
if ($square.hasClass('black-3c85d')) {
background = blackSquareGrey
}
$square.css('background', background)
}
// Logs the position to the console when a move is made (optional function, can be commented)
function onChange(oldPos, newPos){
console.log('Position Changed')
console.log('New Position: ' + Chessboard.objToFen(newPos))
}
// Executed when a piece is dragged
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 the side to move
if ((game.turn() === 'w' && piece.search(/^b/) !== -1) ||
(game.turn() === 'b' && piece.search(/^w/) !== -1)) {
return false
}
// Console logs for debugging
console.log('Drag Started')
console.log('Source: ' + source)
console.log('Piece: ' + piece)
console.log('Position: ' + Chessboard.objToFen(position))
console.log('Orientation: ' + orientation)
}
// Executed when a piece is dropped on the board
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'
updateStatus()
}
// update the board position after the piece snap
// for castling, en passant, pawn promotion
function onSnapEnd () {
board.position(game.fen())
removeGreySquares()
}
function updateStatus () {
var status = ''
var moveColor = 'White'
if (game.turn() === 'b') {
moveColor = 'Black'
}
// checkmate?
if (game.in_checkmate()) {
status = 'Game over, ' + moveColor + ' is in checkmate.'
}
// draw?
else if (game.in_draw()) {
status = 'Game over, drawn position'
}
// game still on
else {
status = moveColor + ' to move'
// check?
if (game.in_check()) {
status += ', ' + moveColor + ' is in check'
}
}
$status.html(status)
// $fen.html(game.fen())
$pgn.html(game.pgn())
}
// Binds highlighting to mouse hover
function onMouseoverSquare (square, piece) {
// get list of possible moves for this square
var moves = game.moves({
square: square,
verbose: true
})
// exit if there are no moves available for this square
if (moves.length === 0) return
// highlight the square they moused over
greySquare(square)
// highlight the possible squares for this piece
for (var i = 0; i < moves.length; i++) {
greySquare(moves[i].to)
}
}
// Binds highlight clearing to moving mouse out of square
function onMouseoutSquare (square, piece) {
removeGreySquares()
}
// Game configuration
var config = {
position: 'start',
orientation: 'white',
draggable: true,
dropOffBoard: 'snapback',
moveSpeed: 'slow',
snapbackSpeed: 500,
snapSpeed: 100,
sparePieces: false,
pieceTheme: 'img/chesspieces/wikipedia/{piece}.png',
onChange: onChange,
onDragStart: onDragStart,
onSnapEnd: onSnapEnd,
onMouseoutSquare: onMouseoutSquare,
onMouseoverSquare: onMouseoverSquare,
onDrop: onDrop
}
// Final board setup using chessboard.js
var board = Chessboard('myBoard', config)
// Logs the board position to the console
function clickShowPositionBtn () {
console.log('Current position as an Object:')
console.log(board.position())
console.log('Current position as a FEN string:')
console.log(board.fen())
}
// Reloads the window to reset the game and board
function resetGame(){
window.location.reload()
// Another way to reload everything without refreshing the page
// board.destroy
// board = Chessboard('myBoard', config)
// game.reset()
// if (timer){
// clearTimeout(timer)
// timer = 0
// }
// updateStatus()
}
// Changes the board orientation
function flip(){
board.flip()
}
// ######################################################################
// Simulation
// ######################################################################
// Function that makes a random move
function makeRandomMove () {
var possibleMoves = game.moves()
// exit if the game is over
if (game.game_over()) return
var randomIdx = Math.floor(Math.random() * possibleMoves.length)
game.move(possibleMoves[randomIdx])
board.position(game.fen())
updateStatus()
timer = window.setTimeout(makeRandomMove, 500)
}
// Starts a game between two AIs playing random moves
function playRandom(){
board.destroy
game.reset()
updateStatus()
board = Chessboard('myBoard', 'start')
timer = window.setTimeout(makeRandomMove, 500)
}
// #######################################################################
// Bindings to buttons
// #######################################################################
$('#showPositionBtn').on('click', clickShowPositionBtn)
$('#resetBtn').on('click', resetGame)
$('#flipOrientationBtn').on('click', flip)
$('#playRandomBtn').on('click', playRandom)
$(window).resize(board.resize)
// ##################################################################
// Adding the AI interfaces in this section
// ##################################################################
// Loads the level 0 AI script into the head tag
function level0 (){
board.destroy()
var newScript = document.createElement('script')
newScript.type = 'text/javascript'
newScript.src = 'engine/random.js'
document.getElementsByTagName('head')[0].appendChild(newScript)
}
// Loads the level 1 AI script into the head tag
function level1 (){
board.destroy()
var newScript = document.createElement('script')
newScript.type = 'text/javascript'
newScript.src = 'engine/level1.js'
document.getElementsByTagName('head')[0].appendChild(newScript)
}
// Loads the level 2 AI script into the head tag
function level2 (){
board.destroy()
var newScript = document.createElement('script')
newScript.type = 'text/javascript'
newScript.src = 'engine/level2.js'
document.getElementsByTagName('head')[0].appendChild(newScript)
}
// Loads the level 3 AI script into the head tag
function level3 (){
board.destroy()
var newScript = document.createElement('script')
newScript.type = 'text/javascript'
newScript.src = 'engine/level3.js'
document.getElementsByTagName('head')[0].appendChild(newScript)
}
// Buttons to start a game against AI
$('#playLevel0').on('click', level0)
$('#playLevel1').on('click', level1)
$('#playLevel2').on('click', level2)
$('#playLevel3').on('click', level3)
// Undo operation
// Removes the last two moves from the board
function undoMove(){
game.undo()
game.undo()
board.position(game.fen())
updateStatus()
}
$('#undoBtn').on('click', undoMove)