-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
327 lines (297 loc) · 12.2 KB
/
Copy pathmain.js
File metadata and controls
327 lines (297 loc) · 12.2 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
const PLAYER_LOOKUP = {
'0': '',
'1': 'rebels',
'-1': 'empire',
}
let turn
let winner
const messageEl = document.querySelector('h2')
const forfeitBtn = document.getElementById('forfeitBtn')
const spaces = document.querySelector('#board')
let spacesArr = [...document.querySelectorAll('#board > div')]
const renderMessage = () => {
if (winner === 'rebels') {
messageEl.innerText = "Winner: The Rebels!"
} else if (winner === 'empire') {
messageEl.innerText = "Winner: The Empire!"
}
else {
messageEl.innerText = `Move: The ${PLAYER_LOOKUP[turn]}`
}
}
const getCellId = (targetOld) => {
let parentId = targetOld.parentElement.id
console.log(parentId)
let positionArr = parentId.split('')
positionArr.shift()
positionArr.splice(1, 1)
let unparsedColIdx = positionArr[0]
console.log(unparsedColIdx)
let unparsedRowIdx = positionArr[1]
let colIdx = parseInt(unparsedColIdx, 10)
let rowIdx = parseInt(unparsedRowIdx, 10)
return [colIdx, rowIdx]
}
const getOffsetId = (colOffset, rowOffset) => {
return `c${colOffset}r${rowOffset}`;
};
const getOffsetCell = (colOffset, rowOffset) => {
const offsetId = getOffsetId(colOffset, rowOffset);
return document.getElementById(offsetId);
};
const highlightCell = (cell) => {
if (cell !== null && cell.firstChild === null) {
cell.style.backgroundColor = 'gold';
}
};
const checkMovePossibilityRebels = (targetOld, colIdx, rowIdx) => {
let possibilityArr = [];
let offsetCellNE = getOffsetCell(colIdx + 1, rowIdx + 1);
let offsetCellNW = getOffsetCell(colIdx - 1, rowIdx + 1);
let offsetCellSE = getOffsetCell(colIdx + 1, rowIdx - 1);
let offsetCellSW = getOffsetCell(colIdx - 1, rowIdx - 1);
if (offsetCellNE !== null) {
highlightCell(offsetCellNE);
possibilityArr.push(offsetCellNE);
if (offsetCellNE.firstChild !== null && offsetCellNE.firstChild.classList.contains('empire')) {
const jumpCellNE = getOffsetCell(colIdx + 2, rowIdx + 2);
if (jumpCellNE !== null) {
highlightCell(jumpCellNE);
possibilityArr.push(jumpCellNE);
}
}
}
if (offsetCellNW !== null) {
highlightCell(offsetCellNW);
possibilityArr.push(offsetCellNW);
if (offsetCellNW.firstChild !== null && offsetCellNW.firstChild.classList.contains('empire')) {
const jumpCellNW = getOffsetCell(colIdx - 2, rowIdx + 2)
if (jumpCellNW !== null) {
highlightCell(jumpCellNW);
possibilityArr.push(jumpCellNW)
}
}
}
if (targetOld !== null && targetOld.classList.contains('yoda')) {
if (offsetCellSE != null) {
highlightCell(offsetCellSE)
possibilityArr.push(offsetCellSE)
}
if (offsetCellSE.firstChild !== null && offsetCellSE.firstChild.classList.contains('empire')) {
const jumpCellSE = getOffsetCell(colIdx + 2, rowIdx - 2);
if (jumpCellSE !== null) {
highlightCell(jumpCellSE);
possibilityArr.push(jumpCellSE)
}
}
if (offsetCellSW !== null) {
highlightCell(offsetCellSW)
possibilityArr.push(offsetCellSW)
}
if (offsetCellSW.firstChild !== null && offsetCellSW.firstChild.classList.contains('empire')) {
const jumpCellSW = getOffsetCell(colIdx - 2, rowIdx - 2);
if (jumpCellSW !== null) {
highlightCell(jumpCellSW);
possibilityArr.push(jumpCellSW)
}
}
}
return possibilityArr
};
const checkMovePossibilityEmpire = (targetOld, colIdx, rowIdx) => {
let possibilityArr = [];
let offsetCellNE = getOffsetCell(colIdx - 1, rowIdx - 1);
let offsetCellNW = getOffsetCell(colIdx + 1, rowIdx - 1);
let offsetCellSE = getOffsetCell(colIdx - 1, rowIdx + 1);
let offsetCellSW = getOffsetCell(colIdx + 1, rowIdx + 1);
if (offsetCellNE !== null) {
highlightCell(offsetCellNE);
possibilityArr.push(offsetCellNE);
if (offsetCellNE.firstChild !== null && offsetCellNE.firstChild.classList.contains('rebels')) {
const jumpCellNE = getOffsetCell(colIdx - 2, rowIdx - 2);
if (jumpCellNE !== null) {
highlightCell(jumpCellNE);
possibilityArr.push(jumpCellNE);
}
}
}
if (offsetCellNW !== null) {
highlightCell(offsetCellNW);
possibilityArr.push(offsetCellNW);
if (offsetCellNW.firstChild !== null && offsetCellNW.firstChild.classList.contains('rebels')) {
const jumpCellNW = getOffsetCell(colIdx + 2, rowIdx - 2)
if (jumpCellNW !== null) {
highlightCell(jumpCellNW);
possibilityArr.push(jumpCellNW)
}
}
}
if (targetOld !== null && targetOld.classList.contains('vader')) {
if (offsetCellSE != null) {
highlightCell(offsetCellSE)
possibilityArr.push(offsetCellSE)
}
if (offsetCellSE.firstChild !== null && offsetCellSE.firstChild.classList.contains('rebels')) {
const jumpCellSE = getOffsetCell(colIdx - 2, rowIdx + 2);
if (jumpCellSE !== null) {
highlightCell(jumpCellSE);
possibilityArr.push(jumpCellSE)
}
}
if (offsetCellSW !== null) {
highlightCell(offsetCellSW)
possibilityArr.push(offsetCellSW)
}
if (offsetCellSW.firstChild !== null && offsetCellSW.firstChild.classList.contains('empire')) {
const jumpCellSW = getOffsetCell(colIdx + 2, rowIdx + 2);
if (jumpCellSW !== null) {
highlightCell(jumpCellSW);
possibilityArr.push(jumpCellSW)
}
}
}
return possibilityArr
}
const checkMove = (targetOld) => {
const cellId = getCellId(targetOld)
const colIdx = cellId[0]
const rowIdx = cellId[1]
if (PLAYER_LOOKUP[turn] === 'rebels') {
let possibilityArr = checkMovePossibilityRebels(targetOld, colIdx, rowIdx)
return possibilityArr
}
else if (PLAYER_LOOKUP[turn] === 'empire') {
let possibilityArr = checkMovePossibilityEmpire(targetOld, colIdx, rowIdx)
return possibilityArr
}
}
const changeToBlack = () => {
const blackDivs = document.querySelectorAll('.black')
blackDivs.forEach((div) => {
div.style.backgroundColor = 'black'
})
}
const jumpPlayer = (targetOld, targetNew) => {
console.log(targetNew)
const oldIdxArr = getCellId(targetOld)
const colIdxOld = oldIdxArr[0]
const rowIdxOld = oldIdxArr[1]
const parentIdNew = targetNew.id
const positionArrNew = parentIdNew.split('')
positionArrNew.shift()
positionArrNew.splice(1, 1)
const unparsedColIdxNew = positionArrNew[0]
const unparsedRowIdxNew = positionArrNew[1]
const colIdxNew = parseInt(unparsedColIdxNew, 10)
const rowIdxNew = parseInt(unparsedRowIdxNew, 10)
let rebelsTurnNEJumpedCellPosition = getOffsetCell(colIdxOld + 1, rowIdxOld + 1);
let rebelsTurnNWJumpedCellPosition = getOffsetCell(colIdxOld - 1, rowIdxOld + 1);
let rebelsTurnSEJumpedCellPosition = getOffsetCell(colIdxOld + 1, rowIdxOld - 1);
let rebelsTurnSWJumpedCellPosition = getOffsetCell(colIdxOld - 1, rowIdxOld - 1);
let empireTurnNEJumpedCellPosition = getOffsetCell(colIdxOld - 1, rowIdxOld - 1);
let empireTurnNWJumpedCellPosition = getOffsetCell(colIdxOld + 1, rowIdxOld - 1);
let empireTurnSEJumpedCellPosition = getOffsetCell(colIdxOld - 1, rowIdxOld + 1);
let empireTurnSWJumpedCellPosition = getOffsetCell(colIdxOld + 1, rowIdxOld + 1);
if (PLAYER_LOOKUP[turn]==='rebels') {
if (colIdxNew === colIdxOld + 2 && rowIdxNew === rowIdxOld + 2) {
rebelsTurnNEJumpedCellPosition.removeChild(rebelsTurnNEJumpedCellPosition.firstChild)
return rebelsTurnNEJumpedCellPosition
} else if (colIdxNew === colIdxOld -2 && rowIdxNew === rowIdxOld + 2) {
rebelsTurnNWJumpedCellPosition.removeChild(rebelsTurnNWJumpedCellPosition.firstChild)
return rebelsTurnNWJumpedCellPosition
} else if (colIdxNew === colIdxOld + 2 && rowIdxNew === rowIdxOld - 2) {
rebelsTurnSEJumpedCellPosition.removeChild(rebelsTurnSEJumpedCellPosition.firstChild)
return rebelsTurnSEJumpedCellPosition
} else if (colIdxNew === colIdxOld - 2 && rowIdxNew === rowIdxOld - 2) {
rebelsTurnSWJumpedCellPosition.removeChild(rebelsTurnSWJumpedCellPosition.firstChild)
return rebelsTurnSWJumpedCellPosition
}
} else if (PLAYER_LOOKUP[turn]==='empire') {
if (colIdxNew === colIdxOld - 2 && rowIdxNew === rowIdxOld - 2) {
empireTurnNEJumpedCellPosition.removeChild(empireTurnNEJumpedCellPosition.firstChild)
return empireTurnNEJumpedCellPosition
} else if (colIdxNew === colIdxOld + 2 && rowIdxNew === rowIdxOld - 2) {
empireTurnNWJumpedCellPosition.removeChild(empireTurnNWJumpedCellPosition.firstChild)
return empireTurnNWJumpedCellPosition
} else if (colIdxNew === colIdxOld - 2 && rowIdxNew === rowIdxOld + 2) {
empireTurnSEJumpedCellPosition.removeChild(empireTurnSEJumpedCellPosition.firstChild)
return empireTurnSEJumpedCellPosition
} else if (colIdxNew === colIdxOld + 2 && rowIdxNew === rowIdxOld + 2) {
empireTurnSWJumpedCellPosition.removeChild(empireTurnSWJumpedCellPosition.firstChild)
return empireTurnSWJumpedCellPosition
}
}
}
let targetOld = null;
const handleMove = (event) => {
let targetNew = null;
if(targetOld !== null) {
if(!event.target.classList.contains('empire') || !event.target.classList.contains('rebels')) {
if(event.target.classList.contains('black') && event.target.firstChild === null){
let possibilityArr = checkMove(targetOld)
if (possibilityArr.includes(event.target)) {
targetNew = event.target
jumpPlayer(targetOld, targetNew)
targetNew.appendChild(targetOld).style.borderColor = 'white'
targetOld = null
getWinner()
turn *= -1
changeToBlack()
}
}
}
}
if((event.target.classList.contains('empire') && PLAYER_LOOKUP[turn]==='empire') || (event.target.classList.contains('rebels') && PLAYER_LOOKUP[turn]==='rebels')){
targetOld = event.target
checkKing(targetOld)
checkMove(targetOld)
targetOld.style.borderColor = 'gold'
}
render()
};
const getWinner = () => {
spacesArr = [...document.querySelectorAll('#board > div')]
const winnerArr = []
spacesArr.forEach((space) => {
if (space?.childNodes[0]?.classList.contains('rebels') || space?.childNodes[0]?.classList.contains('empire')) {
winnerArr.push(space)
}
})
const rebelWin = winnerArr.every(space => space?.childNodes[0]?.classList.contains('rebels'))
const empireWin = winnerArr.every(space => space?.childNodes[0]?.classList.contains('empire'))
winner = rebelWin ? 'rebels' : empireWin ? 'empire' : null
}
const checkKing = (targetOld) => {
let parentIdOld = targetOld.parentElement.id
let positionArrOld = parentIdOld.split('')
positionArrOld.shift()
positionArrOld.splice(1, 1)
let rowIdxOld = positionArrOld[1]
let parsedRowIdxOld = parseInt(rowIdxOld, 10)
if (PLAYER_LOOKUP[turn] ==='empire' && parsedRowIdxOld === 0) {
targetOld.classList.add('vader')
}
if (PLAYER_LOOKUP[turn] ==='rebels' && parsedRowIdxOld === 7) {
targetOld.classList.add('yoda')
}
}
const render = () => {
renderMessage()
}
const forfeit = () => {
if (PLAYER_LOOKUP[turn]==='empire') {
winner = "rebels"
} else if (PLAYER_LOOKUP[turn]==='rebels') {
winner = "empire"
}
render()
return winner
}
const init = () => {
turn = 1
winner = null
render ()
}
init()
spaces.addEventListener('click', handleMove)
forfeitBtn.addEventListener('click', forfeit)