-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
121 lines (103 loc) · 2.96 KB
/
app.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
const gameBoard = document.querySelector("#gameboard");
const playerDisplay = document.querySelector("#player");
const infoDisplay = document.querySelector("#info-display");
const width = 8;
let playerGo = 'black'
playerDisplay.textContent = 'black'
const startPieces = [
rook, knight, bishop, queen, king, bishop, knight ,rook,
pawn, pawn, pawn, pawn, pawn, pawn ,pawn, pawn,
'','','','','','','','',
'','','','','','','','',
'','','','','','','','',
'','','','','','','','',
pawn, pawn, pawn, pawn, pawn, pawn ,pawn, pawn,
rook, knight, bishop, queen, king, bishop, knight ,rook,
]
function createBoard(){
startPieces.forEach((startPiece, i) => {
const square = document.createElement('div')
square.classList.add('square')
square.innerHTML = startPiece
square.firstChild && square.firstChild.setAttribute('draggable', true)
square.setAttribute('square-id' ,i)
const row = Math.floor( (63 - i)/8 )+1
if (row % 2 == 0){
square.classList.add(i%2 == 0 ? "beige": "brown" )
}
else {
square.classList.add(i%2 == 0 ? "brown": "beige" )
}
if (i <= 15){
square.firstChild.firstChild.classList.add('black')
}
if (i >= 48){
square.firstChild.firstChild.classList.add('white')
}
gameBoard.append(square)
})
}
createBoard()
const allSquares = document.querySelectorAll(".square")
allSquares.forEach(square => {
square.addEventListener('dragstart', dragStart)
square.addEventListener('dragover', dragOver)
square.addEventListener('drop', dragDrop)
})
let startPositionId
let draggedElement
function dragStart(e){
startPositionId = e.target.parentNode.getAttribute('square-id')
draggedElement = e.target
}
function dragOver(e){
e.preventDefault()
}
function dragDrop(e){
e.stopPropagation()
const correctGo = draggedElement.firstChild.classList.contains(playerGo)
const opponentGo = playerGo === 'white' ? 'black' : 'white'
const taken = e.target.classList.contains('piece')
const valid = checkIfValid(e.target)
const takenByOpponent = e.target.firstChild?.classList.contains(opponentGo)
if (correctGo){
if (takenByOpponent && valid){
e.target.parentNode.append(draggedElement)
e.target.remove()
changePlayer()
return
}
if (taken){
infoDisplay.textContent = "Invalid move!"
setTimeout(()=>infoDisplay.textContent= "", 500)
return
}
if (valid){
e.target.append(draggedElement)
changePlayer()
return
}
}
}
function changePlayer(){
if (playerGo === "black"){
playerGo = "white"
playerDisplay.textContent = "black"
}
else {
playerGo = "black"
playerDisplay.textContent = "white"
}
}
function reverseIds(){
const allSquares = document.querySelectorAll(".square")
allSquares.forEach((square, i) =>
square.setAttribute('square-id',(width * width-1)-i))
}
function revertIds(){
const allSquares = document.querySelectorAll(".square")
allSquares.forEach((square, i) =>
square.setAttribute('square-id',i))
}
function checkIfValid(target){
}