-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
495 lines (391 loc) · 11.6 KB
/
script.js
File metadata and controls
495 lines (391 loc) · 11.6 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
let diceOne;
let diceTwo;
let player = 'black';
let color;
let checker;
let lane;
let turns = 0;
let moves = [];
function getCurrentColor( event ) {
return event.target.getAttribute( 'data-color' );
}
function getCurrentChecker( event ) {
return Number.parseInt( event.target.getAttribute( 'data-checker' ) );
}
function getCurrentLane( event ) {
return Number.parseInt(
event.target.parentElement.getAttribute( 'data-lane' )
);
}
function getTargetLane( currentPlayer, currentLane, dice ) {
let targetLane;
if ( 'white' === currentPlayer ) {
targetLane = currentLane + dice;
} else {
targetLane = currentLane - dice;
}
return targetLane;
}
function getTargetLaneCount( lane ) {
const data = document.querySelector( `[data-lane="${ lane }"]` );
if ( data ) return data.childElementCount;
}
function getTargetLaneColor( lane ) {
const data = document.querySelector( `[data-lane="${ lane }"]` );
const count = getTargetLaneCount( lane );
if ( count ) return data.firstElementChild.dataset.color;
}
function getThrownCheckerCount() {
const data = document.querySelector( `#thrown-${ player }` );
if ( data ) return data.childElementCount;
}
function getCurrentPlayer() {
return player;
}
function getCurrentOpponent() {
swapPlayer();
const opponent = getCurrentPlayer();
swapPlayer();
return opponent;
}
function swapPlayer() {
player = 'white' === player ? 'black' : 'white';
}
function swapChecker() {
const color = getCurrentPlayer();
const checkers = Array.prototype.slice.call(
document.querySelectorAll( '.checker' )
);
checkers.map(
( checker ) =>
( checker.style.cursor =
color === checker.dataset.color ? 'pointer' : 'initial' )
);
}
function resetTurns() {
turns = 0;
}
function hasGameStarted() {
return ! diceOne || ! diceTwo ? false : true;
}
/**
* Check if checker can be removed from the board.
*
* If it's the turn of the white player, the checker can be removed if all
* checkers are located in tethe lanes 19-24. If it's the turn of the black
* player, the checker can be removed if all checkers are located in the lanes
* 1-6.
*
* @param {string} currentPlayer The color of the current player.
* @return true or false depending if the checker can be removed.
*/
function canRemoveChecker(dice) {
// white == 25
// black == 0
if ( 'white' == currentPlayer ) {
console.log(currentLane + dice);
} else {
console.log(currentLane - dice);
}
console.log({currentLane});
console.log({dice});
console.log({currentChecker});
console.log({currentPlayer});
const checkers = Array.prototype.slice.call(
document.querySelectorAll( `[data-color="${ currentPlayer }"]` )
);
let allowed = true;
checkers.forEach( ( checker ) => {
const lane = checker.parentElement.dataset.lane;
if (
( 'white' == currentPlayer && 19 > lane ) ||
( 'black' == currentPlayer && 6 < lane )
) {
allowed = false;
}
} );
return allowed;
}
function checkMove( event, dice ) {
currentPlayer = getCurrentPlayer();
currentLane = getCurrentLane( event );
currentChecker = getCurrentChecker( event );
currentColor = getCurrentColor( event );
targetLane = getTargetLane( currentPlayer, currentLane, dice );
targetLaneCount = getTargetLaneCount( targetLane );
targetLaneColor = getTargetLaneColor( targetLane );
thrownCheckerCount = getThrownCheckerCount();
// Check if any checkers need to be brought back into the game.
if ( thrownCheckerCount ) {
return addChecker( currentChecker, dice );
}
// Check if checkers can be removed.
if ( ( 0 < targetLane || 24 > targetLane ) ) {
// Move the checker if it can be removed.
canRemoveChecker(dice);
// if ( canRemoveChecker( currentPlayer ) ) {
// return moveChecker( currentChecker, targetLane, currentLane );
// }
}
// Move checker if target lane is empty.
if ( ! targetLaneCount ) {
return moveChecker( currentChecker, targetLane, currentLane );
}
// Move checker if target lane contains only one checker of the other color
// and throw the other checker out.
if ( currentPlayer !== targetLaneColor && 1 === targetLaneCount ) {
return throwChecker( currentChecker, targetLane );
}
// Move checker if target lane contains less than 5 checkers of own color.
if ( currentPlayer === targetLaneColor && 5 > targetLaneCount ) {
return moveChecker( currentChecker, targetLane, currentLane );
}
// Show error message that checker cannot be moved to the target lane.
showCheckerMoveError();
}
function moveChecker( currentChecker, targetLane, currentLane, undo = false ) {
const state = {
currentChecker: currentChecker,
targetLane: targetLane,
currentLane: currentLane
}
const checker = document.querySelector(
`[data-checker="${ currentChecker }"]`
);
const lane = document.querySelector( `[data-lane="${ targetLane }"]` );
lane.appendChild( checker );
if ( ! undo ) {
moves.push(state);
turns++;
}
}
function throwChecker( currentChecker, targetLane ) {
const opponent = getCurrentOpponent();
const lane = document.querySelector( `[data-lane="${ targetLane }"]` );
const thrown = document.querySelector( `#thrown-${ opponent }` );
while ( lane.firstChild ) {
thrown.appendChild( lane.firstChild );
}
return moveChecker( currentChecker, targetLane, currentLane );
}
function isThrownChecker( currentChecker ) {
const thrown = Array.prototype.slice.call(
document.querySelectorAll( `#thrown-${ player } .checker` )
);
const result = thrown.filter( ( checker ) => {
return currentChecker == checker.dataset.checker;
} );
return result.length;
}
function addChecker( currentChecker, dice ) {
// If current checker is on the list of thrown checkers, bring it into the game.
if ( isThrownChecker( currentChecker ) ) {
const targetLane = 'white' === player ? 0 + dice : 25 - dice;
targetLaneCount = getTargetLaneCount( targetLane );
targetLaneColor = getTargetLaneColor( targetLane );
// Move checker if target lane is empty.
if ( ! targetLaneCount ) {
return moveChecker( currentChecker, targetLane, currentLane );
}
// Move checker if target lane contains only one checker of the other color
// and throw the other checker out.
if ( currentPlayer !== targetLaneColor && 1 === targetLaneCount ) {
return throwChecker( currentChecker, targetLane );
}
// Move checker if target lane contains less than 5 checkers of own color.
if ( currentPlayer === targetLaneColor && 5 > targetLaneCount ) {
return moveChecker( currentChecker, targetLane, currentLane );
}
// Show error message that checker cannot be moved to the target lane.
return showCheckerMoveError();
}
// If current checker is on the list of thrown checkers, show error message.
showCheckerThrownError();
}
function showCheckerMoveError() {
return showErrorMessage(
'This checker cannot be moved to the wanted lane!'
);
}
function showCheckerThrownError() {
return showErrorMessage( 'All thrown checkers need to be add first!' );
}
function showRollDiceError() {
return showErrorMessage( 'You need to roll the dice first!' );
}
function showNotYourTurnError() {
return showErrorMessage( 'It is not your turn yet!' );
}
function showNextPlayerError() {
return showErrorMessage( 'It is not your turn anymore!' );
}
function showErrorMessage( message ) {
const container = document.querySelector( '#message' );
container.innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
${ message }
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
`;
}
function getDiceHint() {
let hint;
if ( diceOne > diceTwo ) {
hint = `${ diceOne } + ${ diceTwo }`;
} else if ( diceOne < diceTwo ) {
hint = `${ diceTwo } + ${ diceOne }`;
} else {
hint = `${ diceOne } + ${ diceOne } + ${ diceOne } + ${ diceOne }`;
}
return ` Hint: ${ hint }`;
}
function getPlayerHint() {
return ` ${ player.toUpperCase() }`;
}
function rollDice() {
resetTurns();
swapPlayer();
swapChecker();
diceOne = Math.floor( Math.random() * 6 ) + 1;
diceTwo = Math.floor( Math.random() * 6 ) + 1;
// diceOne = 1;
// diceTwo = 2;
showDice( diceOne, diceTwo );
}
function swapDice() {
let temp = diceOne;
diceOne = diceTwo;
diceTwo = temp;
showDice( diceOne, diceTwo );
}
function getDiceFace( number ) {
switch ( number ) {
case 1:
return '1 <i class="fas fa-dice-one"></i>';
case 2:
return '2 <i class="fas fa-dice-two"></i>';
case 3:
return '3 <i class="fas fa-dice-three"></i>';
case 4:
return '4 <i class="fas fa-dice-four"></i>';
case 5:
return '5 <i class="fas fa-dice-five"></i>';
case 6:
return '6 <i class="fas fa-dice-six"></i>';
default:
return '0 <i class="fas fa-exclamation-square"></i>';
}
}
function showDice( diceOne, diceTwo ) {
const dice = document.querySelector( '#dice' );
const player = document.querySelector( '#player' );
const playerHint = getPlayerHint();
const diceOneFace = getDiceFace( diceOne );
const diceTwoFace = getDiceFace( diceTwo );
// Display dice.
dice.innerHTML = `${ diceOneFace } ${ diceTwoFace }`;
// Display player.
player.innerHTML = `${ playerHint }`;
}
function resetMessage() {
const message = document.querySelector( '#message' );
message.innerText = '';
}
function getStats() {
const statsWhite = document.querySelector( '#stats-white' );
const statsBlack = document.querySelector( '#stats-black' );
statsWhite.innerHTML = getStatsByPlayer( 'white' );
statsBlack.innerHTML = getStatsByPlayer( 'black' );
}
function undoMove() {
if ( 0 == turns ) return;
const move = moves[moves.length - 1];
moves.pop();
turns--;
return moveChecker( move.currentChecker, move.currentLane, move.targetLane, true );
}
function getStatsByPlayer( player ) {
return `
${ getActiveChecker( player ) } <br>
${ getThrownChecker( player ) } <br>
${ getFinishedChecker( player ) } <br>
${ getPipCount( player ) } <br>
`;
}
function getActiveChecker( player ) {
const count = document.querySelectorAll(`.lane .${ player }`).length;
return `Active: ${ count }`;
}
function getThrownChecker( player ) {
const count = document.querySelectorAll(`.thrown .${ player }`).length;
return `Thrown: ${ count }`;
}
function getFinishedChecker( player ) {
const count = document.querySelectorAll(`.finished .${ player }`).length;
return `Finished: ${ count }`;
}
function getPipCount( player ) {
const checker = document.querySelectorAll(`.lane .${ player }`);
let count = 0;
checker.forEach( element => {
const lane = Number.parseInt(element.parentNode.dataset.lane);
const multiplier = 'white' === player ? 24 - lane + 1 : lane;
count += multiplier;
});
return `Pip Count: ${ count }`;
}
document.addEventListener(
'click',
function( event ) {
resetMessage();
if ( event.target.classList.contains( 'checker' ) ) {
if ( ! hasGameStarted() ) {
return showRollDiceError();
}
if ( getCurrentOpponent() === getCurrentColor( event ) ) {
return showNotYourTurnError();
}
if ( diceOne === diceTwo ) {
switch ( turns ) {
case 0:
case 1:
case 2:
case 3:
checkMove( event, diceOne );
break;
default:
showNextPlayerError();
}
} else {
switch ( turns ) {
case 0:
checkMove( event, diceOne );
break;
case 1:
checkMove( event, diceTwo );
break;
default:
showNextPlayerError();
}
}
}
if ( event.target.classList.contains( 'roll' ) ) {
moves = [];
rollDice();
}
if ( event.target.classList.contains( 'swap' ) ) {
swapDice();
}
if ( event.target.classList.contains( 'undo' ) ) {
undoMove();
}
if ( event.target.classList.contains( 'reset' ) ) {
location.reload();
}
getStats();
},
false
);
getStats();