forked from ReKindleOS/ReKindle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2pcheckers.html
More file actions
766 lines (654 loc) · 24.8 KB
/
Copy path2pcheckers.html
File metadata and controls
766 lines (654 loc) · 24.8 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
<!DOCTYPE html><html lang="en"><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
<meta charset="UTF-8">
<title>Checkers 2P</title>
<style>
/* SYSTEM 7 AESTHETICS */
:root {
--bg-color: #ffffff;
--text-color: #000000;
--border: 2px solid #000000;
--shadow: 4px 4px 0px #000000;
--stripe-pattern: repeating-linear-gradient(0deg, transparent, transparent 2px, #000 3px, #000 4px);
--board-light: #ffffff;
--board-dark: #a0a0a0;
--highlight: inset 0 0 0 4px black;
--hint: rgba(0, 0, 0, 0.2);
}
body {
image-rendering: -webkit-crisp-edges;
image-rendering: pixelated;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: 0;
padding: 0;
height: 100vh;
overflow: hidden;
background-color: #e5e5e5;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
user-select: none;
box-sizing: border-box;
}
/* WINDOW FRAME */
.window {
background: white;
border: 2px solid black;
box-shadow: var(--shadow);
display: flex;
flex-direction: column;
height: 90vh;
width: 90vw;
max-width: 800px;
position: relative;
}
.title-bar {
height: 35px;
border-bottom: 2px solid black;
display: flex;
align-items: center;
justify-content: center;
background: white;
padding: 0 10px;
position: relative;
flex-shrink: 0;
}
.title-stripes {
position: absolute;
top: 4px;
bottom: 4px;
left: 4px;
right: 4px;
background-image: var(--stripe-pattern);
z-index: 0;
}
.title-text {
background: white;
padding: 0 15px;
font-weight: bold;
z-index: 1;
font-size: 1.1rem;
}
.close-box {
position: absolute;
left: 10px;
width: 18px;
height: 18px;
border: 2px solid black;
background: white;
z-index: 2;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-family: sans-serif;
font-size: 0.9rem;
line-height: 1;
box-shadow: 2px 2px 0 black;
}
.close-box:active {
background: black;
color: white;
transform: translate(1px, 1px);
box-shadow: none;
}
.stats-btn:active {
background: black;
color: white;
transform: translate(1px, 1px);
box-shadow: none;
}
/* CONTENT */
.window-content {
padding: 5px;
background: white;
display: flex;
flex-direction: column;
align-items: center;
flex-grow: 1;
overflow-y: auto;
}
#status-bar {
width: 100%;
text-align: center;
margin-bottom: 5px;
font-weight: bold;
font-size: 1.1rem;
border-bottom: 1px solid #ccc;
padding-bottom: 5px;
flex-shrink: 0;
}
/* BOARD */
#board-container {
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
min-height: 0;
overflow: hidden;
}
#board {
border: 4px solid black;
display: grid;
grid-template-columns: repeat(8, 1fr);
grid-template-rows: repeat(8, 1fr);
box-sizing: border-box;
}
.square {
display: flex;
justify-content: center;
align-items: center;
position: relative;
width: 100%;
height: 100%;
cursor: pointer;
}
.square.light {
background-color: var(--board-light);
}
.square.dark {
background-color: var(--board-dark);
}
.square.selected {
box-shadow: var(--highlight);
}
.square.valid-move::after {
content: '';
width: 20%;
height: 20%;
background-color: rgba(0, 0, 0, 0.5);
border-radius: 50%;
}
.square.last-move {
background-image: repeating-linear-gradient(45deg, transparent, transparent 5px, rgba(0, 0, 0, 0.1) 5px, rgba(0, 0, 0, 0.1) 10px);
}
/* PIECES */
.piece {
width: 80%;
height: 80%;
border-radius: 50%;
box-sizing: border-box;
border: 2px solid black;
display: flex;
align-items: center;
justify-content: center;
font-weight: 900;
font-size: 1.5rem;
pointer-events: none;
/* Let clicks pass to square */
}
/* Black Piece */
.piece.b {
background-color: black;
color: white;
}
/* White Piece */
.piece.w {
background-color: white;
color: black;
}
/* King Indicator */
.piece.king::after {
content: '♔';
/* Crown symbol */
line-height: 1;
padding-bottom: 4px;
}
/* CONTROLS */
.controls {
display: flex;
width: 100%;
max-width: 500px;
margin-top: 10px;
flex-shrink: 0;
padding-bottom: 5px;
}
.controls > * + * {
margin-left: 10px;
}
button.game-btn {
background: white;
border: 2px solid black;
padding: 10px;
flex: 1;
font-weight: bold;
font-size: 1rem;
cursor: pointer;
box-shadow: 3px 3px 0 black;
}
button.game-btn:active {
background: black;
color: white;
transform: translate(2px, 2px);
box-shadow: none;
}
/* MODALS */
.modal-overlay {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.9);
z-index: 100;
align-items: center;
justify-content: center;
}
.modal-box {
background: white;
border: 2px solid black;
box-shadow: 8px 8px 0 black;
padding: 20px;
width: 250px;
text-align: center;
}
</style>
<script src="js/i18n.js"></script>
<script src="theme.js"></script>
</head>
<body>
<div class="window">
<div class="title-bar">
<div class="title-stripes"></div>
<div class="close-box" onclick="window.location.href='index'" data-i18n="battleship.btn.close">X</div>
<span class="title-text" data-i18n="checkers2p.title">Checkers 2P</span>
</div>
<div class="window-content">
<div id="status-bar" data-i18n="checkers2p.turn.black">Black to move</div>
<div id="board-container">
<div id="board"></div>
</div>
<div class="controls">
<button class="game-btn" onclick="flipBoard()" data-i18n="checkers2p.btn.flip">Flip</button>
<button class="game-btn" onclick="undoMove()" data-i18n="checkers2p.btn.undo">Undo</button>
<button class="game-btn" onclick="openResetModal()" data-i18n="checkers2p.btn.new_game">New
Game</button>
</div>
</div>
</div>
<div id="reset-modal" class="modal-overlay">
<div class="modal-box">
<h3 data-i18n="checkers2p.modal.new_game_title">Start New Game?</h3>
<p style="margin: 15px 0;" data-i18n="checkers2p.modal.new_game_msg">Current progress will be lost.</p>
<div style="display: flex; justify-content: center">
<button class="game-btn" onclick="confirmReset()" data-i18n="checkers2p.btn.yes">Yes</button>
<button class="game-btn" onclick="closeResetModal()" data-i18n="checkers2p.btn.no" style="margin-left: 10px">No</button>
</div>
</div>
</div>
<script>
// --- GAME LOGIC ---
class CheckersGame {
constructor() {
this.reset();
}
reset() {
// Board: 8x8. 0=empty.
// Piece: { color: 'b'|'w', king: boolean }
this.board = Array(8).fill(null).map(() => Array(8).fill(null));
this.turn = 'b'; // Black moves first in Checkers usually
this.history = [];
this.winner = null;
this.mustJumpPos = null; // If set, only this piece can move (multi-jump)
// Initialize Pieces
for (let r = 0; r < 8; r++) {
for (let c = 0; c < 8; c++) {
if ((r + c) % 2 === 1) { // Dark squares only
if (r < 3) this.board[r][c] = { color: 'w', king: false }; // White on top
if (r > 4) this.board[r][c] = { color: 'b', king: false }; // Black on bottom
}
}
}
}
getPiece(r, c) {
if (r < 0 || r > 7 || c < 0 || c > 7) return null;
return this.board[r][c];
}
// Get all valid moves for the current turn
getValidMoves() {
let moves = [];
let canJump = false;
// If we are in a multi-jump sequence, only check that piece
if (this.mustJumpPos) {
const jumps = this.getJumps(this.mustJumpPos.r, this.mustJumpPos.c);
return jumps; // Only jumps allowed in chain
}
// Iterate all pieces of current turn
for (let r = 0; r < 8; r++) {
for (let c = 0; c < 8; c++) {
const p = this.board[r][c];
if (p && p.color === this.turn) {
const jumps = this.getJumps(r, c);
if (jumps.length > 0) {
canJump = true;
moves.push(...jumps);
}
}
}
}
// If any piece can jump, ONLY jumps are allowed (Mandatory Jump Rule)
if (canJump) return moves;
// Otherwise get simple moves
for (let r = 0; r < 8; r++) {
for (let c = 0; c < 8; c++) {
const p = this.board[r][c];
if (p && p.color === this.turn) {
moves.push(...this.getSimpleMoves(r, c));
}
}
}
return moves;
}
getSimpleMoves(r, c) {
const moves = [];
const p = this.board[r][c];
const dirs = p.king ? [[-1, -1], [-1, 1], [1, -1], [1, 1]] : (p.color === 'b' ? [[-1, -1], [-1, 1]] : [[1, -1], [1, 1]]);
for (let d of dirs) {
const nr = r + d[0];
const nc = c + d[1];
if (this.isValidPos(nr, nc) && this.board[nr][nc] === null) {
moves.push({ from: { r, c }, to: { r: nr, c: nc }, type: 'move' });
}
}
return moves;
}
getJumps(r, c) {
const jumps = [];
const p = this.board[r][c];
const dirs = p.king ? [[-1, -1], [-1, 1], [1, -1], [1, 1]] : (p.color === 'b' ? [[-1, -1], [-1, 1]] : [[1, -1], [1, 1]]);
for (let d of dirs) {
const midR = r + d[0];
const midC = c + d[1];
const destR = r + d[0] * 2;
const destC = c + d[1] * 2;
// FIX: Ensure no crash if dest out of bounds before checking board content
if (this.isValidPos(destR, destC) && this.board[destR][destC] === null) {
const midR = r + d[0];
const midC = c + d[1];
const midP = this.board[midR][midC];
if (midP && midP.color !== this.turn) {
jumps.push({
from: { r, c },
to: { r: destR, c: destC },
type: 'jump',
captured: { r: midR, c: midC }
});
}
}
}
return jumps;
}
isValidPos(r, c) {
return r >= 0 && r < 8 && c >= 0 && c < 8;
}
makeMove(move) {
// Save state for undo
this.history.push(JSON.parse(JSON.stringify({
board: this.board,
turn: this.turn,
mustJumpPos: this.mustJumpPos,
winner: this.winner
})));
const p = this.board[move.from.r][move.from.c];
this.board[move.to.r][move.to.c] = p;
this.board[move.from.r][move.from.c] = null;
if (move.type === 'jump') {
this.board[move.captured.r][move.captured.c] = null;
// Check multi-jump
const moreJumps = this.getJumps(move.to.r, move.to.c);
// Cannot multi-jump if just promoted
const promoted = this.checkPromotion(move.to.r, move.to.c);
if (moreJumps.length > 0 && !promoted) {
this.mustJumpPos = { r: move.to.r, c: move.to.c };
return; // Turn does not change
}
} else {
this.checkPromotion(move.to.r, move.to.c);
}
this.mustJumpPos = null;
this.turn = this.turn === 'b' ? 'w' : 'b';
this.checkWin();
this.save();
}
checkPromotion(r, c) {
const p = this.board[r][c];
if (!p.king) {
if ((p.color === 'b' && r === 0) || (p.color === 'w' && r === 7)) {
p.king = true;
return true;
}
}
return false;
}
checkWin() {
// Count pieces
let w = 0, b = 0;
for (let row of this.board) for (let p of row) {
if (p) p.color === 'w' ? w++ : b++;
}
if (w === 0) this.winner = 'b';
if (b === 0) this.winner = 'w';
// Check if current player has moves
if (!this.winner && this.getValidMoves().length === 0) {
this.winner = this.turn === 'b' ? 'w' : 'b';
}
}
undo() {
if (this.history.length > 0) {
const prev = this.history.pop();
this.board = prev.board;
this.turn = prev.turn;
this.mustJumpPos = prev.mustJumpPos;
this.winner = prev.winner;
return true;
}
return false;
}
save() {
const state = {
board: this.board,
turn: this.turn,
history: this.history,
winner: this.winner,
mustJumpPos: this.mustJumpPos
};
localStorage.setItem('2pcheckers_state', JSON.stringify(state));
}
load() {
try {
const saved = localStorage.getItem('2pcheckers_state');
if (saved) {
const state = JSON.parse(saved);
this.board = state.board;
this.turn = state.turn;
this.history = state.history || [];
this.winner = state.winner;
this.mustJumpPos = state.mustJumpPos;
return true;
}
} catch (e) { console.error(e); }
return false;
}
}
// --- UI LOGIC ---
let game = new CheckersGame();
let selectedSquare = null;
let isFlipped = false;
let lastMove = null;
let moveCount = 0;
function flashRefresh() {
// Check Display Mode
const mode = (window.rekindleGetDisplayMode ? window.rekindleGetDisplayMode() : localStorage.getItem('rekindle_display_mode')) || 'eink';
if (mode !== 'eink') return;
const board = document.getElementById('board');
if (!board) return;
const rect = board.getBoundingClientRect();
const flash = document.createElement('div');
flash.style.position = 'fixed';
flash.style.top = '0px';
flash.style.left = '0px';
flash.style.width = '100%';
flash.style.height = '100%';
flash.style.backgroundColor = 'black';
flash.style.zIndex = '9999';
flash.style.pointerEvents = 'none';
flash.style.opacity = '1'; // Start visible immediately (Hard Flash)
document.body.appendChild(flash);
// Force reflow
void flash.offsetWidth;
setTimeout(() => {
if (flash.parentNode) flash.parentNode.removeChild(flash);
}, 150); // Hold for 150ms then remove
}
function clearSaveAndRestart() {
localStorage.removeItem('2pcheckers_state');
confirmReset();
}
window.onload = () => {
if (window.rekindleApplyWallpaper) window.rekindleApplyWallpaper();
game.load();
renderBoard();
resizeBoard();
};
window.addEventListener('resize', resizeBoard);
function resizeBoard() {
const container = document.getElementById('board-container');
const board = document.getElementById('board');
if (container && board) {
const size = Math.min(container.clientWidth, container.clientHeight) * 0.95;
board.style.width = size + 'px';
board.style.height = size + 'px';
}
}
function renderBoard() {
const boardEl = document.getElementById('board');
boardEl.innerHTML = '';
// Flip logic
let rows = [];
for (let i = 0; i < 8; i++) rows.push(i);
if (isFlipped) rows.reverse();
let cols = [0, 1, 2, 3, 4, 5, 6, 7];
if (isFlipped) cols.reverse();
const validMoves = game.getValidMoves();
const selectedMoves = selectedSquare ? validMoves.filter(m => m.from.r === selectedSquare.r && m.from.c === selectedSquare.c) : [];
rows.forEach(r => {
cols.forEach(c => {
const div = document.createElement('div');
const isDark = (r + c) % 2 !== 0;
div.className = `square ${isDark ? 'dark' : 'light'}`;
// Selected highlight
if (selectedSquare && selectedSquare.r === r && selectedSquare.c === c) {
div.classList.add('selected');
}
// Valid move hint
if (selectedMoves.some(m => m.to.r === r && m.to.c === c)) {
div.classList.add('valid-move');
}
// Last move highlight
if (lastMove && ((lastMove.from.r === r && lastMove.from.c === c) || (lastMove.to.r === r && lastMove.to.c === c))) {
div.classList.add('last-move');
}
div.onclick = () => handleSquareClick(r, c);
const p = game.getPiece(r, c);
if (p) {
const pDiv = document.createElement('div');
pDiv.className = `piece ${p.color}`;
if (p.king) pDiv.classList.add('king');
div.appendChild(pDiv);
}
boardEl.appendChild(div);
});
});
updateStatus();
resizeBoard();
}
function handleSquareClick(r, c) {
if (game.winner) return;
// 1. Check if clicking a valid move destination
if (selectedSquare) {
const validMoves = game.getValidMoves();
const move = validMoves.find(m =>
m.from.r === selectedSquare.r && m.from.c === selectedSquare.c &&
m.to.r === r && m.to.c === c
);
if (move) {
game.makeMove(move);
game.save();
lastMove = move;
selectedSquare = null;
moveCount++;
setTimeout(flashRefresh, 300);
renderBoard();
// If multi-jump is active, auto-select the piece
if (game.mustJumpPos) {
selectedSquare = game.mustJumpPos;
}
renderBoard();
return;
}
}
// 2. Select a piece
const p = game.getPiece(r, c);
if (p && p.color === game.turn) {
// If in multi-jump mode, can only select the active piece
if (game.mustJumpPos) {
if (game.mustJumpPos.r !== r || game.mustJumpPos.c !== c) return;
}
selectedSquare = { r, c };
renderBoard();
} else {
// Deselect if clicking empty space or enemy (unless move, handled above)
if (!game.mustJumpPos) {
selectedSquare = null;
renderBoard();
}
}
}
function updateStatus() {
const el = document.getElementById('status-bar');
if (game.winner) {
const winnerName = window.t ? window.t('pool2p.status.' + (game.winner === 'b' ? 'black' : 'white')) : (game.winner === 'b' ? 'Black' : 'White');
el.innerText = window.t ? window.t('checkers2p.msg.win', { winner: winnerName }) : `${winnerName} Wins!`;
} else {
const turnName = window.t ? window.t('checkers2p.turn.' + (game.turn === 'b' ? 'black' : 'white')) : (game.turn === 'b' ? 'Black to move' : 'White to move');
el.innerText = turnName;
if (game.mustJumpPos) el.innerText += (window.t ? window.t('checkers2p.msg.double_jump') : " (Double Jump!)");
}
}
function flipBoard() {
isFlipped = !isFlipped;
renderBoard();
}
function undoMove() {
if (game.undo()) {
selectedSquare = null;
lastMove = null;
renderBoard();
}
}
// --- RESET MODAL ---
function openResetModal() {
document.getElementById('reset-modal').style.display = 'flex';
}
function closeResetModal() {
document.getElementById('reset-modal').style.display = 'none';
}
function confirmReset() {
game.reset();
selectedSquare = null;
lastMove = null;
moveCount = 0;
renderBoard();
resizeBoard();
updateStatus();
closeResetModal();
}
</script>
<script>
</script>
<script src="https://cdn.counter.dev/script.js" data-id="4e551911-0e5d-4c48-91a6-b828c3301675" data-utcoffset="11"></script>
</body></html>