-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessAI.js
421 lines (382 loc) · 12.4 KB
/
ChessAI.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//Choosing side
let toShow=document.querySelector("#toShow");
toShow.style.display="none";
$('#black').on('click',0, startGame)
$('#white').on('click',1, startGame)
const whiteSquareGrey = '#a9a9a9'
const blackSquareGrey = '#696969'
var game;
var board;
let side;
let playerSide;
let moveToMake;
let bonus;
let DEPTH=3;
let moveSound;
let captureSound;
let totalScore=0;
/*let playerTime=0;
let compTime=0;
let Ctimer;
let Ptimer;*/
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.volume = 0.1;
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
}
function init(){
let pawn=[ 0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0];
let knight=[-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50]
let bishop= [-20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20]
let rook=[ 0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0]
let queen=[-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20]
let king=[-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-20,-30,-30,-40,-40,-30,-30,-20,
-10,-20,-20,-20,-20,-20,-20,-10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20]
let blackp=pawn.slice().reverse();
let blackn=knight.slice().reverse();
let blackb=bishop.slice().reverse();
let blackr=rook.slice().reverse();
let blackq= queen.slice().reverse();
let blackk=king.slice().reverse();
let whitepieces= [pawn, knight, bishop, rook, queen, king];
let blackpieces= [blackp, blackn, blackb, blackr, blackq, blackk];
for (let j=0; j<6;j++){
for (let i=0; i<64;i++){
whitepieces[j][i]=[game.SQUARES[i],whitepieces[j][i]]
blackpieces[j][i]=[game.SQUARES[i],blackpieces[j][i]]
}
}
let wp=new Map(whitepieces[0]);let bp= new Map(blackpieces[0])
let wn= new Map(whitepieces[1]); let bn=new Map(blackpieces[1])
let wb= new Map(whitepieces[2]); let bb= new Map(blackpieces[2])
let wr= new Map(whitepieces[3]);let br= new Map(blackpieces[3])
let wq= new Map(whitepieces[4]); let bq= new Map(blackpieces[4])
let wk= new Map(whitepieces[5]); let bk= new Map(blackpieces[5])
let white=[["p",wp],["n",wn],["b",wb],["r",wr],["q",wq],["k",wk]], black=[["p",bp],["n",bn],["b",bb],["r",br],["q",bq],["k",bk]];
let w= new Map(white), b=new Map(black);
let bonusarr=[["w",w],["b",b]];
bonus= new Map(bonusarr);
}
function startGame(data){
moveSound = new sound("./asssets/moveSound.wav");
captureSound= new sound("./assets/captureSound.wav");
side= data.data
playerSide= (side)?'w':'b'
let toRemove= document.querySelector("#choice");
toRemove.style.display= "none";
createBoard(moveSound)
toShow.style.display='inline';
toShow.style.margin='0px';
}
// Creating Board with pieces
function createBoard(){
$('#restartBtn').on('click', restart)
$('#resetBtn').on('click', reset)
game= new Chess()
init()
const boardConfig= {
draggable: true,
position: 'start',
onDragStart: onDragStart,
onDrop: onDrop,
onSnapEnd: onSnapEnd,
showErrors: 'console',
pieceTheme: 'https://koblenski.github.io/javascript/chessboardjs-0.3.0/img/chesspieces/wikipedia/{piece}.png',
moveSpeed: 450,
onMouseoutSquare: onMouseoutSquare,
onMouseoverSquare: onMouseoverSquare,
orientation: (side)?'white':'black'
}
//Only move your pieces
function onDragStart (source, piece, position, orientation) {
if (game.game_over()) return false
if (piece.search(playerSide) === -1) {
return false
}
}
board = new Chessboard("board",boardConfig);
//Only allow legal moves
function onDrop (source, target) {
var tar=game.get(target);
var move = game.move({
from: source,
to: target,
promotion: 'q'
})
if (move === null) return 'snapback'
removeGreySquares();
if(move.flags === 'c' || move.flags ==='e'){
captureSound.play()
var t=tar.type; var c= tar.color;
updateScore(t,c,"#playScore","#compScore")
var p=c+t.toUpperCase();
$('#playPieces').prepend("<img src='https://koblenski.github.io/javascript/chessboardjs-0.3.0/img/chesspieces/wikipedia/"+p+".png' width='25'/>" );
}
else {
moveSound.play()
}
window.setTimeout(makeBestMove, 250);
updateStatus()
}
//Update board position
function onSnapEnd () {
board.position(game.fen())
}
function makeBestMove(){
minimax(new Chess(game.fen()),DEPTH,Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)
var tar=game.get(moveToMake.to)
game.move(moveToMake)
board.position(game.fen())
if(moveToMake.flags === 'c' || moveToMake.flags ==='e' ){
captureSound.play()
var t=tar.type; var c= tar.color;
updateScore(t,c,"#compScore","#playScore")
var p=c+t.toUpperCase();
$('#compPieces').prepend("<img src='https://koblenski.github.io/javascript/chessboardjs-0.3.0/img/chesspieces/wikipedia/"+p+".png' width='25'/>" );
}
else {
moveSound.play()
}
updateStatus()
}
//Show available moves
function onMouseoverSquare (square, piece) {
if(piece && piece.search(playerSide) !== -1 && ! game.game_over()){
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)
}
}
}
function onMouseoutSquare (square, piece) {
removeGreySquares()
}
function removeGreySquares () {
$('#board .square-55d63').css('background', '')
}
function greySquare (square) {
var $square = $('#board .square-' + square)
var background = whiteSquareGrey
if ($square.hasClass('black-3c85d')) {
background = blackSquareGrey
}
$square.css('background', background)
}
// Update game score in the web page
function updateScore(t,c,player,notPlayer){
console.log("test")
$(player).empty();
$(notPlayer).empty();
if(c ==='w'){
totalScore+=(t == 'p')?1:(t == 'n')?3:(t=='b')?3:(t == 'r')?5:9;
}
else{
totalScore-=(t == 'p')?1:(t == 'n')?3:(t=='b')?3:(t == 'r')?5:9;
}
if(totalScore > 0){
if(side){
$('#compScore').append("+"+totalScore);
}
else {
$('#playScore').append("+"+totalScore);
}
}
else if(totalScore < 0){
if(side){
$('#playScore').append("+"+Math.abs(totalScore));
}
else {
$('#compScore').append("+"+Math.abs(totalScore));
}
}
}
// Update game status in the web page
function updateStatus () {
var status = ''
var moveColor = 'White'
if (game.turn() === 'b') {
moveColor = 'Black'
}
if (game.in_checkmate()) {
status = 'Game over, ' + moveColor + ' is in checkmate.'
alert(status);
}
else if (game.in_draw()) {
status = 'Game over, drawn position'
alert(status)
}
else {
status = moveColor + ' to move'
if (game.in_check()) {
status += ', ' + moveColor + ' is in check'
colorKing(game.turn())
}
}
$('#status').html(status)
$('#pgn').html(game.pgn({ max_width: 10, newline_char: '<br />' }))
}
//Color in red the cell of the king in check
function colorKing(t){
var sq;
for(let i=0; i<64 ; i++){
let square= game.SQUARES[i];
let piece= game.get(square);
if(piece){
if(piece.color === t && piece.type === 'k'){
sq=square;
}
}
}
$('#board .square-' + sq).css("background","red");
}
// Make the best move decision
function minimax(position, depth, alpha, beta){
if((depth == 0)|| position.game_over()){
return evaluation(position)
}
var legalMoves= position.moves()
var bestVal= (position.turn()=== 'w')? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
for (var i = 0; i < legalMoves.length; i++) {
position.move(legalMoves[i])
var val= minimax(new Chess(position.fen()),depth-1,alpha, beta)
let m=position.undo()
bestVal=(position.turn()=== 'w')? MAX(bestVal,val, depth, m): MIN(bestVal, val, depth, m);
if(position.turn()==='w'){
alpha = Math.max(alpha, bestVal)
}else{
beta = Math.min(beta, bestVal)
}
if(beta <= alpha){
return bestVal;
}
}
return bestVal
}
function MAX(bestVal, val, depth, m){
if(val > bestVal && depth == DEPTH){
moveToMake=m;
}
return Math.max(bestVal, val)
}
function MIN(bestVal, val, depth,m){
if(val < bestVal && depth == DEPTH){
moveToMake=m;
}
return Math.min(bestVal, val)
}
function evaluation(position){
let eval=0;
for(let i=0; i<64 ; i++){
let square= position.SQUARES[i];
let piece= position.get(square);
if(piece){
eval+=pieceEval(piece,square);
}
}
return eval ;
}
// evaluation of onboard pieces
function pieceEval(piece, square){
let t=piece.type;
let c=piece.color;
let coeff=(c =='w')?1:-1;
let val=(t == 'p')?100:(t == 'n')?320:(t=='b')?330:(t == 'r')?500:(t == 'q')?900: 10000;
val+=bonus.get(c).get(t).get(square);
return coeff*val;
}
updateStatus()
// restart game
function restart(){
clearTimeout(makeBestMove)
clearTimeout(board.position)
board.start();
game.reset();
$('#compPieces').empty();
$('#playPieces').empty();
$('#compPieces').append("<b><p id='compScore'></p></b>")
$('#playPieces').append("<b><p id='playScore'></p></b>")
totalScore=0;
updateStatus();
if(!side){
window.setTimeout(makeBestMove, 750);
}
}
//reset game
function reset(){
window.location.reload(true);
}
/*function updateTimer(){
playingSide=game.turn();
orientation= board.orientation()[0];
if(playingSide === orientation ){
if(Ptimer) clearInterval(Ptimer)
document.getElementById('playerTimer').innerHTML=playerTime++;
Ctimer= setInterval(function(){document.getElementById('playerTimer').innerHTML=playerTime++;},1000);
}else{
console.log("Comp "+compTime)
if(Ctimer) clearInterval(Ctimer)
document.getElementById('compTimer').innerHTML=compTime++;
Ptimer= setInterval(function(){document.getElementById('compTimer').innerHTML=compTime++;},900);
}
}*/
if(! side){
window.setTimeout(makeBestMove, 250);
}
createBoard.restart=restart
}