-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
677 lines (597 loc) · 19.3 KB
/
demo.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
<!DOCTYPE html>
<html>
<head>
<title>trueChess | Open Source Chess framework.</title>
<meta charset="UTF-8">
<meta name="description" content="Free and Open Source chess framework for your next chess game project.">
<meta name="keywords" content="trueChess, chess game, game, board, pawn, king, knight, rook, bishop, chess board, white pawn, black pawn, check mate, ichess, chessboard, play chess, learn chess, develop chess, integrate chess board">
<meta name="author" content="Sonu Auti @sonuauti">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css"> <!-- required for animation, if set false then not required-->
<style type="text/css">
:root{
--cell1:#779556;
--cell2:#ebecd0;
--abort:#ff00006f;
--draw:#999;
--resign:#ffd7009f;
--cellhw:58px;
--theme_wood:#B06328;
--theme_darkwood:#855F38;
--theme_pink:#FF576B;
--theme_vibrant:#F25A04;
--theme_black:#222222;
--theme_default:#779556;
--borad_border:#444;
--wking:url('img/wking.png');
--wqueen:url('img/wqueen.png');
--wbishop:url('img/wbishop.png');
--wknight:url('img/wknight.png');
--wrook:url('img/wrook.png');
--wpawn:url('img/wpawn.png');
--bking:url('img/bking.png');
--bqueen:url('img/bqueen.png');
--bbishop:url('img/bbishop.png');
--bknight:url('img/bknight.png');
--brook:url('img/brook.png');
--bpawn:url('img/bpawn.png');
}
#startBtn:hover{
background: #95BB4A !important;
}
.btn{
font-size: 16px;border-radius: 8px;border: none;width: 120px;height: 50px;color: #fff;font-weight: bold;letter-spacing: 2px;margin: 10px;
}
.themes{
max-width: 20px;
min-width: 15px;
min-height: 15px;
border-radius: 15px;
padding: 4px;
margin: 10px;
display: inline-block !important;
}
.evenCol{
display: inline-block;
background-color: var(--cell2);
padding: 4px;
}
.oddCol{
display: inline-block;
background-color: var(--cell1);
padding: 4px;
}
.cell{
font-size: 14px;
padding: 1px;
overflow: hidden;
text-align: center;
min-width: var(--cellhw);
min-height: var(--cellhw);
max-width: var(--cellhw);
max-height: var(--cellhw);
}
.boardBg{
box-shadow: 0px 2px 5px 4px #ccc;
border-radius: 5px;
padding: 10px;
}
#controls{
margin: 5px;
max-width: 100%;
text-align: center;
}
.wking{
background: var(--wking);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.wqueen{
background: var(--wqueen);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.wbishop{
background: var(--wbishop);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.wknight{
background: var(--wknight);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.wrook{
background: var(--wrook);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.wpawn{
background: var(--wpawn);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.bking{
background: var(--bking);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.bqueen{
background: var(--bqueen);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.bbishop{
background: var(--bbishop);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.bknight{
background: var(--bknight);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.brook{
background: var(--brook);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.bpawn{
background: var(--bpawn);
background-repeat: no-repeat;
background-size: contain;
text-align:left;
}
.hnotations{
font-size: 13px;
float: right;
margin-top: 75%;
}
.vnotations{
font-size: 13px;
float: left;
}
</style>
<script type="text/javascript">
const truechess={memoryBank:[],
pieces:["king","queen","bishop","knight","rook","pawn"],
ruleBook:{
"king":{"moves":["step_fw","step_bw","step_dig","step_left","step_right"],"step_size_max":"1","step_size_min":"1"},
"queen":{"moves":["step_fw","step_bw","step_dig","step_left","step_right"],"step_size_max":"n","step_size_min":"1"},
"bishop":{"moves":["step_dig"],"step_size_max":"n","step_size_min":"1"},
"knight":{"moves":["step_knight"],"step_size_max":"3","step_size_min":"3"},
"rook":{"moves":["step_fw","step_bw","step_left","step_right"],"step_size_max":"n","step_size_min":"1"},
"pawn":{"moves":["step_fw","step_dig"],"step_size_max":"2","step_size_min":"1"}
},
uniCodeSym:{wking:'♔',wqueen:'♕',wbishop:'♗',wknight:'♘',wrook:'♖',wpawn:'♙',bking:'♚',bqueen:'♛',bbishop:'♝',bknight:'♞',brook:'♜',bpawn:'♟'},
aplhas:['a','b','c','d','e','f','g','h'],
updateCssPro:function(key,value){
let root_ = document.querySelector(':root');
root_.style.setProperty(key,value);
},
updateTheme:function(value){
let root_ = document.querySelector(':root');
root_.style.setProperty('--cell1',value);
window.localStorage['btheme']=value;
},
getStyle:function(key){
let root_ = document.querySelector(':root');
let rs = getComputedStyle(root_);
return rs.getPropertyValue(key);
},
createControls:function(){
let controls=document.getElementById("controls");
},
checkIfExists:function(pieceName,positions){
for (var key in positions) {
if (positions.hasOwnProperty(key)) {
let val = positions[key];
console.log("Input name "+pieceName+" -> "+val);
if (val==pieceName) {
break;
return true;
}
}
}
},
searchMemBank:function(memoryBank){
console.log(memoryBank['a8']);
},
local_config:[],
createChessBoard:function(boardId,config){
this.local_config=config;
if (window.localStorage['btheme']) {
this.updateTheme(window.localStorage['btheme']);
}else{
var root_ = document.querySelector(':root');
var rs = getComputedStyle(root_);
this.updateTheme(rs.getPropertyValue('--'+config.boardTheme));
}
var chessboard=document.getElementById(boardId);
if (config.showBorder==true) {
chessboard.style.border="2px solid #444";
}
var table=document.createElement('div');
table.setAttribute("id","table");
chessboard.appendChild(table);
if (config.isDraggable=true) {
this.addDrag();
}
//check the width of main div and calculate the cell size
var hw = chessboard.style.width.split("px")[0];
if (hw<150) {
hw=150;
chessboard.style.width='150px';
}else if (hw<=300) {
config.showBoardNotations=false;
}
//console.log(hw);
this.updateCssPro('--cellhw',((hw/8)-16)+"px");
var maxRowCol=8;
var notationNum=8;
var numNotations=['8','7','6','5','4','3','2','1'];
for (var row = 0; row <maxRowCol; row++) {
var tr=document.createElement('div');
tr.style.marginTop="-2px";
table.appendChild(tr);
for (var icol = 0; icol <maxRowCol; icol++) {
acol=this.aplhas[icol];
ntrow=numNotations[row];
var pieceName='no';
var squareId=(acol+""+ntrow);
//calculate the positions
if (config.position=='start') {
pieceName=this.getInitialPos(ntrow,acol);
}else if(isObject(config.position)){
//console.log('object is here');
for (var key in config.position) {
if (config.position.hasOwnProperty(key)) {
var val = config.position[key];
// console.log(val);
if (squareId==key) {
pieceName=val;
}
}
}
}
//store the initial piece information in memory bank
this.memoryBank[squareId]=pieceName;
var td=document.createElement('div');
td.setAttribute("id",squareId);
if (row%2==0) {
if (icol%2==0) {
td.className = 'evenCol';
}else{
td.className = 'oddCol';
}
}else{
if (icol%2==0) {
td.className = 'oddCol';
}else{
td.className = 'evenCol';
}
}
var notationVerticals='';
var notationHorizontal='';
var notationColor='gray';
if(config.showBoardNotations==true){
if (icol==0) {
notationVerticals=notationNum--;
}
if (row==7) {
notationHorizontal=this.aplhas[icol];
}
if (td.className=='evenCol') {
notationColor='--cell1';
}else{
notationColor='--cell2';
}
}
if (pieceName!='no') {
var real_div=document.createElement("div");
if (config.lockMoves!=true) {
if (config.isDraggable==true) {
real_div.setAttribute("draggable",true);
}
}
if (config.displayUnicodeSymbols==true) {
this.updateCssPro("--wrook",uniCodeSym[pieceName]);
//console.log(uniCodeSym[pieceName]);
}
if (config.imgSrc && config.imgSrc!="") {
this.updateCssPro("--"+pieceName,"url('"+config.imgSrc+pieceName+".png')");
//console.log(uniCodeSym[pieceName]);
}
if (config.animation==true) {
real_div.className=pieceName+" cell animated bounceIn";
}else{
real_div.className=pieceName+" cell";
}
}else{
var real_div=document.createElement("div");
real_div.setAttribute("draggable",false);
if (config.animation==true) {
real_div.className='cell animated';
}else{
real_div.className='cell animated';
}
}
real_div.setAttribute("id",(acol+"_"+ntrow));
if (config.lockMoves!=true) {
real_div.onclick=function(e){
squareId=e.target.id.split("_")[0]+""+e.target.id.split("_")[1];
window.truechess.onCellTap(e.target.id,squareId);
};
}else{
real_div.setAttribute("draggable",false);
}
if (config.showBoardNotations) {
let span1=document.createElement("span");
span1.innerHTML=notationVerticals;
span1.style.color=this.getStyle(notationColor);
span1.className="vnotations";
let span2=document.createElement("span");
span2.innerHTML=notationHorizontal;
span2.style.color=this.getStyle(notationColor);
span2.className="hnotations";
real_div.appendChild(span1);
real_div.appendChild(span2);
//console.log(notationColor);
}
td.appendChild(real_div);
tr.appendChild(td);
}
}
},
getInitialPos:function(row,col){
let ptype = 'no'; //not occupied
if (row==8 || row ==7) {
ptype='b';
}else if (row==1 || row ==2) {
ptype='w';
}
let algebricSqr= (col+""+row);
//console.log(algebricSqr);
//this is for rook
if (algebricSqr=="a8" || algebricSqr=="h8" || algebricSqr=="a1" || algebricSqr=="h1") {
return ptype+""+this.pieces[4];
}
//this is for knight
if (algebricSqr=="b1" || algebricSqr=="b8" || algebricSqr=="g1" || algebricSqr=="g8") {
return ptype+""+this.pieces[3];
}
//this is bishop
if (algebricSqr=="c1" || algebricSqr=="c8" || algebricSqr=="f1" || algebricSqr=="f8") {
return ptype+""+this.pieces[2];
}
//this is for queen
if (algebricSqr=="d1" || algebricSqr=="d8") {
return ptype+""+this.pieces[1];
}
//this if king
if (algebricSqr=="e1" || algebricSqr=="e8") {
return ptype+""+this.pieces[0];
}
//this if for pawn
if (row==2 || row==7) {
return ptype+""+this.pieces[5];
}else{
return "no";
}
},
cleanup:function(boardId){
if (document.getElementById("table")) {
document.getElementById("table").remove();
}
this.createChessBoard(boardId,local_config);
},animateView:function(viewId,animateClass){
//console.log('animation of view '+viewId);
let e=document.getElementById(viewId);
//console.log(e.classList.contains(animateClass));
if (e.classList.contains(animateClass)) {
e.classList.remove(animateClass);
setTimeout(function(){
e.classList.add(animateClass);
},50);
}else{
e.classList.add(animateClass);
}
setTimeout(function() {
e.classList.remove(animateClass);
}, 300);
},onCellTap:function(eleId,squareId){
this.animateView(eleId,'bounceIn');
this.checkRule(squareId,eleId);
},
/**
update the cell symbol
**/
udpateCell:function(eleId,data){
document.getElementById(eleId).className=data;
},
isKingCheck:function(){
},
getPositions:function(){
return this.memoryBank;
},
moves:{start:false,end:false,ptype:'na',squareId:-1,startEleId:'',endEleId:'',data:''},
checkRule:function(squareId,eleId){
let pieceName=this.memoryBank[squareId];
if (pieceName!="no" && this.moves.start==false) {
console.log('First tap at '+pieceName);
this.moves.start=true;
this.moves.end=false;
this.moves.ptype=pieceName; //store first position
this.moves.squareId=squareId;
this.moves.startEleId=eleId;
this.moves.data=document.getElementById(this.moves.startEleId).className;
if (this.local_config.onDragStart) {
this.local_config.onDragStart(eleId);
}
}else if (this.moves.end==false && this.moves.start==true) {
console.log('second tap at '+pieceName);
if (this.local_config.onDragStop) {
this.local_config.onDragStop(eleId);
}
if (this.moves.ptype[0]==this.memoryBank[squareId][0]) {
console.log('trying to capture own pieces');
this.moves.start=false;
return;
}else if(pieceName=="wking" || pieceName=="bking"){
console.log('Kind can not be captured');
this.moves.start=false;
return;
}
this.memoryBank[squareId]=this.moves.ptype; //update position with last
this.memoryBank[this.moves.squareId]='no';
this.moves.endEleId=eleId;
if (this.moves.ptype[0]=="w" && this.memoryBank[squareId][0]=="b") {
//capture move
this.udpateCell(this.moves.startEleId,document.getElementById(this.moves.endEleId).className);
}else{
//no capture move
this.udpateCell(this.moves.startEleId,"cell");
}
this.udpateCell(this.moves.endEleId,this.moves.data);
if (this.local_config.isDraggable==true) {
document.getElementById(this.moves.endEleId).setAttribute("draggable",true);
document.getElementById(this.moves.startEleId).setAttribute("draggable",false);
}
this.local_config.onPositionUpdate(this.moves.startEleId,this.moves.endEleId,this.moves.ptype);
this.moves={start:false,end:true,ptype:'no',squareId:-1,startEleId:'',endEleId:'',data:''};
}else {
if (this.moves.start==false) {
console.log('Not selected');
}else{
console.log('Invalid move, please check');
}
this.moves={start:false,end:true,ptype:'no',squareId:-1,startEleId:'',endEleId:'',data:''};
}
},
isObject:function(obj){
return obj !== undefined && obj !== null && obj.constructor == Object;
},
//drag functions
dragObj:{dragstart:'',dragend:''},
dragOnCellTap:function(eleId,squareId,isStart){
if (isStart && this.dragObj.dragstart!=squareId) {
this.dragObj.dragstart=squareId;
this.onCellTap(eleId,squareId);
}else if(!isStart){
this.dragObj.dragend=squareId;
this.onCellTap(eleId,squareId);
}
},
highLightBox:function(event,isShadow){
if (event.target.id[1]=="_") {
if (!isShadow) {
console.log('remove border for '+event.target.id);
event.target.style.boxShadow="none";
}else{
event.target.style.boxShadow="0px 0px 2px 2px #ccc";
}
}
},
addDrag:function(){
//console.log(this);
document.addEventListener("dragstart", function(event) {
console.log('drag started ->'+event.target.id);
window.truechess.dragOnCellTap(event.target.id,event.target.id[0]+""+event.target.id[2],true);
});
document.addEventListener("dragend", function(event) {
console.log('into dragend '+event.target.id);
if (event.target.id[1]=="_") {
console.log('remove border for '+event.target.id);
window.truechess.highLightBox(event,false);
}
console.log(window.truechess.dragObj);
window.truechess.dragObj={dragstart:'',dragend:''};
});
document.addEventListener("dragenter", function(event) {
console.log('drag enter over -> '+event.target.id);
if (event.target.id[1]=="_") {
window.truechess.highLightBox(event,true);
}
});
document.addEventListener("dragover", function(event) {
event.preventDefault();
});
document.addEventListener("dragleave", function(event) {
console.log('drag leave '+event.target.id);
if (event.target.id[1]=="_") {
window.truechess.highLightBox(event,false);
}
});
document.addEventListener("drop", function(event) {
event.preventDefault();
console.log('into drop target '+event.target.id);
if (event.target.id[1]=="_") {
window.truechess.dragOnCellTap(event.target.id,event.target.id[0]+""+event.target.id[2],false);
console.log('finished '+event.target.id);
window.truechess.highLightBox(event,false);
}
});
}
};
window['truechess']=truechess;
</script> <!-- main js file -->
</head>
<body style="text-align: center;font-size: 16px;">
<div id="chessBoard1" style="width: 360px; margin: auto;"> </div>
<div style="box-shadow: 0px 0px 2px 2px #eee;
border-radius: 10px;margin-left: 12px;display: inline-block;margin-top: 10px;">
<div id="theme_wood" class="themes animated" style="background: var(--theme_wood);" onclick="truechess.updateTheme(document.getElementById(this.id).style.background);"></div>
<div id="theme_darkwood" class="themes animated" style="background: var(--theme_darkwood);" onclick="truechess.updateTheme(document.getElementById(this.id).style.background);"></div>
<div id="theme_pink" class="themes animated" style="background: var(--theme_pink);" onclick="truechess.updateTheme(document.getElementById(this.id).style.background);"></div>
<div id="theme_vibrant" class="themes animated" style="background: var(--theme_vibrant);" onclick="truechess.updateTheme(document.getElementById(this.id).style.background);"></div>
<div id="theme_default" class="themes animated" style="background: var(--theme_default);" onclick="truechess.updateTheme(document.getElementById(this.id).style.background);"></div>
<div id="theme_black" class="themes animated" style="background: var(--theme_black);" onclick="truechess.updateTheme(document.getElementById(this.id).style.background);"></div>
</div>
<script type="text/javascript">
function initiChess(){
var config = {
showBoardNotations:true, //show board notations 1-8, a-f
position:"start", // either start or position array
orientation:'default', //top side down
isDraggable:true, //drag element
isFocus:false, //
boardTheme:'theme_default', //board color
showClock:true, //hide or show clock
gameTime:'10', //game time
imgSrc:'img/', //absolute path of icons
displayUnicodeSymbols:false, //display web friendly icons
showCapturedPieces:true, //show captured pieces
showBoardWithPieces:true, //show board only
animation:true, //show animation
showBorder:false, //show or hide board border
lockMoves:false,
onPositionUpdate:function(startPos,endPos,element){
console.log('This is from callback function '+startPos+","+endPos+","+element);
console.log(truechess.getPositions());
},
onDragStart:function(startPos){
console.log('Position started at '+startPos);
},
onDragStop:function(endPos){
console.log('Position end at '+endPos);
},
}
//console.log(window.truechess);
truechess.createChessBoard('chessBoard1',config);
//console.log(truechess.getPositions());
}
initiChess();
</script>
</body>
</html>