-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
312 lines (250 loc) · 8.76 KB
/
script.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
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
//Default variables
var board = {
properties: {
teams: 2,
size: 64,
numOfLight: 12,
numOfDark: 12,
turn: 0 //0 as in Team 0 -> Light
},
pieces: [],
teams: [
{
id: 0,
name: "Team 1",
color: "cyan"
},
{
id: 1,
name: "Team 2",
color: "red"
}
],
validator: new Validator()
}
//Two Last clicked pieces
var clickedPieces = []
//Check if needed and/or beneficial later
class Piece
{
constructor(team, row, column, doubled)
{
this.team = team
this.row = row
this.column = column
this.doubled = doubled
}
}
initGame()
function initGame()
{
createDefaultPieces()
createBoard()
updateBoard()
}
/*
Initialize default pieces (default checkers pieces)
!TODO Should be customizable later
!TODO Can be simplified, 'shifted' no longer needed -> use even and uneven sum of row+column
*/
function createDefaultPieces()
{
//Clear Pieces
board.pieces = []
let board_size_root = Math.sqrt(board.properties.size)
let shifted = true
//Row loop
for (let i = 0; i < board_size_root; i++) {
//Column loop
for (let j = 0; j < board_size_root; j++) {
if (shifted) {
//If current row is in the light team and on valid ground
if (i <= 2 && j % 2 != 0) {
board.pieces.push({ team_id: 0, row: i, column: j, doubled: false })
}
//If current row is in the dark team and on valid ground
else if (i >= 5 && j % 2 != 0) {
board.pieces.push({ team_id: 1, row: i, column: j, doubled: false })
}
} else {
//If current row is in the light team and on valid ground
if (i <= 2 && j % 2 == 0) {
board.pieces.push({ team_id: 0, row: i, column: j, doubled: false })
}
//If current row is in the dark team and on valid ground
else if (i >= 5 && j % 2 == 0) {
board.pieces.push({ team_id: 1, row: i, column: j, doubled: false })
}
}
}
shifted = !shifted
}
}
//Puts all existing chips in the right place on the board
//TODO Simplify according to new "teams"-definition in boards obj & move the debug section with "output" to debug functions
function updateBoard()
{
let output = "\n"
let board_size_root = Math.sqrt(board.properties.size)
let pieces = board.pieces
//clear existing pieces
document.querySelectorAll(".chip").forEach(chip =>
{
chip.parentNode.innerHTML = ""
})
//Row loop
for (let i = 0; i < board_size_root; i++) {
let current_row_pieces = pieces.filter(piece => piece.row == i)
//Column loop
for (let j = 0; j < board_size_root; j++) {
let current_piece = current_row_pieces.find(piece => piece.column == j) || null
if (current_piece) {
try {
if (current_piece.team_id == 0) {
output += "o"
document.querySelector(`td[data-row='${i}'][data-column='${j}']`).insertAdjacentElement("beforeend", getNewPieceElement("cyan"))
} else {
output += "x"
document.querySelector(`td[data-row='${i}'][data-column='${j}']`).insertAdjacentElement("beforeend", getNewPieceElement("red"))
}
} catch (e) {
console.error(e)
}
}
else {
output += " "
}
}
output += "\n"
}
return output
}
//Create HTML board as table and mark every cell with the according attributes
function createBoard()
{
let game = document.querySelector("#game")
let table = document.createElement("table")
let board_size_root = Math.sqrt(board.properties.size)
//Row loop
for (let i = 0; i < board_size_root; i++) {
let currentTableRow = document.createElement("tr")
//Column Loop
for (let j = 0; j < board_size_root; j++) {
let currentCell = document.createElement("td")
currentCell.dataset.row = i
currentCell.dataset.column = j
currentCell.classList.add("piece")
currentCell.addEventListener("click", (event) => clickedPieceHandler(event))
//Color background of cell in checked pattern thorugh even or uneven number of summed row and column values
if ((i + j) % 2 == 0) {
currentCell.style.backgroundColor = "white"
} else {
currentCell.style.backgroundColor = "black"
}
currentTableRow.insertAdjacentElement("beforeend", currentCell)
}
table.insertAdjacentElement("beforeend", currentTableRow)
}
game.innerHTML = ""
game.insertAdjacentElement("beforeend", table)
}
//Create a playable figurine aka chip
function getNewPieceElement(color)
{
let piece = document.createElement("div")
piece.style.backgroundColor = color
piece.classList.add("chip")
return piece;
}
//TODO Needs rework after layout change
function movePiece(from, to)
{
//Get relevant pieces (if existant)
let source = board.pieces.find(piece => piece.row == from.row && piece.column == from.column) || null
let target = board.pieces.find(piece => piece.row == to.row && piece.column == to.column) || null
//Abort if source does not contain a moveable piece OR move is invalid
//TODO Send message to Notification engine
if (source == null || !board.validator.validateMove(source, target || to)) {
removeAllHighlights()
console.log("Invalid move.")
return
}
//Log move (TODO link to notification engine)
console.log(`${board.teams[source.team_id].name} moves from %c[Row: ${from.row}, Column: ${from.column}] %cto %c[Row: ${to.row}, Column: ${to.column}]`, "color:red", "color:white", "color:green")
//lol
// console.log('%c\uD83D\uDE09 Giant Rainbow Text!',
// 'font-weight:bold; font-size:50px;color:red; ' +
// 'text-shadow:3px 3px 0 red,6px 6px 0 orange,9px 9px 0 yellow, ' +
// '12px 12px 0 green,15px 15px 0 blue,18px 18px 0 indigo,21px 21px 0 violet');
//Move
source.row = to.row
source.column = to.column
}
//Event handler for clicking on any cell.
function clickedPieceHandler(event)
{
// Get relevant information about clicked Piece
let clickedRow = (event.target.dataset.row || event.target.parentNode.dataset.row)
let clickedColumn = (event.target.dataset.column || event.target.parentNode.dataset.column)
let clickedCell = board.pieces.find(piece => piece.row == clickedRow && piece.column == clickedColumn)
clickedCell = clickedCell == undefined ? { row: clickedRow, column: clickedColumn } : clickedCell
try {
//If the clicked Piece is the first clicked piece, push it in the clickedPieces Array
if (clickedPieces.length == 0) {
removeAllHighlights()
clickedPieces.push(clickedCell)
updateHighlightedPieces()
}
else { //Else, push it, print two clicked pieces to console, move the piece, reset the array and update screen
clickedPieces.push(clickedCell)
updateHighlightedPieces()
movePiece(clickedPieces[0], clickedPieces[1])
console.table(clickedPieces)
clickedPieces = []
updateBoard()
}
} catch (e) {
removeAllHighlights()
clickedPieces = []
console.error(e)
}
}
//Highlights any cell in the clickedPieces Array
function updateHighlightedPieces()
{
//Get html elements for currently clicked Pieces
let clickedPiecesHTML = []
for (let i = 0; i < clickedPieces.length; i++) {
clickedPiecesHTML.push(document.querySelector(`td[data-row='${clickedPieces[i].row}'][data-column='${clickedPieces[i].column}']`))
}
clickedPiecesHTML.forEach((piece =>
{
piece.classList.add("highlightedCell")
}))
}
//Remove highlights from all cells
function removeAllHighlights()
{
document.querySelectorAll(".highlightedCell").forEach(element =>
{
element.classList.remove("highlightedCell")
})
}
//Debug functions
function printBoardToConsole()
{
console.log(updateBoard())
}
//deprecated -> will be moved to validator
function isDirectDiagonalNeighbor(from, to)
{
return isXthDiagonalNeighbor(from, to, 1)
}
function isDirectJumpedDiagonalNeigbor(from, to)
{
return isXthDiagonalNeighbor(from, to, 2)
}
function isXthDiagonalNeighbor(from, to, distance)
{
let board_size_root = Math.sqrt(board.properties.size)
}