-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
764 lines (676 loc) · 28.3 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
<html>
<head>
<meta charset="UTF-8">
<title>Checkers Game</title>
<style>
.board-container {
display: flex;
justify-content: center;
align-items: center;
width:65%;
margin-right:2%;
float: left;
}
.board {
display: flex;
flex-wrap: wrap;
width: 90%;
max-width: 600px; /* Maximum width of the board */
max-height: 600px; /* Maximum height of the board */
margin: 0 auto; /* Centers the board */
padding: 10px; /* Adds padding around the board */
}
.square {
width: 50px;
height: 50px;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
}
.smallsquare {
width: 20px;
height: 20px;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
}
.light {
background-color: #eaeaea;
}
.dark {
background-color: gray;
}
.grid-row {
display: flex;
flex-direction: row;
width: 100%;
}
.movebox {
text-align: center;
width: 20%;
}
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
.left-box {
flex-basis: 70%;
padding: 10px;
}
.right-box {
width: 100%;
background-color: #b4b4b4;
padding: 10px;
margin-right: 20px;
}
.selected {
background-color: rgb(158, 0, 0);
}
.rightside {
width:40%;
float: right;
}
.leftside {
display: flex;
align-items: flex-start;
justify-content: center;
width:55%;
float: left;
}
.whiteMoves {
float: left;
width: 40%;
}
.blackMoves {
float: right;
width: 40%;
}
.turnNumber {
font-weight: bold;
float: left;
width: 10%;
}
.whitemove {
background-color: rgb(252, 213, 172);
border: 1px black solid;
}
.blackmove {
background-color: rgb(44, 44, 44);
color: white;
border: 1px rgb(252, 213, 172) solid;
}
.gameOver {
padding: 10px;
display: block;
clear: both;
max-width: 140px;
}
.dot {
position: relative;
}
.dot::before {
content: "";
display: block;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: white;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="container" id="container">
<div class="left-box">
<div class="board" id="boardContainer">
<div id="board"></div>
</div>
</div>
<div class="right-box movebox" id="rightbox">
<label style="color:black;">New game:</label><br/>
<button id="startWhite">White</button> <button id="startBlack">Black</button> <button id="puzzle">Puzzle</button>
<br/>
Evaluation: <label id="eval"></label> Evaluated Nodes [This Move]: <label id="nodes"></label> [<label id="nodesThisMove"></label>]<br/>
<div class="leftside" style="height: 320px; overflow: auto;">
<div class="turnNumber" id="turnNumber"></div>
<div class="whiteMoves" id="whiteMoves"></div>
<div class="blackMoves" id="blackMoves"></div>
</div>
<div class="rightside">
<div id="demoBoard"></div><br/>
<div id="gameover" class="gameover"></div>
</div>
</div>
</div>
<!--<button onclick="setupBoard()">PuzzleBoard</button>-->
<button onclick="saveAsPNG('board')">Save as PNG</button>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.2.1/axios.min.js"></script>
<script>
function saveAsPNG(id) {
// Get the div element
var div = document.getElementById(id);
console.log(div)
// Use HTML2Canvas to convert the contents of the div to an image
html2canvas(div).then(function(canvas) {
// Get the PNG image data URL
var imageData = canvas.toDataURL("image/png");
// Create a new anchor element to download the image
var downloadLink = document.createElement("a");
downloadLink.href = imageData;
downloadLink.download = "image.png";
// Trigger a click event on the anchor element to download the image
downloadLink.click();
});
}
const boardContainer = document.getElementById("board");
const demoBoard = document.getElementById("demoBoard");
const whiteMovebox = document.getElementById("whiteMoves");
const blackMovebox = document.getElementById("blackMoves");
const turnNumber = document.getElementById("turnNumber");
var gameoverbox = document.getElementById("gameover");
var evaluation = document.getElementById("eval")
var evaluatedNodes = document.getElementById("nodes")
var evaluatedNodesThisMove = document.getElementById("nodesThisMove")
evaluation.innerHTML = '0'
evaluatedNodes.innerHTML = '0'
evaluatedNodesThisMove.innerHTML = '0'
var turnIndex = 0;
const gridSquares = [];
var imgSrc = "./images/";
var gameData;
var gameover = false;
const whitePieceSrc = imgSrc + "white_piece.png";
const blackPieceSrc = imgSrc + "black_piece.png";
const whiteKingSrc = imgSrc + "white_king.png";
const blackKingSrc = imgSrc + "black_king.png";
function getData() {
axios.get('/data')
.then(response => {
gameData = response.data;
console.log(gameData);
})
.catch(error => {
console.error(error);
});
}
getData();
// Create an 8x8 grid
for (let i = 0; i < 8; i++) {
const row = document.createElement("div");
row.classList.add("grid-row");
for (let j = 0; j < 8; j++) {
const square = document.createElement("div");
square.classList.add("square");
if ((i + j) % 2 === 0) {
square.classList.add("light");
} else {
square.classList.add("dark");
}
square.id = String(squareIndexToBitIndex(gridSquares.length));
row.appendChild(square);
gridSquares.push(square);
}
boardContainer.appendChild(row);
}
for (let i = 0; i < 8; i++) {
const row = document.createElement("div");
row.classList.add("grid-row");
for (let j = 0; j < 8; j++) {
const square = document.createElement("div");
square.classList.add("smallsquare");
if ((i + j) % 2 === 0) {
square.classList.add("light");
} else {
square.classList.add("dark");
}
square.innerHTML = String(squareIndexToWCDFIndex(i*8 + j));
row.appendChild(square);
}
demoBoard.appendChild(row);
}
function setupBoard() {
console.log("setupBoard")
axios.get('/setBoard')
.then(response => {
const data = response.data;
console.log(data)
const whiteMen = data.whiteMen;
const blackMen = data.blackMen;
const whiteKings = data.whiteKings;
const blackKings = data.blackKings;
setBoard(whiteMen, blackMen, whiteKings, blackKings);
})
.catch(error => {
console.error(error);
});
}
// Set the icon for a square
function setIcon(bitIndex, colour) {
let index = this.bitIndexToSquareIndex(bitIndex);
let iconSrc = colour.length == 1
? colour == "w" ? whitePieceSrc : blackPieceSrc
: colour == "wk" ? whiteKingSrc : blackKingSrc;
if (colour.length !== 1) {
console.log(colour, iconSrc)
}
const square = gridSquares[index];
const icon = document.createElement("img");
icon.src = iconSrc;
icon.alt = colour;
icon.style.width = "90%";
icon.style.height = "90%";
square.innerHTML = "";
square.appendChild(icon);
}
// Remove the icon from a square
function removeIcon(bitIndex) {
let index = this.bitIndexToSquareIndex(bitIndex);
const square = gridSquares[index];
square.innerHTML = "";
}
function squareIndexToWCDFIndex(index) {
if (Math.floor(index/8) % 2 == 0) {
return index % 2 == 1 ? Math.floor((index)/ 2) + 1 : '';
} else {
return index % 2 == 0 ? Math.floor((index)/ 2) + 1: '';
}
}
function squareIndexToBitIndex(index) {
if (Math.floor(index/8) % 2 == 0) {
return index % 2 == 1 ? Math.floor((63-(index))/ 2) : '';
} else {
return index % 2 == 0 ? Math.floor((63-(index))/ 2) : '';
}
}
function bitIndexToSquareIndex(index) {
return Math.floor(index/4) % 2 == 0 ? 62-(index * 2) : 64-(index * 2) - 1;
}
function bitIndexToDisplayIndex(index) {
return (Math.floor(index / 4) * 4) + (3 - index % 4)
}
function showSquareIndexes() {
for (let i = 0; i < 64; i++) {
let index = squareIndexToBitIndex(i);
if (index !== '') {
gridSquares[i].innerHTML = bitIndexToDisplayIndex(index)
}
}
}
function addMove(move, colour) {
let box;
let c;
if (colour == "w") {
box = whiteMovebox;
c = "whitemove";
turnIndex++;
let turn = document.createElement("div");
turn.innerHTML = turnIndex + '. ';
turn.style.border = "1px solid black";
turnNumber.appendChild(turn);
} else {
box = blackMovebox;
c = "blackmove";
}
let newmove = document.createElement("div");
newmove.classList.add(c);
newmove.innerHTML = move;
box.appendChild(newmove)
}
const rightbox = document.getElementById('rightbox');
function setSize() {
rightbox.style.height = `380px`;
}
setSize();
window.addEventListener('resize', setSize);
function clearMoveboxes() {
whiteMovebox.innerHTML = '';
blackMovebox.innerHTML = '';
turnNumber.innerHTML = '';
turnIndex = 0;
}
function setBoard(whiteMen = [], blackMen = [], whiteKings = [], blackKings = []) {
for (let i = 0; i < 64; i++) {
gridSquares[i].innerHTML = "";
}
for (let i = 0; i < whiteMen.length; i++) {
setIcon(whiteMen[i], "w");
}
for (let i = 0; i < blackMen.length; i++) {
setIcon(blackMen[i], "b");
}
for (let i = 0; i < whiteKings.length; i++) {
console.log("setting white king", whiteKings[i])
setIcon(whiteKings[i], "wk");
}
for (let i = 0; i < blackKings.length; i++) {
console.log("setting black king", blackKings[i])
setIcon(blackKings[i], "bk");
}
}
function addDots(squares) {
for (let i = 0; i < squares.length; i++) {
let square = document.getElementById(squares[i]);
square.classList.add("dot");
}
}
function clearDots() {
const elements = document.querySelectorAll('.dot');
elements.forEach(element => {
element.classList.remove('dot');
});
}
axios.get('/reset')
.then(response => {
const data = response.data;
const whiteMen = data.whiteMen;
const blackMen = data.blackMen;
const whiteKings = data.whiteKing;
const blackKings = data.blackKing;
setBoard(whiteMen, blackMen, whiteKings, blackKings);
})
.catch(error => {
console.error(error);
});
document.getElementById("startWhite").addEventListener('click', () => {
gameover = false;
gameoverbox.innerHTML = "";
if (selectedPiece) document.getElementById(selectedPiece).classList.remove("selected");
selectedPiece = null;
clearDots();
axios.get('/reset')
.then(response => {
const data = response.data;
const whiteMen = data.whiteMen;
const blackMen = data.blackMen;
const whiteKings = data.whiteKing;
const blackKings = data.blackKing;
setBoard(whiteMen, blackMen, whiteKings, blackKings);
clearMoveboxes();
})
.catch(error => {
console.error(error);
});
axios.post('/start', { start: "w" })
.then((response) => {
console.log(response.data);
getData();
})
.catch((error) => {
console.error(error);
});
});
document.getElementById("startBlack").addEventListener('click', () => {
gameover = false;
gameoverbox.innerHTML = "";
if (selectedPiece) document.getElementById(selectedPiece).classList.remove("selected");
selectedPiece = null;
clearDots();
axios.get('/reset')
.then(response => {
const data = response.data;
const whiteMen = data.whiteMen;
const blackMen = data.blackMen;
const whiteKings = data.whiteKing;
const blackKings = data.blackKing;
setBoard(whiteMen, blackMen, whiteKings, blackKings);
clearMoveboxes();
})
.catch(error => {
console.error(error);
});
axios.post('/start', { start: "b" })
.then((response) => {
console.log(response.data);
getData();
})
.catch((error) => {
console.error(error);
});
axios.get('/aimove')
.then(response => {
const data = response.data;
console.log(data)
const whiteMen = data.white;
const blackMen = data.black;
const whiteKings = data.whiteKing;
const blackKings = data.blackKing;
const moveString = data.moveString;
const thisEval = data.evaluation;
const nodeCount = data.evaluatedNodes;
const thisNodeCount = data.evaluatedNodesThisMove;
if (thisEval !== "nan") evaluation.innerHTML = thisEval
evaluatedNodes.innerHTML = nodeCount
evaluatedNodesThisMove.innerHTML = thisNodeCount
const colour = gameData.playAs == 'w' ? 'b' : 'w';
addMove(moveString, colour);
setBoard(whiteMen, blackMen, whiteKings, blackKings);
}).catch(error => {
console.error(error);
});
});
document.getElementById("puzzle").addEventListener('click', () => {
gameover = false;
gameoverbox.innerHTML = "";
if (selectedPiece) document.getElementById(selectedPiece).classList.remove("selected");
selectedPiece = null;
clearDots();
axios.get('/reset')
.then(response => {
const data = response.data;
const whiteMen = data.whiteMen;
const blackMen = data.blackMen;
const whiteKings = data.whiteKing;
const blackKings = data.blackKing;
setBoard(whiteMen, blackMen, whiteKings, blackKings);
clearMoveboxes();
})
.catch(error => {
console.error(error);
});
axios.get('/puzzle')
.then((response) => {
console.log('puzzle data', response.data);
getData();
console.log(gameData)
})
.catch((error) => {
console.error(error);
});
axios.get('/aimove')
.then(response => {
const data = response.data;
console.log(data)
const whiteMen = data.white;
const blackMen = data.black;
const whiteKings = data.whiteKing;
const blackKings = data.blackKing;
const moveString = data.moveString;
const thisEval = data.evaluation;
const nodeCount = data.evaluatedNodes;
const thisNodeCount = data.evaluatedNodesThisMove;
if (thisEval !== "nan") evaluation.innerHTML = thisEval
evaluatedNodes.innerHTML = nodeCount
evaluatedNodesThisMove.innerHTML = thisNodeCount
const colour = gameData.playAs == 'w' ? 'b' : 'w';
addMove(moveString, colour);
setBoard(whiteMen, blackMen, whiteKings, blackKings);
}).catch(error => {
console.error(error);
});
});
var selectedPiece = null;
gridSquares.forEach((pieceElement) => {
pieceElement.addEventListener('click', () => {
// Get the id of the clicked element
const pieceId = pieceElement.id;
if (selectedPiece == null) {
if (validateSelection(pieceId)) {
axios.post('/select_piece', { start: pieceId })
.then((response) => {
console.log(response.data);
if (response.data !== false) {
selectedPiece = pieceId;
pieceElement.classList.add("selected");
addDots(response.data)
}
}).catch((error) => {
console.error(error);
});
}
} else {
if (selectedPiece == pieceId) {
selectedPiece = null;
pieceElement.classList.remove("selected");
clearDots();
} else {
validateMove(selectedPiece, pieceId)
.then((isValid) => {
if (isValid) {
axios.post('/play', { start: selectedPiece, end: pieceId })
.then(response => {
const data = response.data;
console.log(data)
const whiteMen = data.white;
const blackMen = data.black;
const whiteKings = data.whiteKing;
const blackKings = data.blackKing;
const moveString = data.moveString;
const status = data.status;
const colour = gameData.playAs;
addMove(moveString, colour);
setBoard(whiteMen, blackMen, whiteKings, blackKings);
if (status == 0) {
axios.get('/aimove')
.then(response => {
const data = response.data;
console.log(data)
const whiteMen = data.white;
const blackMen = data.black;
const whiteKings = data.whiteKing;
const blackKings = data.blackKing;
const moveString = data.moveString;
const thisEval = data.evaluation;
const nodeCount = data.evaluatedNodes;
const thisNodeCount = data.evaluatedNodesThisMove;
if (thisEval !== "nan") evaluation.innerHTML = thisEval
evaluatedNodes.innerHTML = nodeCount
evaluatedNodesThisMove.innerHTML = thisNodeCount
const colour = gameData.playAs == 'w' ? 'b' : 'w';
addMove(moveString, colour);
setBoard(whiteMen, blackMen, whiteKings, blackKings);
}).catch(error => {
console.error(error);
});
} else {
gameover = true;
switch(status) {
case 1:
gameoverbox.innerHTML = "<b>GAME OVER<br/>WHITE WINS<b>";
gameoverbox.style.backgroundColor = "white";
gameoverbox.style.color = "black";
break;
case 2:
gameoverbox.innerHTML = "<b>GAME OVER<br/>BLACK WINS<b>";
gameoverbox.style.backgroundColor = "black";
gameoverbox.style.color = "white";
break;
case 3:
document.getElementById("gameover").innerHTML = "<b>GAME OVER<br/>DRAW<b>";
break;
}
}
}).catch(error => {
console.error(error);
});
document.getElementById(selectedPiece).classList.remove("selected");
selectedPiece = null;
clearDots();
} else {
document.getElementById(selectedPiece).classList.remove("selected");
selectedPiece = null;
clearDots();
if (validateSelection(pieceId)) {
axios.post('/select_piece', { start: pieceId })
.then((response) => {
console.log(response.data);
if (response.data !== false) {
selectedPiece = pieceId;
pieceElement.classList.add("selected");
addDots(response.data)
}
}).catch((error) => {
console.error(error);
});
}
}
});
}
}
});
});
function validateMove(start, end) {
if (!validateSelection(start)) {
return Promise.resolve(false);
}
if (start == end) {
return Promise.resolve(false);
}
return axios.post('/check_move', { start: start, end: end })
.then((response) => {
return response.data == true;
})
.catch((error) => {
console.error(error);
return false;
});
}
function validateSelection(id) {
console.log(document.getElementById(id));
if (gameover) return false;
const colour = gameData.playAs;
if (document.getElementById(id)) {
if (document.getElementById(id).childNodes.length > 0) {
if (document.getElementById(id).firstChild.alt[0] == colour) {
return true;
}
}
}
return false;
}
/*
* Bitwise Board
* ----------------
* 31 30 29 28
* 27 26 25 24
* 23 22 21 20
* 19 18 17 16
* 15 14 13 12
* 11 10 09 08
* 07 06 05 04
* 03 02 01 00
* ----------------
*
*
* WCDF Board
* ----------------
* 01 02 03 04
* 05 06 07 08
* 09 10 11 12
* 13 14 15 16
* 17 18 19 20
* 21 22 23 24
* 25 26 27 28
* 29 30 31 32
* ----------------
*
*/
</script>
</body>
</html>