-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspider.html
More file actions
1703 lines (1446 loc) · 61.8 KB
/
spider.html
File metadata and controls
1703 lines (1446 loc) · 61.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, user-scalable=yes, viewport-fit=cover">
<link rel="manifest" href="manifest.webmanifest">
<meta name="application-name" content="IdleGames">
<script src="assets/js/pwa.js" defer></script>
<meta name="theme-color" content="#1a5f2a">
<title>Spider Solitaire</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<style>
:root {
--felt-color: #1a5f2a;
--felt-dark: #0d3d17;
--card-white: #fff;
--card-shadow: rgba(0,0,0,0.4);
--ui-bg: rgba(0,0,0,0.6);
--ui-text: #fff;
--accent: #ffd700;
--card-width: 71px;
--card-height: 100px;
--card-radius: 6px;
--stack-offset: 25px;
--face-down-offset: 8px;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-tap-highlight-color: transparent;
}
html, body {
width: 100%;
height: 100%;
overflow: hidden;
touch-action: none;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
body {
background: linear-gradient(145deg, var(--felt-color) 0%, var(--felt-dark) 100%);
}
#gameCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
cursor: grab;
}
#gameCanvas.dragging {
cursor: grabbing;
}
/* UI Overlay */
.ui-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 44px;
background: var(--ui-bg);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 12px;
z-index: 100;
gap: 8px;
}
.ui-bar-left, .ui-bar-right {
display: flex;
align-items: center;
gap: 8px;
}
.ui-bar-center {
display: flex;
align-items: center;
gap: 16px;
color: var(--ui-text);
font-size: 14px;
}
.stat {
display: flex;
align-items: center;
gap: 4px;
}
.stat-label {
opacity: 0.7;
}
.ui-btn {
background: rgba(255,255,255,0.15);
border: 1px solid rgba(255,255,255,0.2);
color: var(--ui-text);
padding: 8px 12px;
border-radius: 6px;
font-size: 13px;
cursor: pointer;
transition: all 0.2s;
display: flex;
align-items: center;
gap: 4px;
}
.ui-btn:hover {
background: rgba(255,255,255,0.25);
}
.ui-btn:active {
transform: scale(0.95);
}
.ui-btn.icon-only {
padding: 8px;
font-size: 16px;
}
/* Modal */
.modal-overlay {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.7);
backdrop-filter: blur(4px);
-webkit-backdrop-filter: blur(4px);
display: none;
align-items: center;
justify-content: center;
z-index: 200;
}
.modal-overlay.active {
display: flex;
}
.modal {
background: #2a2a2a;
border-radius: 16px;
padding: 24px;
max-width: 90vw;
width: 360px;
color: var(--ui-text);
box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.modal h2 {
margin-bottom: 16px;
font-size: 20px;
}
.modal p {
margin-bottom: 16px;
opacity: 0.8;
line-height: 1.5;
}
.modal-buttons {
display: flex;
gap: 12px;
flex-wrap: wrap;
}
.modal-btn {
flex: 1;
min-width: 100px;
padding: 12px 16px;
border-radius: 8px;
border: none;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
.modal-btn.primary {
background: var(--accent);
color: #000;
}
.modal-btn.secondary {
background: rgba(255,255,255,0.15);
color: var(--ui-text);
}
.modal-btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.suit-options {
display: flex;
flex-direction: column;
gap: 8px;
margin-bottom: 16px;
}
.suit-option {
display: flex;
align-items: center;
gap: 12px;
padding: 12px;
background: rgba(255,255,255,0.1);
border-radius: 8px;
cursor: pointer;
transition: all 0.2s;
}
.suit-option:hover {
background: rgba(255,255,255,0.2);
}
.suit-option.selected {
background: rgba(255,215,0,0.3);
border: 1px solid var(--accent);
}
.suit-option input {
display: none;
}
.suit-icons {
font-size: 18px;
}
.suit-desc {
flex: 1;
}
.suit-desc strong {
display: block;
margin-bottom: 2px;
}
.suit-desc small {
opacity: 0.7;
}
/* Win celebration */
.win-overlay {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.8);
display: none;
align-items: center;
justify-content: center;
z-index: 300;
flex-direction: column;
gap: 24px;
}
.win-overlay.active {
display: flex;
}
.win-title {
font-size: 48px;
color: var(--accent);
text-shadow: 0 0 30px rgba(255,215,0,0.5);
animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.win-stats {
color: var(--ui-text);
text-align: center;
font-size: 18px;
line-height: 1.8;
}
.win-overlay .modal-btn {
flex: none;
padding: 14px 32px;
}
/* Touch hint */
.touch-hint {
position: fixed;
bottom: 12px;
left: 50%;
transform: translateX(-50%);
background: var(--ui-bg);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
color: var(--ui-text);
padding: 8px 16px;
border-radius: 20px;
font-size: 12px;
opacity: 0.8;
z-index: 100;
pointer-events: none;
}
@media (min-width: 768px) {
.touch-hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div class="ui-bar">
<div class="ui-bar-left">
<button class="ui-btn" id="btnNewGame">🃏 New</button>
<button class="ui-btn" id="btnUndo" title="Undo">↶</button>
</div>
<div class="ui-bar-center">
<div class="stat"><span class="stat-label">Moves:</span> <span id="statMoves">0</span></div>
<div class="stat"><span class="stat-label">Score:</span> <span id="statScore">500</span></div>
<div class="stat"><span class="stat-label">Time:</span> <span id="statTime">0:00</span></div>
</div>
<div class="ui-bar-right">
<button class="ui-btn icon-only" id="btnZoomFit" title="Fit to screen">⊡</button>
<button class="ui-btn icon-only" id="btnHelp" title="Help">?</button>
</div>
</div>
<div class="touch-hint">Pinch to zoom • Drag to pan • Double-tap to auto-move</div>
<!-- New Game Modal -->
<div class="modal-overlay" id="modalNewGame">
<div class="modal">
<h2>🕷️ Spider Solitaire</h2>
<p>Choose difficulty:</p>
<div class="suit-options">
<label class="suit-option selected" data-suits="1">
<input type="radio" name="suits" value="1" checked>
<span class="suit-icons">♠️</span>
<div class="suit-desc">
<strong>1 Suit (Easy)</strong>
<small>All spades - great for learning</small>
</div>
</label>
<label class="suit-option" data-suits="2">
<input type="radio" name="suits" value="2">
<span class="suit-icons">♠️ ♥️</span>
<div class="suit-desc">
<strong>2 Suits (Medium)</strong>
<small>Spades and hearts</small>
</div>
</label>
<label class="suit-option" data-suits="4">
<input type="radio" name="suits" value="4">
<span class="suit-icons">♠️ ♥️ ♣️ ♦️</span>
<div class="suit-desc">
<strong>4 Suits (Hard)</strong>
<small>Full deck - expert challenge</small>
</div>
</label>
</div>
<div class="modal-buttons">
<button class="modal-btn secondary" id="btnCancelNew">Cancel</button>
<button class="modal-btn primary" id="btnStartNew">Start Game</button>
</div>
</div>
</div>
<!-- Help Modal -->
<div class="modal-overlay" id="modalHelp">
<div class="modal">
<h2>How to Play</h2>
<p><strong>Goal:</strong> Build 8 complete sequences from King to Ace of the same suit.</p>
<p><strong>Rules:</strong></p>
<p>• Move cards in descending order (K→Q→J→10...→A)<br>
• Same-suit sequences can be moved as a group<br>
• Any card can fill an empty column<br>
• Deal new cards from the stock (bottom right)<br>
• Complete sequences are automatically removed</p>
<p><strong>Controls:</strong></p>
<p>• Drag cards to move them<br>
• Double-tap/click for auto-move<br>
• Pinch or scroll to zoom<br>
• Drag empty area to pan</p>
<div class="modal-buttons">
<button class="modal-btn primary" id="btnCloseHelp">Got it!</button>
</div>
</div>
</div>
<!-- Win Overlay -->
<div class="win-overlay" id="winOverlay">
<div class="win-title">🎉 You Won! 🎉</div>
<div class="win-stats" id="winStats"></div>
<button class="modal-btn primary" id="btnPlayAgain">Play Again</button>
</div>
<script>
// ========================================
// SPIDER SOLITAIRE - Full Implementation
// ========================================
const STORAGE_KEY = 'idlegames-spider-v1';
const CARD_WIDTH = 71;
const CARD_HEIGHT = 100;
const CARD_RADIUS = 6;
const STACK_OFFSET_FACE_UP = 25;
const STACK_OFFSET_FACE_DOWN = 8;
// Card suits and their colors
const SUITS = ['♠', '♥', '♣', '♦'];
const SUIT_COLORS = { '♠': '#000', '♥': '#c41e3a', '♣': '#000', '♦': '#c41e3a' };
const RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
// Game state
let gameState = {
columns: Array.from({length: 10}, () => []), // 10 columns of cards
stock: [], // remaining cards to deal
completed: 0, // number of completed sequences (0-8)
moves: 0,
score: 500,
startTime: null,
elapsedSeconds: 0,
numSuits: 1,
history: [] // for undo
};
// View state
let viewState = {
scale: 1,
offsetX: 0,
offsetY: 60, // account for UI bar
minScale: 0.3,
maxScale: 5 // Increased for better mobile zoom
};
// Interaction state
let dragState = {
isDragging: false,
isPanning: false,
cards: [], // cards being dragged
sourceColumn: -1,
sourceIndex: -1,
startX: 0,
startY: 0,
offsetX: 0,
offsetY: 0,
dragOffsetX: 0,
dragOffsetY: 0,
lastTapTime: 0,
lastTapCard: null
};
// Touch state for pinch zoom
let touchState = {
touches: [],
initialDistance: 0,
initialScale: 1,
initialCenterX: 0,
initialCenterY: 0,
initialOffsetX: 0,
initialOffsetY: 0
};
// Animation state
let animations = [];
// Canvas and rendering
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let cardCache = new Map(); // cached card images
let backCache = null; // cached card back
// Timer
let timerInterval = null;
// ========================================
// CARD RENDERING (Cached for speed)
// ========================================
function createCardCanvas(card) {
const offscreen = document.createElement('canvas');
offscreen.width = CARD_WIDTH * 2; // 2x for retina
offscreen.height = CARD_HEIGHT * 2;
const octx = offscreen.getContext('2d');
octx.scale(2, 2);
// Card background with subtle gradient
const gradient = octx.createLinearGradient(0, 0, CARD_WIDTH, CARD_HEIGHT);
gradient.addColorStop(0, '#ffffff');
gradient.addColorStop(1, '#f8f8f8');
octx.fillStyle = gradient;
octx.strokeStyle = '#888';
octx.lineWidth = 1;
roundRect(octx, 0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1, CARD_RADIUS);
octx.fill();
octx.stroke();
// Inner border highlight
octx.strokeStyle = 'rgba(255,255,255,0.8)';
octx.lineWidth = 1;
roundRect(octx, 2, 2, CARD_WIDTH - 4, CARD_HEIGHT - 4, CARD_RADIUS - 1);
octx.stroke();
const color = SUIT_COLORS[card.suit];
octx.fillStyle = color;
octx.textAlign = 'left';
octx.textBaseline = 'top';
// Top-left rank and suit
octx.font = 'bold 14px Arial';
octx.fillText(card.rank, 5, 5);
octx.font = '12px Arial';
octx.fillText(card.suit, 6, 20);
// Bottom-right (rotated)
octx.save();
octx.translate(CARD_WIDTH - 5, CARD_HEIGHT - 5);
octx.rotate(Math.PI);
octx.font = 'bold 14px Arial';
octx.fillText(card.rank, 0, 0);
octx.font = '12px Arial';
octx.fillText(card.suit, 1, 15);
octx.restore();
// Center suit (large)
octx.font = '36px Arial';
octx.textAlign = 'center';
octx.textBaseline = 'middle';
octx.fillText(card.suit, CARD_WIDTH / 2, CARD_HEIGHT / 2);
return offscreen;
}
function createCardBack() {
const offscreen = document.createElement('canvas');
offscreen.width = CARD_WIDTH * 2;
offscreen.height = CARD_HEIGHT * 2;
const octx = offscreen.getContext('2d');
octx.scale(2, 2);
// Card shape
const gradient = octx.createLinearGradient(0, 0, CARD_WIDTH, CARD_HEIGHT);
gradient.addColorStop(0, '#1e3a5f');
gradient.addColorStop(0.5, '#2d5a87');
gradient.addColorStop(1, '#1e3a5f');
octx.fillStyle = gradient;
octx.strokeStyle = 'rgba(255,255,255,0.2)';
octx.lineWidth = 1;
roundRect(octx, 0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1, CARD_RADIUS);
octx.fill();
octx.stroke();
// Inner border
octx.strokeStyle = 'rgba(255,255,255,0.2)';
roundRect(octx, 3, 3, CARD_WIDTH - 6, CARD_HEIGHT - 6, CARD_RADIUS - 1);
octx.stroke();
// Diamond pattern
octx.strokeStyle = 'rgba(255,255,255,0.15)';
octx.lineWidth = 1;
const spacing = 10;
for (let x = -CARD_HEIGHT; x < CARD_WIDTH + CARD_HEIGHT; x += spacing) {
octx.beginPath();
octx.moveTo(x, 0);
octx.lineTo(x + CARD_HEIGHT, CARD_HEIGHT);
octx.stroke();
octx.beginPath();
octx.moveTo(x + CARD_HEIGHT, 0);
octx.lineTo(x, CARD_HEIGHT);
octx.stroke();
}
// Center spider icon
octx.fillStyle = 'rgba(255,255,255,0.3)';
octx.font = '28px Arial';
octx.textAlign = 'center';
octx.textBaseline = 'middle';
octx.fillText('🕷️', CARD_WIDTH / 2, CARD_HEIGHT / 2);
return offscreen;
}
function getCardImage(card) {
const key = `${card.rank}${card.suit}`;
if (!cardCache.has(key)) {
cardCache.set(key, createCardCanvas(card));
}
return cardCache.get(key);
}
function getCardBack() {
if (!backCache) {
backCache = createCardBack();
}
return backCache;
}
function roundRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
// ========================================
// GAME LOGIC
// ========================================
function createDeck(numSuits) {
const deck = [];
const suitsToUse = SUITS.slice(0, numSuits);
const decksNeeded = 8 / numSuits; // 8 complete sequences needed
for (let d = 0; d < decksNeeded; d++) {
for (const suit of suitsToUse) {
for (const rank of RANKS) {
deck.push({ suit, rank, faceUp: false });
}
}
}
// Shuffle
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
return deck;
}
function initGame(numSuits = 1) {
const deck = createDeck(numSuits);
// Initialize 10 columns
gameState.columns = [];
for (let i = 0; i < 10; i++) {
gameState.columns.push([]);
}
// Deal initial cards: columns 0-3 get 6 cards, columns 4-9 get 5 cards
let cardIndex = 0;
for (let col = 0; col < 10; col++) {
const numCards = col < 4 ? 6 : 5;
for (let row = 0; row < numCards; row++) {
const card = deck[cardIndex++];
card.faceUp = (row === numCards - 1); // Only last card face up
gameState.columns[col].push(card);
}
}
// Remaining 50 cards go to stock
gameState.stock = deck.slice(cardIndex);
gameState.completed = 0;
gameState.moves = 0;
gameState.score = 500;
gameState.startTime = Date.now();
gameState.elapsedSeconds = 0;
gameState.numSuits = numSuits;
gameState.history = [];
startTimer();
saveGame();
render();
updateStats();
}
function getRankValue(rank) {
return RANKS.indexOf(rank);
}
function canStack(movingCard, existingCard) {
// Can stack if existing card's rank is one higher than moving card
return getRankValue(existingCard.rank) === getRankValue(movingCard.rank) + 1;
}
function getMovableSequence(column, startIndex) {
// Returns cards that form a valid same-suit descending sequence
const cards = [];
let lastCard = null;
for (let i = startIndex; i < column.length; i++) {
const card = column[i];
if (!card.faceUp) return [];
if (lastCard) {
// Must be same suit and one rank lower
if (card.suit !== lastCard.suit || getRankValue(card.rank) !== getRankValue(lastCard.rank) - 1) {
return [];
}
}
cards.push(card);
lastCard = card;
}
return cards;
}
function canMoveTo(cards, targetColumn) {
if (cards.length === 0) return false;
const target = gameState.columns[targetColumn];
// Empty column - any card can go there
if (target.length === 0) return true;
// Check if top card of target can accept bottom card of moving sequence
const targetTop = target[target.length - 1];
const movingBottom = cards[0];
return targetTop.faceUp && canStack(movingBottom, targetTop);
}
function moveCards(fromCol, fromIndex, toCol) {
// Save state for undo
saveHistory();
const cards = gameState.columns[fromCol].splice(fromIndex);
gameState.columns[toCol].push(...cards);
// Flip the new top card of source column if needed
const source = gameState.columns[fromCol];
if (source.length > 0 && !source[source.length - 1].faceUp) {
source[source.length - 1].faceUp = true;
}
gameState.moves++;
gameState.score = Math.max(0, gameState.score - 1);
checkForCompleteSequence(toCol);
saveGame();
updateStats();
}
function checkForCompleteSequence(colIndex) {
const column = gameState.columns[colIndex];
if (column.length < 13) return;
// Check if bottom 13 cards form K to A of same suit
const startIndex = column.length - 13;
let valid = true;
let suit = null;
for (let i = 0; i < 13; i++) {
const card = column[startIndex + i];
if (!card.faceUp) { valid = false; break; }
if (suit === null) suit = card.suit;
if (card.suit !== suit) { valid = false; break; }
if (card.rank !== RANKS[12 - i]) { valid = false; break; } // K, Q, J, 10, ..., A
}
if (valid) {
// Remove the sequence with animation
const removed = column.splice(startIndex, 13);
gameState.completed++;
gameState.score += 100;
// Flip new top card
if (column.length > 0 && !column[column.length - 1].faceUp) {
column[column.length - 1].faceUp = true;
}
// Create celebration animation
animateCompletedSequence(removed);
// Check for win
if (gameState.completed === 8) {
setTimeout(showWin, 1500);
}
saveGame();
updateStats();
}
}
function dealFromStock() {
if (gameState.stock.length === 0) return;
// Must have no empty columns to deal
const hasEmpty = gameState.columns.some(col => col.length === 0);
if (hasEmpty) {
showMessage('Fill all empty columns before dealing!');
return;
}
saveHistory();
// Deal one card to each column
for (let i = 0; i < 10 && gameState.stock.length > 0; i++) {
const card = gameState.stock.pop();
card.faceUp = true;
gameState.columns[i].push(card);
}
gameState.moves++;
// Check for complete sequences in all columns
for (let i = 0; i < 10; i++) {
checkForCompleteSequence(i);
}
saveGame();
updateStats();
render();
}
function saveHistory() {
// Deep clone current state for undo
const snapshot = {
columns: gameState.columns.map(col => col.map(card => ({...card}))),
stock: gameState.stock.map(card => ({...card})),
completed: gameState.completed,
moves: gameState.moves,
score: gameState.score
};
gameState.history.push(snapshot);
// Limit history size
if (gameState.history.length > 50) {
gameState.history.shift();
}
}
function undo() {
if (gameState.history.length === 0) return;
const snapshot = gameState.history.pop();
gameState.columns = snapshot.columns;
gameState.stock = snapshot.stock;
gameState.completed = snapshot.completed;
gameState.moves = snapshot.moves;
gameState.score = snapshot.score;
saveGame();
updateStats();
render();
}
function findAutoMove(card, sourceCol, sourceIndex) {
// Find best column to move card(s) to
const movableCards = getMovableSequence(gameState.columns[sourceCol], sourceIndex);
if (movableCards.length === 0) return -1;
let bestCol = -1;
let bestScore = -1;
for (let col = 0; col < 10; col++) {
if (col === sourceCol) continue;
if (!canMoveTo(movableCards, col)) continue;
const target = gameState.columns[col];
let score = 0;
// Prefer same suit matches
if (target.length > 0 && target[target.length - 1].suit === movableCards[0].suit) {
score += 10;
}
// Prefer non-empty columns (to keep empty columns available)
if (target.length > 0) {
score += 5;
}
// Prefer columns that would uncover face-down cards
const remaining = gameState.columns[sourceCol].length - sourceIndex;
if (sourceIndex > 0 && !gameState.columns[sourceCol][sourceIndex - 1].faceUp) {
score += 3;
}
if (score > bestScore) {
bestScore = score;
bestCol = col;
}
}
return bestCol;
}
// ========================================
// RENDERING
// ========================================
function resize() {
const dpr = window.devicePixelRatio || 1;
canvas.width = window.innerWidth * dpr;
canvas.height = window.innerHeight * dpr;
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
ctx.scale(dpr, dpr);
// Auto-fit view on resize
fitToScreen();
}
function fitToScreen() {
const padding = 20;
const topOffset = 60; // UI bar
const availableWidth = window.innerWidth - padding * 2;
const availableHeight = window.innerHeight - topOffset - padding * 2;
// Calculate required space
const columnSpacing = 10;
const totalWidth = 10 * CARD_WIDTH + 9 * columnSpacing;
const maxColumnHeight = Math.max(...gameState.columns.map(col => {
let h = 0;
col.forEach((card, i) => {
h += card.faceUp ? STACK_OFFSET_FACE_UP : STACK_OFFSET_FACE_DOWN;
});
return h + CARD_HEIGHT;
}), CARD_HEIGHT);
// Account for stock area at bottom
const totalHeight = maxColumnHeight + 80;
const scaleX = availableWidth / totalWidth;
const scaleY = availableHeight / totalHeight;
viewState.scale = Math.min(scaleX, scaleY, 1.5);
viewState.scale = Math.max(viewState.scale, viewState.minScale);
// Center horizontally
viewState.offsetX = (window.innerWidth - totalWidth * viewState.scale) / 2;
viewState.offsetY = topOffset + padding;
render();
}
function render() {
const width = window.innerWidth;
const height = window.innerHeight;
// Clear with felt color
ctx.fillStyle = '#1a5f2a';
ctx.fillRect(0, 0, width, height);
// Add felt texture
ctx.fillStyle = 'rgba(0,0,0,0.1)';
for (let i = 0; i < 50; i++) {
ctx.fillRect(
Math.random() * width,
Math.random() * height,
1 + Math.random() * 2,
1 + Math.random() * 2
);
}
ctx.save();
ctx.translate(viewState.offsetX, viewState.offsetY);
ctx.scale(viewState.scale, viewState.scale);
const columnSpacing = 10;
// Draw columns
for (let col = 0; col < 10; col++) {
const x = col * (CARD_WIDTH + columnSpacing);
drawColumn(col, x, 0);
}
// Draw stock area (bottom right)
const stockX = 9 * (CARD_WIDTH + columnSpacing);
const stockY = getMaxColumnHeight() + 30;
drawStock(stockX, stockY);
// Draw completed sequences area (bottom left)
drawCompleted(0, stockY);
ctx.restore();
// Draw dragged cards (not transformed)
if (dragState.isDragging && dragState.cards.length > 0) {
drawDraggedCards();
}
// Run animations
runAnimations();
}
function getMaxColumnHeight() {
let max = CARD_HEIGHT;
for (const col of gameState.columns) {
let h = 0;
col.forEach(card => {
h += card.faceUp ? STACK_OFFSET_FACE_UP : STACK_OFFSET_FACE_DOWN;
});
max = Math.max(max, h + CARD_HEIGHT);
}
return max;