-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
135 lines (111 loc) · 3.79 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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)
square.classList.add('beige')
// square.classList.add('beige')
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('#gameboard .square')
allSquares.forEach(square => {
square.addEventListener('dragStart', dragStart)
square.addEventListener('dragOver', dragOver)
square.addEventListener('drop', dragDrop)
})
let startPositionId
function dragStart(e) {
startPositionId = e.target.parentNode.getAttribute('square-id')
draggedElement = e.target
}
function dragOver(e) {
e.preventDefault()
}
function dragDrop(e) {
e.stopPropagation()
console.log('e.target', e.target)
const correctGo = draggedElement.firstChild.classList.contains(playerGo)
const taken = e.target.classList.contains('piece')
const valid = checkIfValid = (e.target)
const opponentGo = playerGo === 'white' ? 'black' : 'white'
const takenByOpponent = e.target.firstChild?.class.contains(opponentGo)
if (correctGo) {
//check this first
if (takenByOpponent && valid) {
e.target.parentNode.append(draggedElement)
e.target.remove()
changePlayer()
return
if (taken && !takenByOpponent) {
infoDisplay.textContent = 'Nope, try again!'
setTimeout(() => infoDisplay.textContent = '', 2000)
return
}
if (valid) {
e.target.append(draggedElement)
changePlayer()
return
}
}
}
//e.target.parentNode.append(draggedElemnt)
//e.target.append(draggedElement)
//e.target.remove()
changePlayer()
}
function changePlayer() {
if (playerGo === 'black') {
reverseIds()
playerGo = 'white'
playerDisplay.textContent = 'white'
} else {
revertIds()
playerGo = 'black'
playerDisplay.textContent = 'black'
}
}
function reverseIds() {
const allSquares = document.querySelectorAll('.square')
allSquares.forEach((square, i) =>
square.setAttribute('square-id', (width * width - 1) - i))
}
function revertIds() {
const allSwuares = document.querySelectorAll('.square')
allSquares.forEach((square, i) => square.setAttribute('square-id', i))
}
function checkValid(target) {
console.log(target)
const targetId = target.getAttribute('square-id') || target.parentNode.getAttribute('square-id')
}