Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
1428 lines (1313 loc) · 56.9 KB
/
Copy pathscript.js
File metadata and controls
1428 lines (1313 loc) · 56.9 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/**
* @file script.js
* @description The core logic and controller layer for the Checkers Web Application.
* This file implements a Model-View-Controller (MVC) architectural pattern to separate
* game rules, user interface rendering, artificial intelligence, and audio feedback.
*
* It utilizes modern JavaScript (ES6+) features including Classes, Arrow Functions,
* and the Web Audio API for a rich, interactive user experience without external dependencies.
*
* @module CheckersGame
* @requires window.AudioContext
* @requires window.requestAnimationFrame
*
* @author John S. Garvey
* @copyright 2026 John S. Garvey
* @date January 2026
*/
// ==========================================
// Constants & Configuration
// ==========================================
/**
* The dimension of the game board (8x8 grid).
* @constant {number} BOARD_SIZE
* @default 8
*/
const BOARD_SIZE = 8;
/**
* Numeric representation of board pieces and states.
* @readonly
* @enum {number} PIECES
*/
const PIECES = { EMPTY: 0, RED: 1, BLACK: 2, RED_KING: 3, BLACK_KING: 4 };
/**
* Text strings used throughout the user interface for localization and easy updates.
* @readonly
* @enum {string} LABELS
*/
const LABELS = {
TITLE: "John Garvey's<br>Checkers Game",
ME: "Me",
YOU: "You",
RED: "Red",
BLACK: "Black",
JOHN: "John",
BTN_DISABLE: "Disable NPC (Me)",
BTN_ENABLE: "Enable NPC (Me)",
BTN_RESET: "Reset Game",
BTN_RESET_SCORE: "Reset Score",
BTN_HELP: "Help",
SCOREBOARD: "SCOREBOARD",
WIN_YOU: "You Win!",
WIN_JOHN: "John Wins!",
STATUS_YOUR: "Your Turn",
STATUS_MY: "My Turn",
STATUS_RED: "Red's Turn",
STATUS_BLACK: "Black's Turn",
STATUS_STILL_YOUR: "Still Your Turn",
STATUS_STILL_MY: "Still My Turn",
STATUS_STILL_RED: "Still Red's Turn",
STATUS_STILL_BLACK: "Still Black's Turn"
};
/**
* Color palette and visual configuration settings for the application.
* @readonly
* @enum {string|Array<string>} THEME
*/
const THEME = {
SELECTION: '#ffeb3b',
SCORE_DIGIT: '#ff0000',
SCORE_BG: '#000',
SCORE_BORDER: '#333',
SCORE_CONTAINER_BG: '#111',
SCORE_CONTAINER_BORDER: '#333',
SCORE_TEXT: '#fff',
SCORE_TITLE_BORDER: '#555',
BUTTON_START: '#444',
BUTTON_END: '#222',
BUTTON_BORDER: '#555',
BUTTON_HOVER_START: '#555',
BUTTON_HOVER_END: '#333',
RESET_SCORE_BTN_BG: '#333',
RESET_SCORE_BTN_BORDER: '#555',
RESET_SCORE_BTN_HOVER: '#444',
RESET_SCORE_BTN_TEXT: '#fff',
PROFILE_BORDER_ACTIVE: 'black',
PROFILE_BORDER_INACTIVE: 'white',
CAPTURED_BG: 'rgba(0,0,0,0.15)',
CAPTURED_BORDER: '#555',
SOCIAL_ICON: 'black',
SOCIAL_ICON_OPACITY: '0.6',
SOCIAL_ICON_HOVER_OPACITY: '1',
CONFETTI: [
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',
'#03a9f4', '#00bcd4', '#009688', '#4caf50', '#8bc34a', '#cddc39',
'#ffeb3b', '#ffc107', '#ff9800', '#ff5722'
]
};
/**
* CSS class names used for DOM manipulation and styling.
* @readonly
* @enum {string} CSS_CLASSES
*/
const CSS_CLASSES = {
SQUARE: 'square',
LIGHT: 'light',
DARK: 'dark',
PIECE: 'piece',
RED: 'red',
BLACK: 'black',
KING: 'king',
SELECTED: 'selected'
};
/**
* Configuration settings for gameplay timing and visual effects.
* @readonly
* @type {Object<string, number>} GAME_CONFIG
*/
const GAME_CONFIG = {
CPU_MOVE_DELAY: 500,
WINNER_DISPLAY_TIME: 3000,
CONFETTI_COUNT: 300,
NOTE_DELAY: 100
};
/**
* Configuration parameters for the Web Audio API synthesizer.
* @readonly
* @type {Object} AUDIO_CONFIG
*/
const AUDIO_CONFIG = {
MOVE: {
type: 'triangle', freq: 150, duration: 0.1,
volStart: 0.3, volEnd: 0.01, freqEnd: 40
},
CAPTURE: {
type: 'triangle', freq: 400, duration: 0.15,
volStart: 0.2, volEnd: 0.01, freqEnd: 100
},
KING_NOTES: [523.25, 659.25, 783.99, 1046.50],
WIN_NOTES: [523.25, 659.25, 783.99, 1046.50, 783.99, 1046.50]
};
/**
* SVG path data for social icons.
* @readonly
* @enum {string} ICONS
*/
const ICONS = {
LINKEDIN: `M19 3a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h14m-.5
15.5v-5.3a3.26 3.26 0 0 0-3.26-3.26c-.85 0-1.84.52-2.32 1.3v-1.11h-2.79v8.37h2.79v-4.93c0-.77.62-1.4
1.39-1.4a1.4 1.4 0 0 1 1.4 1.4v4.93h2.79M6.88 8.56a1.68 1.68 0 0 0 1.68-1.68c0-.93-.75-1.69-1.68-1.69a1.69
1.69 0 0 0-1.69 1.69c0 .93.76 1.68 1.69 1.68m1.39 9.94v-8.37H5.5v8.37h2.77z`,
GITHUB: `M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577
0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633
17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305
3.495.998.108-.776.419-1.305.763-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38
1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405
1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84
1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015
3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12`,
CODE: "M9.4 16.6L4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4zm5.2 0l4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4z",
GLOBE: `M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1
17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2
2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1
0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z`
};
/**
* Data structure for profile social media links and their SVG icons.
* @readonly
* @type {Array<{url: string, path: string}>} SOCIAL_LINKS
*/
const SOCIAL_LINKS = [
{ label: "LinkedIn", url: "https://www.linkedin.com/in/john-s-garvey/", path: ICONS.LINKEDIN },
{ label: "GitHub Profile", url: "https://github.com/jgarvey928", path: ICONS.GITHUB },
{ label: "GitHub Repo", url: "https://github.com/jgarvey928/Checkers_Web_Game", path: ICONS.CODE },
{ label: "Portfolio", url: "https://jgarvey928.github.io/jsgarveyportfolio.io/", path: ICONS.GLOBE }
];
// ==========================================
// Utility Functions
// ==========================================
/**
* Creates a new DOM element with the specified configuration and appends it to a parent.
* This utility function abstracts the verbose DOM API for cleaner component creation.
*
* @param {string} tag - The HTML tag name to create (e.g., 'div', 'button').
* @param {HTMLElement|null} parent - The parent DOM element to append the new element to. If null, the element is not appended.
* @param {Object} [options={}] - Configuration options for the element.
* @param {string} [options.className] - CSS class name(s) to apply.
* @param {string} [options.text] - Text content to set for the element.
* @param {string} [options.cssText] - Inline CSS styles to apply.
* @param {Object.<string, string>} [options.attrs] - Key-value pairs of HTML attributes to set (e.g., { href: '#', target: '_blank' }).
* @returns {HTMLElement} The newly created DOM element.
*/
function createElement(tag, parent, options = {}) {
const el = document.createElement(tag);
if (options.className) el.className = options.className;
if (options.text) el.textContent = options.text;
if (options.cssText) el.style.cssText = options.cssText;
if (options.attrs) {
Object.entries(options.attrs).forEach(([key, value]) => el.setAttribute(key, value));
}
if (parent) parent.appendChild(el);
return el;
}
/**
* Creates a styled button element with hover effects and click handling.
* Encapsulates the standard button styling and interaction logic used throughout the UI.
*
* @param {string} text - The text label to display on the button.
* @param {Function} [onClick] - Optional callback function to execute when the button is clicked.
* @returns {HTMLButtonElement} The configured button element.
*/
function createButton(text, onClick) {
const btn = createElement('button', null, {
text: text,
cssText: `
display: block;
margin: 2px auto;
padding: 10px 5px;
width: 185px;
box-sizing: border-box;
font-size: 14px;
font-weight: bold;
cursor: pointer;
background: linear-gradient(to bottom, ${THEME.BUTTON_START}, ${THEME.BUTTON_END});
color: white;
border: 2px solid ${THEME.BUTTON_BORDER};
border-radius: 25px;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
transition: all 0.2s ease;
`
});
btn.addEventListener('mouseover', () => {
btn.style.transform = 'translateY(-2px)';
btn.style.boxShadow = '0 6px 12px rgba(0,0,0,0.4)';
btn.style.background = `linear-gradient(to bottom, ${THEME.BUTTON_HOVER_START}, ${THEME.BUTTON_HOVER_END})`;
});
btn.addEventListener('mouseout', () => {
btn.style.transform = 'translateY(0)';
btn.style.boxShadow = '0 4px 6px rgba(0,0,0,0.3)';
btn.style.background = `linear-gradient(to bottom, ${THEME.BUTTON_START}, ${THEME.BUTTON_END})`;
});
if (onClick) btn.addEventListener('click', onClick);
return btn;
}
/**
* Determines if a given piece belongs to the Red player.
*
* @param {PIECES} piece - The numeric representation of the piece.
* @returns {boolean} True if the piece is RED or RED_KING; otherwise false.
*/
const isRedPiece = (piece) => piece === PIECES.RED || piece === PIECES.RED_KING;
/**
* Determines if a given piece is a King (promoted).
*
* @param {PIECES} piece - The numeric representation of the piece.
* @returns {boolean} True if the piece is RED_KING or BLACK_KING; otherwise false.
*/
const isKing = (piece) => piece === PIECES.RED_KING || piece === PIECES.BLACK_KING;
/**
* Validates if the given coordinates are within the game board boundaries.
*
* @param {number} row - The row index to check.
* @param {number} col - The column index to check.
* @returns {boolean} True if the position is within the [0, BOARD_SIZE-1] range.
*/
const isValidPos = (row, col) => row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
/**
* Retrieves the allowed movement directions for a specific piece type.
* Regular pieces move forward diagonally; Kings move in all diagonal directions.
*
* @param {PIECES} piece - The numeric representation of the piece.
* @returns {Array<{rowOffset: number, colOffset: number}>} Array of direction objects.
*/
function getAllowedDirections(piece) {
const directions = [];
if (isRedPiece(piece) || isKing(piece)) {
directions.push({rowOffset: -1, colOffset: -1}, {rowOffset: -1, colOffset: 1});
}
if (!isRedPiece(piece) || isKing(piece)) {
directions.push({rowOffset: 1, colOffset: -1}, {rowOffset: 1, colOffset: 1});
}
return directions;
}
// ==========================================
// Controllers
// ==========================================
/**
* AudioController
* Manages the Web Audio API context, synthesizes sound effects, and handles playback.
* This class abstracts the complexity of the AudioContext and provides high-level methods
* for game events. It handles browser compatibility and auto-resuming of suspended contexts.
*/
class AudioController {
/**
* Initializes the AudioController and attempts to create an AudioContext.
* Logs a warning if the Web Audio API is not supported by the browser.
*/
constructor() {
this.audioCtx = null;
try {
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
} catch (e) {
console.warn("Web Audio API is not supported in this browser.");
}
}
/**
* Synthesizes and plays a tone using an oscillator and gain node.
* This method creates a temporary oscillator for each sound, allowing for overlapping effects.
*
* @param {OscillatorType} type - The oscillator waveform type.
* @param {number} freq - The starting frequency in Hz.
* @param {number} duration - The duration of the tone in seconds.
* @param {number} volStart - The starting volume (gain).
* @param {number} volEnd - The ending volume (gain) for fade-out.
* @param {number|null} [freqEnd=null] - Optional ending frequency for frequency ramping effects (e.g., slides).
*/
playTone(type, freq, duration, volStart = 0.1, volEnd = 0.01, freqEnd = null) {
if (!this.audioCtx) return;
try {
const osc = this.audioCtx.createOscillator();
const gain = this.audioCtx.createGain();
osc.connect(gain);
gain.connect(this.audioCtx.destination);
osc.type = type;
osc.frequency.setValueAtTime(freq, this.audioCtx.currentTime);
if (freqEnd) osc.frequency.exponentialRampToValueAtTime(freqEnd, this.audioCtx.currentTime + duration);
gain.gain.setValueAtTime(volStart, this.audioCtx.currentTime);
gain.gain.exponentialRampToValueAtTime(volEnd, this.audioCtx.currentTime + duration);
osc.start(this.audioCtx.currentTime);
osc.stop(this.audioCtx.currentTime + duration);
} catch (error) {
console.error("Error playing tone:", error);
}
}
/**
* Triggers a predefined high-level sound effect based on game events.
* Handles resuming the AudioContext if it was suspended by the browser.
*
* @param {'move'|'capture'|'king'|'win'} type - The type of sound effect to play.
*/
playSound(type) {
if (!this.audioCtx) return;
if (this.audioCtx.state === 'suspended') {
this.audioCtx.resume().catch(e => console.warn("Audio resume failed:", e));
}
if (type === 'move') {
const cfg = AUDIO_CONFIG.MOVE;
this.playTone(cfg.type, cfg.freq, cfg.duration, cfg.volStart, cfg.volEnd, cfg.freqEnd);
} else if (type === 'capture') {
const cfg = AUDIO_CONFIG.CAPTURE;
this.playTone(cfg.type, cfg.freq, cfg.duration, cfg.volStart, cfg.volEnd, cfg.freqEnd);
} else if (type === 'king') {
AUDIO_CONFIG.KING_NOTES.forEach((freq, i) => setTimeout(() => this.playTone('triangle', freq, 0.3, 0.05, 0.001), i * GAME_CONFIG.NOTE_DELAY));
} else if (type === 'win') {
AUDIO_CONFIG.WIN_NOTES.forEach((freq, i) => setTimeout(() => this.playTone('square', freq, 0.1, 0.1, 0.01), i * GAME_CONFIG.NOTE_DELAY));
}
}
}
/**
* AIController
* Implements the logic for the computer opponent (NPC).
* Uses a heuristic-based decision making process to select the optimal move
* from the set of valid moves available to the Black player.
*/
class AIController {
/**
* Creates an instance of the AIController.
* @param {Game} game - Reference to the main Game instance.
*/
constructor(game) {
this.game = game;
}
/**
* Determines the best possible move for the computer player.
* Uses a heuristic approach prioritizing promotions, multi-jumps, and then captures.
* @returns {{fromRow: number, fromCol: number, toRow: number, toCol: number}|null} The selected move object or null if no moves are available.
*/
makeMove() {
const moves = this.getAllCPUMoves();
if (moves.length === 0) return null;
const bestMoves = this.getBestCPUMoves(moves);
if (bestMoves.length > 0) {
return bestMoves[Math.floor(Math.random() * bestMoves.length)];
}
return null;
}
/**
* Retrieves all valid moves available to the computer (Black pieces).
* @returns {Array<{fromRow: number, fromCol: number, toRow: number, toCol: number}>} An array of valid move objects.
*/
getAllCPUMoves() {
if (this.game.chainJumpInProgress) {
return this.getMovesForPiece(this.game.selectedRow, this.game.selectedCol);
}
let moves = [];
for (let row = 0; row < BOARD_SIZE; row++) {
for (let col = 0; col < BOARD_SIZE; col++) {
const piece = this.game.board[row][col];
if (piece === PIECES.BLACK || piece === PIECES.BLACK_KING) {
moves = moves.concat(this.getMovesForPiece(row, col));
}
}
}
return moves;
}
/**
* Filters and prioritizes moves based on game strategy.
* Priority: King Promotion > Multi-Jumps > Single Jumps > Regular Moves.
* @param {Array<{fromRow: number, fromCol: number, toRow: number, toCol: number}>} moves - The list of available moves.
* @returns {Array<{fromRow: number, fromCol: number, toRow: number, toCol: number}>} The subset of best moves.
*/
getBestCPUMoves(moves) {
const promotionMoves = moves.filter(move => {
const piece = this.game.board[move.fromRow][move.fromCol];
return piece === PIECES.BLACK && move.toRow === BOARD_SIZE - 1;
});
if (promotionMoves.length > 0) return promotionMoves;
const jumps = moves.filter(move => Math.abs(move.fromRow - move.toRow) === 2);
if (jumps.length === 0) return moves;
const multiJumps = jumps.filter(move => this.canChainJumpAfterMove(move));
if (multiJumps.length > 0) return multiJumps;
return jumps;
}
/**
* Simulates a move to check if it enables a subsequent jump (chain jumping).
* @param {{fromRow: number, fromCol: number, toRow: number, toCol: number}} move - The move to simulate.
* @returns {boolean} True if a chain jump is possible immediately after this move.
*/
canChainJumpAfterMove(move) {
const board = this.game.board;
const piece = board[move.fromRow][move.fromCol];
const capturedRow = (move.fromRow + move.toRow) / 2;
const capturedCol = (move.fromCol + move.toCol) / 2;
const capturedPiece = board[capturedRow][capturedCol];
// Simulate move
board[move.toRow][move.toCol] = piece;
board[move.fromRow][move.fromCol] = PIECES.EMPTY;
board[capturedRow][capturedCol] = PIECES.EMPTY;
const canChain = this.game.canJumpFrom(move.toRow, move.toCol);
// Restore board
board[move.fromRow][move.fromCol] = piece;
board[move.toRow][move.toCol] = PIECES.EMPTY;
board[capturedRow][capturedCol] = capturedPiece;
return canChain;
}
/**
* Helper to get moves for a specific piece from the Game instance.
* @param {number} row - Row index.
* @param {number} col - Column index.
* @returns {Array<{fromRow: number, fromCol: number, toRow: number, toCol: number}>} Valid moves for the piece.
*/
getMovesForPiece(row, col) {
return this.game.getMovesForPiece(row, col);
}
}
/**
* UIController
* Manages the DOM, handles rendering of the board and pieces, and controls visual effects.
* This class acts as the View in the MVC pattern, observing the Game state and updating
* the HTML elements accordingly.
*/
class UIController {
/**
* Initializes the UI controller, validates required DOM elements, and builds the initial layout.
* @param {Game} game - Reference to the main Game instance.
*/
constructor(game) {
this.game = game;
this.boardElement = document.getElementById('board');
this.winnerOverlay = document.getElementById('winner-overlay');
this.winnerMessage = document.getElementById('winner-message');
if (!this.boardElement || !this.winnerOverlay || !this.winnerMessage) {
throw new Error("Missing DOM elements");
}
this.initLayout();
this.initConfetti();
}
/**
* Sets up the initial HTML layout, including the board, controls, profile, and scoreboard.
* Dynamically creates and appends elements to the DOM to construct the game interface.
*/
initLayout() {
// Viewport setup
let viewportMeta = document.querySelector('meta[name="viewport"]');
if (!viewportMeta) {
viewportMeta = document.createElement('meta');
viewportMeta.name = "viewport";
document.head.appendChild(viewportMeta);
}
viewportMeta.content = "width=device-width, initial-scale=1.0, maximum-scale=5.0, user-scalable=yes";
document.body.style.touchAction = "manipulation";
document.body.style.overflow = "auto";
// Title
const titleElement = document.querySelector('h1');
if (titleElement) titleElement.innerHTML = LABELS.TITLE;
// Layout Containers
let container = this.boardElement;
while (container.parentNode && container.parentNode !== document.body) {
container = container.parentNode;
}
this.gameLayout = createElement('div', null, { cssText: "position: relative; display: inline-block;" });
document.body.insertBefore(this.gameLayout, container);
this.gameLayout.appendChild(container);
// Profile Group
this.profileGroup = createElement('div', this.gameLayout, {
cssText: `
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
position: absolute;
right: 100%;
top: -170px;
margin-right: 45px;
width: max-content;
`
});
this.createProfile();
// Controls Group
this.controlsGroup = createElement('div', this.gameLayout, {
cssText: `
display: flex;
flex-direction: column;
gap: 2px;
position: absolute;
right: 100%;
top: 40px;
margin-right: 35px;
width: max-content;
`
});
this.statusElement = createElement('div', this.controlsGroup, {
className: 'status-indicator',
cssText: "padding: 8px 5px; width: 185px; box-sizing: border-box;"
});
this.singlePlayerButton = createButton(LABELS.BTN_DISABLE, () => this.game.toggleSinglePlayer());
this.controlsGroup.appendChild(this.singlePlayerButton);
this.controlsGroup.appendChild(createButton(LABELS.BTN_RESET, () => this.game.resetGame()));
// Scoreboard
this.createScoreboard();
// Captured Pieces
this.createCapturedContainer();
// Help Button
this.createHelpButton();
// Help Modal
this.createHelpModal();
}
/**
* Creates the user profile section with avatar and social links.
* Handles hover effects for the profile picture and social icons.
*/
createProfile() {
const profileLink = createElement('a', this.profileGroup, {
attrs: { href: "https://www.linkedin.com/in/john-s-garvey/", target: "_blank" },
cssText: `
display: block;
width: 140px;
height: 140px;
margin: 0 auto 10px auto;
cursor: pointer;
`
});
this.profilePic = createElement('img', profileLink, {
attrs: { src: 'JGarvey_Prof_Profile.jpg' },
cssText: `width: 100%;
height: 100%;
border-radius: 50%;
border: 4px solid ${THEME.PROFILE_BORDER_ACTIVE};
box-shadow: 0 4px 8px rgba(0,0,0,0.5);
object-fit: cover;
transition: transform 0.3s ease;`
});
this.profilePic.addEventListener('mouseover', () => this.profilePic.style.transform = 'scale(1.1)');
this.profilePic.addEventListener('mouseout', () => this.profilePic.style.transform = 'scale(1)');
const socialContainer = createElement('div', this.profileGroup, { cssText: "display: flex; justify-content: center; gap: 5px;" });
SOCIAL_LINKS.forEach(linkData => {
const link = createElement('a', socialContainer, {
attrs: { href: linkData.url, target: "_blank", title: linkData.label },
cssText: `
display: inline-block;
margin: 0 2px;
color: ${THEME.SOCIAL_ICON};
opacity: ${THEME.SOCIAL_ICON_OPACITY};
transition: opacity 0.2s;
vertical-align: middle;
`
});
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("viewBox", "0 0 24 24");
svg.setAttribute("width", "32");
svg.setAttribute("height", "32");
svg.style.fill = "currentColor";
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", linkData.path);
svg.appendChild(path);
link.appendChild(svg);
link.addEventListener('mouseover', () => link.style.opacity = THEME.SOCIAL_ICON_HOVER_OPACITY);
link.addEventListener('mouseout', () => link.style.opacity = THEME.SOCIAL_ICON_OPACITY);
});
}
/**
* Creates the scoreboard display for tracking wins.
* Includes the score counters for both players and the reset score button.
*/
createScoreboard() {
const scoreGroup = createElement('div', this.gameLayout, {
cssText: `
display: flex;
flex-direction: column;
align-items: center;
gap: 15px;
position: absolute;
left: 100%;
top: 20px;
margin-left: 35px;
width: 110px;
padding: 30px 10px;
background-color: ${THEME.SCORE_CONTAINER_BG};
border: 4px solid ${THEME.SCORE_CONTAINER_BORDER};
border-radius: 6px;
box-shadow: 0 4px 12px rgba(0,0,0,0.5);
font-family: 'Courier New', Courier, monospace;
color: ${THEME.SCORE_TEXT};
`
});
createElement('div', scoreGroup, {
text: LABELS.SCOREBOARD,
cssText: `
font-weight: bold;
font-size: 14px;
text-decoration: none;
border-bottom: 1px solid ${THEME.SCORE_TITLE_BORDER};
padding-bottom: 5px;
margin-bottom: 5px;
letter-spacing: 1px;
`
});
this.topLabel = createElement('div', scoreGroup, { text: LABELS.ME, cssText: "font-weight: bold; font-size: 18px;" });
const scoreDisplayCss = `
font-size: 42px;
font-family: 'Courier New', monospace;
font-weight: bold;
color: ${THEME.SCORE_DIGIT};
background-color: ${THEME.SCORE_BG};
padding: 5px 15px;
border: 4px solid ${THEME.SCORE_BORDER};
border-radius: 4px;
box-shadow: inset 0 0 10px ${THEME.SCORE_BG};
text-shadow: 0 0 5px ${THEME.SCORE_DIGIT}, 0 0 10px ${THEME.SCORE_DIGIT};
min-width: 50px;
text-align: center;
letter-spacing: 2px;
`;
this.topScoreDisplay = createElement('div', scoreGroup, { text: "0", cssText: scoreDisplayCss + " margin-bottom: 10px;" });
this.bottomLabel = createElement('div', scoreGroup, { text: LABELS.YOU, cssText: "font-weight: bold; font-size: 18px;" });
this.bottomScoreDisplay = createElement('div', scoreGroup, { text: "0", cssText: scoreDisplayCss });
const resetScoreBtn = createElement('button', scoreGroup, {
text: LABELS.BTN_RESET_SCORE,
cssText: `
margin-top: 15px;
padding: 8px 12px;
background: ${THEME.RESET_SCORE_BTN_BG};
color: ${THEME.RESET_SCORE_BTN_TEXT};
border: 1px solid ${THEME.RESET_SCORE_BTN_BORDER};
cursor: pointer;
font-family: inherit;
font-weight: bold;
font-size: 12px;
border-radius: 2px;
transition: all 0.2s;
`
});
resetScoreBtn.addEventListener('mouseover', () => resetScoreBtn.style.backgroundColor = THEME.RESET_SCORE_BTN_HOVER);
resetScoreBtn.addEventListener('mouseout', () => resetScoreBtn.style.backgroundColor = THEME.RESET_SCORE_BTN_BG);
resetScoreBtn.addEventListener('click', () => this.game.resetScore());
}
/**
* Creates the containers for displaying captured pieces.
* These containers are positioned at the bottom of the game layout.
*/
createCapturedContainer() {
const capturedContainer = createElement('div', this.gameLayout, {
cssText: `
display: flex;
flex-direction: column;
gap: 5px;
position: absolute;
right: 100%;
bottom: 25px;
margin-right: 10px;
`
});
const capturedStyle = `
display: grid;
grid-template-columns: repeat(4, 50px);
grid-template-rows: repeat(3, 30px);
gap: 5px;
padding: 10px;
justify-items: center;
background-color: ${THEME.CAPTURED_BG};
border-radius: 8px;
border: 1px solid ${THEME.CAPTURED_BORDER};
`;
this.blackCaptured = createElement('div', capturedContainer, { cssText: capturedStyle });
this.redCaptured = createElement('div', capturedContainer, { cssText: capturedStyle });
}
/**
* Creates a floating help button positioned next to the title.
* The button triggers the help modal when clicked.
*/
createHelpButton() {
const helpBtn = createElement('button', this.gameLayout, {
text: "?",
attrs: { title: "Instructions" },
cssText: `
position: absolute;
left: 100%;
top: -60px;
margin-left: 70px;
width: 40px;
height: 40px;
border-radius: 50%;
background: linear-gradient(145deg, #444, #222);
color: #fff;
border: 2px solid #777;
font-family: sans-serif;
font-weight: bold;
font-size: 20px;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
z-index: 100;
display: flex;
justify-content: center;
align-items: center;
transition: all 0.2s ease;
`
});
helpBtn.addEventListener('mouseover', () => {
helpBtn.style.transform = 'scale(1.1)';
helpBtn.style.boxShadow = '0 6px 8px rgba(0,0,0,0.5)';
helpBtn.style.borderColor = '#fff';
});
helpBtn.addEventListener('mouseout', () => {
helpBtn.style.transform = 'scale(1)';
helpBtn.style.boxShadow = '0 4px 6px rgba(0,0,0,0.3)';
helpBtn.style.borderColor = '#777';
});
helpBtn.addEventListener('click', () => this.showHelp());
}
/**
* Creates the modal overlay for displaying the Help content.
* The modal is hidden by default and includes a close button.
*/
createHelpModal() {
this.helpOverlay = createElement('div', document.body, {
cssText: `
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.85);
z-index: 3000;
justify-content: center;
align-items: center;
`
});
const modalContainer = createElement('div', this.helpOverlay, {
cssText: `position: relative;
width: 90%; max-width:
600px; max-height: 85vh;
background-color: #fff;
border-radius: 8px;
padding: 25px;
display: flex;
flex-direction: column;
box-shadow: 0 0 20px rgba(0,0,0,0.5);
color: #333; font-family: sans-serif;`
});
const closeBtn = createElement('button', modalContainer, {
text: "×",
cssText: `position: absolute;
top: 10px;
right: 15px;
background: none;
border: none;
font-size: 28px;
font-weight: bold;
cursor: pointer;
color: #555;
line-height: 1;`
});
closeBtn.onclick = () => this.helpOverlay.style.display = 'none';
this.helpContent = createElement('div', modalContainer, {
cssText: "overflow-y: auto; padding-right: 10px; line-height: 1.6;"
});
this.helpOverlay.addEventListener('click', (e) => {
if (e.target === this.helpOverlay) this.helpOverlay.style.display = 'none';
});
}
/**
* Displays the help modal with the formatted instructions.
* Populates the modal content with HTML if it hasn't been loaded yet.
*/
showHelp() {
this.helpOverlay.style.display = 'flex';
if (!this.helpContent.innerHTML) {
this.helpContent.innerHTML = `
<h2 style="margin-top: 0; border-bottom: 1px solid #eee; padding-bottom: 10px;">John Garvey's Checkers Web Game</h2>
<p>A fully interactive, browser-based implementation of the classic board game Checkers (also known as Draughts). This application brings the traditional tabletop experience to the web using modern web technologies.</p>
<h3 style="color: #1e4d2b;">🕹️ How to Play</h3>
<ol>
<li><strong>Start:</strong> The game begins in Single Player mode. You play as <strong>Red</strong> (bottom), and the computer plays as <strong>Black</strong> (top).</li>
<li><strong>Move:</strong> Click on a piece to select it (highlighted in yellow), then click a valid diagonal tan square to move.</li>
<li><strong>Capture:</strong> If an opponent's piece is diagonally adjacent and the square behind it is empty, you can jump over it to capture it.</li>
<li><strong>Win:</strong> The game ends when one player loses all their pieces or cannot make a valid move.</li>
</ol>
<h4 style="margin-bottom: 5px;">Game Modes</h4>
<ul style="margin-top: 5px;">
<li><strong>Single Player (Default):</strong> The game starts with the NPC enabled. You play as <strong>Red</strong> (bottom) against the computer ("Me"), which plays as <strong>Black</strong> (top).</li>
<li><strong>Two Player:</strong> Click the <strong>"Disable NPC (Me)"</strong> button to switch to Two Player mode. In this mode, you can control both Red and Black pieces, allowing you to play against yourself or a friend on the same device. Click <strong>"Enable NPC (Me)"</strong> to return to Single Player mode.</li>
</ul>
<h3 style="color: #1e4d2b;">✨ Features</h3>
<h4 style="margin-bottom: 5px;">Core Gameplay</h4>
<ul style="margin-top: 5px;">
<li><strong>Classic Rules:</strong> Full implementation of standard checkers rules, including turn-based movement, chain jumping, and kinging promotions.</li>
<li><strong>Single Player Mode:</strong> Challenge a built-in computer opponent (NPC) with intelligent move selection.</li>
<li><strong>Move Validation:</strong> The game engine strictly enforces valid moves, preventing illegal actions.</li>
<li><strong>King Promotion:</strong> Pieces reaching the opposite end of the board are automatically promoted to Kings, gaining multi-directional movement.</li>
<li><strong>Chain Jumps:</strong> Supports complex multi-jump sequences (double/triple jumps) in a single turn.</li>
<li><strong>Win Detection:</strong> Automatically detects victory conditions when a player captures all opponent pieces or blocks all moves.</li>
</ul>
<h3 style="color: #1e4d2b;">👤 Author</h3>
<p style="margin: 10px 0;"><strong>John S. Garvey</strong></p>
<h3 style="color: #1e4d2b;">Code</h3>
<p style="margin: 10px 0;"><a href="https://github.com/jgarvey928/Checkers_Web_Game" target="_blank" style="color: #1e4d2b; text-decoration: none; font-weight: bold;">GitHub Repo</a></p>
`;
}
}
/**
* Renders the game board based on the current state.
* Clears the board element and rebuilds the grid of squares and pieces.
*
* @param {Array<Array<number>>} board - The 2D array representing the board state.
* @param {number} selectedRow - The row index of the currently selected piece.
* @param {number} selectedCol - The column index of the currently selected piece.
*/
renderBoard(board, selectedRow, selectedCol) {
this.boardElement.innerHTML = '';
for (let row = 0; row < BOARD_SIZE; row++) {
for (let col = 0; col < BOARD_SIZE; col++) {
const square = createElement('div', this.boardElement, {
className: `${CSS_CLASSES.SQUARE} ${(row + col) % 2 === 0 ? CSS_CLASSES.LIGHT : CSS_CLASSES.DARK}`,
attrs: { 'data-row': row, 'data-col': col }
});
const piece = board[row][col];
if (piece !== PIECES.EMPTY) {
const classes = [CSS_CLASSES.PIECE, isRedPiece(piece) ? CSS_CLASSES.RED : CSS_CLASSES.BLACK];
if (isKing(piece)) classes.push(CSS_CLASSES.KING);
createElement('div', square, { className: classes.join(' ') });
}
if (row === selectedRow && col === selectedCol) {
square.classList.add(CSS_CLASSES.SELECTED);
square.style.boxShadow = `inset 0 0 20px ${THEME.SELECTION}, 0 0 15px ${THEME.SELECTION}`;
square.style.border = `2px solid ${THEME.SELECTION}`;
square.style.transform = 'scale(1.1)';
square.style.zIndex = '10';
square.style.transition = 'all 0.2s ease';
}
}
}
}
/**
* Adds a visual representation of a captured piece to the side container.
*
* @param {number} piece - The numeric value of the captured piece.
*/
addCapturedPiece(piece) {
const classes = [CSS_CLASSES.PIECE, isRedPiece(piece) ? CSS_CLASSES.RED : CSS_CLASSES.BLACK];
if (isKing(piece)) classes.push(CSS_CLASSES.KING);
const pieceElement = createElement('div', null, {
className: classes.join(' '),
cssText: `
width: 30px;
height: 30px;
margin: 0;
font-size: 14px;
cursor: default;
box-shadow: inset 0 0 0 2px rgba(0,0,0,0.2), inset 0 0 0 4px rgba(255,255,255,0.1);
`
});
(isRedPiece(piece) ? this.redCaptured : this.blackCaptured).appendChild(pieceElement);
}
/**
* Initializes the canvas context for confetti effects.
* Creates a full-screen canvas overlay for rendering particle animations.
*/
initConfetti() {
this.confettiCanvas = createElement('canvas', document.body, {
cssText: `
position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 9999;
`
});
this.ctx = this.confettiCanvas.getContext('2d');
this.confettiParticles = [];
}
/**
* Starts the confetti animation loop.
* Generates a new set of particles and begins the requestAnimationFrame loop.
*/
startConfetti() {
this.confettiCanvas.width = window.innerWidth;
this.confettiCanvas.height = window.innerHeight;
this.confettiParticles = [];
for (let i = 0; i < GAME_CONFIG.CONFETTI_COUNT; i++) {
this.confettiParticles.push({
x: Math.random() * this.confettiCanvas.width,
y: Math.random() * this.confettiCanvas.height - this.confettiCanvas.height,
color: THEME.CONFETTI[Math.floor(Math.random() * THEME.CONFETTI.length)],
size: Math.random() * 10 + 5,
speedY: Math.random() * 3 + 2,
speedX: Math.random() * 2 - 1,
rotation: Math.random() * 360,