-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
753 lines (540 loc) · 19.7 KB
/
Copy pathscript.js
File metadata and controls
753 lines (540 loc) · 19.7 KB
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
//creating board.
let boardContainer = document.getElementById('board-container')
var takenPieces = []
//var moveFx = document.getElementById("myAudio");
var moveFx = new Audio('sounds/move.mp3');
var taken = new Audio('sounds/taken.mp3');
//Turn based system
//White is always first. Meaning that if we count first go as 0, white will have every even go. Black will always be able to go on odd moves.
let currentTurn = 0;
//board array instantiating
let board = [
[['bR'],['bK'],['bB'],['bQ'],['bKn'],['bB'],['bK'],['bR']],
[['bP'],['bP'],['bP'],['bP'],['bP'],['bP'],['bP'],['bP']],
[[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[]],
[[],[],[],[],[],[],[],[]],
[['wP'],['wP'],['wP'],['wP'],['wP'],['wP'],['wP'],['wP']],
[['wR'],['wK'],['wB'],['wQ'],['wKn'],['wB'],['wK'],['wR']]
]
//building pieces
let piece = {
//black pieces with ascii
bP : '♟',
bK : '♞',
bB : '♝',
bR : '♜',
bQ : '♛',
bKn : '♚',
//white pieces with ascii
wP : '♙',
wK : '♘',
wB : '♗',
wR : '♖',
wQ : '♕',
wKn : '♔'
}
//values of pieces
let values = {
//black pieces with ascii
bP : 1,
bK : 3,
bB : 3,
bR : 5,
bQ : 9,
bKn : 0,
//white pieces with ascii
wP : 1,
wK : 3,
wB : 3,
wR : 5,
wQ : 9,
wKn : 0
}
//creating the board tiles
function createBoard(){
for (let i = 0; i <board.length; i++){
//assinging a row variable.
let row = board[i]
//creating a container row div and adding it to DOM.
let rowDom = document.createElement('div')
rowDom.setAttribute('class', 'row n-'+i)
boardContainer.appendChild(rowDom)
//looping through each row
for (let q = 0; q<row.length; q++){
//creating the tile object, adding a class 'tile' and adding to row DOM.
let square = document.createElement('div')
//checking if row index is odd
if (i % 2 != 0){
//if current square is on an even pos
if (q % 2 == 0){
//then we put the white square on the DOM.
square.setAttribute('class', 'square black')
square.setAttribute('onclick', 'point(this)')
square.setAttribute('id', String(i) + String(q))
square.setAttribute('onmouseover','cursorChange(this)')
square.textContent = piece[row[q]]
rowDom.appendChild(square)
}else{
//else we put the black square on the DOM.
square.textContent = piece[row[q]]
square.setAttribute('class', 'square white')
square.setAttribute('onclick', 'point(this)')
square.setAttribute('onmouseover','cursorChange(this)')
square.setAttribute('id', String(i) + String(q))
rowDom.appendChild(square)
}
//cehcking if row index is even,
}else{
if (q % 2 == 0){
//then we put the white square on the DOM.
square.setAttribute('class', 'square white')
square.setAttribute('onclick', 'point(this)')
square.setAttribute('onmouseover','cursorChange(this)')
square.setAttribute('id', String(i) + String(q))
square.textContent = piece[row[q]]
rowDom.appendChild(square)
}else{
//else we put the black square on the DOM.
square.setAttribute('class', 'square black')
square.setAttribute('onclick', 'point(this)')
square.setAttribute('onmouseover','cursorChange(this)')
square.setAttribute('id', String(i) + String(q))
square.textContent = piece[row[q]]
rowDom.appendChild(square)
}
}
}
}
}
//selected should be false if function has not been called yet.
var selectedTF = false
var selectedChessPiece = ''
var allowedKnightMovements = []
var allowedPawnMoves = []
var allowedRookMoves = []
var allowedBishopMoves = []
var allowedQueenMoves = []
var allowedKingMoves = []
var correctSideMoving = false
var allowedToMove = false
function point(x){
allowedToMove = false
if (selectedTF == false){
//if there is a piece
if (x.innerHTML != ''){
x.style.cursor = 'pointer'
if (currentTurn % 2 == 0){
console.log('white turn')
if (x.innerHTML == '♙' || x.innerHTML == '♘' || x.innerHTML == '♗' || x.innerHTML == '♖' || x.innerHTML == '♕' || x.innerHTML == '♔'){
correctSideMoving = true
selectedTF = true
}
}else{
console.log('black turn ')
if (x.innerHTML == '♟' || x.innerHTML == '♞' || x.innerHTML == '♝' || x.innerHTML == '♜' || x.innerHTML == '♛' || x.innerHTML == '♚' ){
correctSideMoving = true
selectedTF = true
}
}
if (correctSideMoving == true){
selectedChessPiece = x
console.log(selectedChessPiece.id)
var piecePos = [Number(x.id[0]), Number(x.id[1])]
//if its a knight, these are the allowed pos's. no pathfinding yet.
if (selectedChessPiece.innerHTML == '♘' || selectedChessPiece.innerHTML == '♞'){
allowedKnightMovements = knightMovement(piecePos)
}
//checking if pawn (black and white)
if (selectedChessPiece.innerHTML == '♟'){
allowedPawnMoves = pawnMovement(piecePos, 'black')
}
if (selectedChessPiece.innerHTML == '♙'){
allowedPawnMoves = pawnMovement(piecePos, 'white')
}
//checking if rook
if (selectedChessPiece.innerHTML == '♜' || selectedChessPiece.innerHTML == '♖'){
allowedRookMoves = rookMovement(piecePos)
}
if (selectedChessPiece.innerHTML == '♝' || selectedChessPiece.innerHTML == '♗'){
allowedBishopMoves = bishopMovement(piecePos)
console.log('bishop')
}
if (selectedChessPiece.innerHTML == '♕' || selectedChessPiece.innerHTML == '♛'){
allowedQueenMoves = queenMovement(piecePos)
}
if (selectedChessPiece.innerHTML == '♔' || selectedChessPiece.innerHTML == '♚'){
allowedKingMoves = kingMovement(piecePos)
}
}
}
//this means if the current piece has been selected:
}else if (selectedTF & selectedChessPiece != x){
//make sure u cant eat your own sided pieces.
//loop through and check if it is black or white moving.
if (currentTurn % 2 == 0){
//whites turn
if (x.innerHTML != '♙' && x.innerHTML != '♘' && x.innerHTML != '♗' && x.innerHTML != '♖' && x.innerHTML != '♕' &&x.innerHTML != '♔'){
//check if we are taking over a piece
if (x.innerHTML == '♟' || x.innerHTML == '♞' || x.innerHTML == '♝' || x.innerHTML == '♜' || x.innerHTML == '♛' || x.innerHTML == '♚' ){
//code for if a piece is being captured
console.log('black piece captured')
takenPieces.push(x.innerHTML)
console.log(takenPieces)
document.getElementById('black-taken').innerHTML += x.innerHTML
}
//calling the function above for all types of pieces.
allowMove(allowedKnightMovements)
allowMove(allowedPawnMoves)
allowMove(allowedRookMoves)
allowMove(allowedBishopMoves)
allowMove(allowedQueenMoves)
allowMove(allowedKingMoves)
}
else{
selectedTF = false
}
}else{
//black's turn
if (x.innerHTML != '♟' && x.innerHTML != '♞' && x.innerHTML != '♝' && x.innerHTML != '♜' && x.innerHTML != '♛' && x.innerHTML != '♚' ){
//check if we are taking over a piece
if (x.innerHTML == '♙' || x.innerHTML == '♘' || x.innerHTML == '♗' || x.innerHTML == '♖' || x.innerHTML == '♕' || x.innerHTML == '♔'){
//code for if a piece is being captured
console.log('white piece captured')
takenPieces.push(x.innerHTML)
console.log(takenPieces)
document.getElementById('taken-list').innerHTML += x.innerHTML
}
allowMove(allowedKnightMovements)
allowMove(allowedPawnMoves)
allowMove(allowedRookMoves)
allowMove(allowedBishopMoves)
allowMove(allowedQueenMoves)
allowMove(allowedKingMoves)
}
else{
selectedTF = false
}
}
console.log('TARGET PIECE:' +x.innerHTML)
//this function allows moves to work by checking if the moved to position is a legal pos.
function allowMove(array){
if (array != []){
//logging for debugs.
console.log([Number(x.id[0]),Number(x.id[1])])
for (item in array){
console.log(array[item])
if (String(array[item]) == String([Number(x.id[0]),Number(x.id[1])])){
console.log('item', array[item])
allowedToMove = true
break
}
selectedTF = false
}
}
}
//if the move is permitted. (remove this if statemnt in order to allow pieces to work if not knight)
if (allowedToMove == true){
if (x.innerHTML){
//if the taken piece is the white queen, black wins.
if (x.innerHTML == '♔'){
alert('BLACK WON')
}
//if the taken piece is the black queen, white wins.
if (x.innerHTML == '♚'){
alert('WHITE WON')
}
taken.play()
//and if piece is not taken
}else{
moveFx.play()
}
//destination piece
let destinationPiece = x
//create distanceBetween, equal to the difference between the selected pice and the destination piece
let distanceBetween = String(selectedChessPiece.id)[0] - String(x.id)[0] + ' ' + String(selectedChessPiece.id[1] - String(x.id[1]))
console.log('destination: '+destinationPiece.id)
selectedTF = false
console.log('distance between: ' +distanceBetween)
//move the chess piece
console.log([board[selectedChessPiece.id[0]][selectedChessPiece.id[1]]])
board[selectedChessPiece.id[0]][selectedChessPiece.id[1]] = ''
board[x.id[0]][x.id[1]] = geyKey(piece, selectedChessPiece.innerHTML)
x.innerHTML = selectedChessPiece.innerHTML
selectedChessPiece.innerHTML = ''
console.log(board)
currentTurn = currentTurn + 1
//Now that we have incremented current turn. If the turn is divisble by 2, update html to say whites turn, else, blacks turn.
if (currentTurn % 2 == 0){
document.getElementById('turn').innerHTML = "White's turn."
}else{
document.getElementById('turn').innerHTML = "Black's turn."
}
checkPoints()
}
//Checking if piece is taken
}
}
//function checks points on board.
function checkPoints(){
let scoreDom = document.getElementById('score')
let whiteScore = 0;
let blackScore = 0;
for (let i = 0; i < board.length; i++){
let currentRow = board[i]
for (let q = 0; q<currentRow.length; q++){
if (currentRow[q].length != 0 && currentRow[q][0][0] == 'w'){
whiteScore = whiteScore + values[currentRow[q]]
}else if (currentRow[q].length != 0 && currentRow[q][0][0] == 'b'){
blackScore = blackScore + values[currentRow[q]]
}
}
}
score = blackScore - whiteScore
let bar = document.getElementById('bar');
bar.style.width = 50+score+'%'
if (score < 0){
let finalScore = 'W+'+score * -1
console.log(finalScore)
scoreDom.innerHTML = finalScore
}else if (score > 0){
let finalScore = 'B+'+score
console.log(finalScore)
scoreDom.innerHTML = finalScore
}else{
finalScore = score
console.log(finalScore)
scoreDom.innerHTML = 'neutral'
}
}
//MISC functions
function cursorChange(x){
if (x.innerHTML != ''){
x.style.cursor = 'pointer'
}
}
//get Key from object value function
function geyKey(object, value) {
return String(Object.keys(object).find(key => object[key] === value));
}
function musicToggle(){
let music = document.getElementById('music')
let button = document.getElementById('musicBtn')
if (music.style.display == 'none'){
music.style.display = 'flex'
button.innerHTML = 'Close music options'
}else{
music.style.display = 'none'
button.innerHTML = 'Music options'
}
}
function podcastToggle(){
let podcast = document.getElementById('podcast')
let podcastBtn = document.getElementById('podcastBtn')
if (podcast.style.display == 'none'){
podcast.style.display = 'flex'
podcastBtn.innerHTML = 'Close podcast options'
}else{
podcast.style.display = 'none'
podcastBtn.innerHTML = 'Podcast options'
}
}
//chess piece MOVES below
//knight movement function. Current pos should be an array pos. Example: knightMovement(currentPos[0,1]).
function knightMovement(currentPos){
let positions = []
//These are coordinate transformations. Knight can transform by any of these.
let moves = [
[-2,+1],
[-1,+2],
[-2,-1],
[-1,-2],
[+1,+2],
[+2,+1],
[+2,-1],
[-1,-2],
[+1,-2]
]
for (item in moves){
positions.push([(moves[item][0]+currentPos[0]), (moves[item][1]+currentPos[1])])
}
//returns array of all possible positions the knight can move to when called.
console.log(positions)
return positions
}
//pawns
function pawnMovement(currentPos, type){
let positions = []
if (type == 'black'){
//moves for pawn. Transformations on the array.
let moves = [
[+1,0]
]
//check if pawn is on first move by checking if the white index is 1 (second row)
if (currentPos[0] == 1){
moves.push([+2,0])
}
for (item in moves){
positions.push([(moves[item][0]+currentPos[0]), (moves[item][1]+currentPos[1])])
}
console.log(positions)
return positions
}else if (type == 'white'){
//moves for pawn. Transformations on the array.
let moves = [
[-1,0]
]
//check if pawn is on first move by checking if the black index is 6 (7th row)
if (currentPos[0] == 6){
moves.push([-2,0])
}
for (item in moves){
positions.push([(moves[item][0]+currentPos[0]), (moves[item][1]+currentPos[1])])
}
console.log(positions)
return positions
}
}
//rook movement
function rookMovement(currentPos){
let positions = []
//moves. Transformations (yet again!)
let moves = [
]
//loops until 7. 7 is the maximum index for x and y axis on the board. pushes to moves array.
for (let i = 0; i <8; i++){
moves.push([+i,0])
moves.push([-i,0])
moves.push([0,+i])
moves.push([0,-i])
}
for (item in moves){
positions.push([(moves[item][0]+currentPos[0]), (moves[item][1]+currentPos[1])])
}
return positions
}
function settings(){
if (document.getElementById('settings')){
document.getElementById('settings').remove()
}else{
let settingDom = document.createElement('div')
settingDom.setAttribute('id', 'settings')
settingDom.innerHTML = 'Light side: '
settingDom.style.fontSize = '20px'
let inputC = document.createElement('input')
inputC.setAttribute('id', 'lightColor')
inputC.setAttribute('type', 'color')
settingDom.appendChild(inputC)
document.getElementById('settings-container').appendChild(settingDom)
}
}
//Bishop moves
function bishopMovement(currentPos){
let positions = []
let moves = [
[+1,+1],
[+2,+2],
[+3,+3],
[+4,+4],
[+5,+5],
[+6,+6],
[+7,+7],
[+1,-1],
[+2,-2],
[+3,-3],
[+4,-4],
[+5,-5],
[+6,-6],
[+7,-7],
[-1,-1],
[-2,-2],
[-3,-3],
[-4,-4],
[-5,-5],
[-6,-6],
[-7,-7],
[-1,+1],
[-2,+2],
[-3,+3],
[-4,+4],
[-5,+5],
[-6,+6],
[-7,+7]
]
for (item in moves){
positions.push([(moves[item][0]+currentPos[0]), (moves[item][1]+currentPos[1])])
}
return positions
}
//queen moves
function queenMovement(currentPos){
let positions = []
//first bit (here) is the same as bishop. Bishop moves:
let moves = [
[+1,+1],
[+2,+2],
[+3,+3],
[+4,+4],
[+5,+5],
[+6,+6],
[+7,+7],
[+1,-1],
[+2,-2],
[+3,-3],
[+4,-4],
[+5,-5],
[+6,-6],
[+7,-7],
[-1,-1],
[-2,-2],
[-3,-3],
[-4,-4],
[-5,-5],
[-6,-6],
[-7,-7],
[-1,+1],
[-2,+2],
[-3,+3],
[-4,+4],
[-5,+5],
[-6,+6],
[-7,+7]
]
//rook moves.
for (let i = 0; i <8; i++){
moves.push([+i,0])
moves.push([-i,0])
moves.push([0,+i])
moves.push([0,-i])
}
for (item in moves){
positions.push([(moves[item][0]+currentPos[0]), (moves[item][1]+currentPos[1])])
}
return positions
}
//king
function kingMovement(currentPos){
let positions = []
let moves = [
[+1,0],
[-1,0],
[0,+1],
[0,-1],
//diag moves
[+1,+1],
[-1,-1],
[+1,-1],
[-1,+1]
]
for (item in moves){
positions.push([(moves[item][0]+currentPos[0]), (moves[item][1]+currentPos[1])])
}
return positions
}
//capturing pieces logic
createBoard()
checkPoints()
let blackTaken = document.createElement('h1')
blackTaken.setAttribute('id', 'black-taken')
document.getElementById('board-container').appendChild(blackTaken)