-
Notifications
You must be signed in to change notification settings - Fork 0
/
games.html
459 lines (420 loc) · 17.5 KB
/
games.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Solitaire</title>
<link rel="icon" href="canvas.jpg" type="image/jpg">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
.container {
max-width: 1000px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1, h2 {
text-align: center;
color: #333;
}
#gameCanvas {
display: block;
margin: 0 auto;
border: 1px solid #ccc;
}
button {
display: block;
margin: 10px auto;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.nav-menu {
text-align: center;
margin-bottom: 20px;
}
.nav-menu a {
text-decoration: none;
color: #333;
padding: 10px 20px;
border-radius: 4px;
background-color: #eee;
margin: 0 10px;
}
.nav-menu a:hover {
background-color: #ddd;
}
.instructions {
background-color: #f9f9f9;
padding: 15px;
border-radius: 8px;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="nav-menu">
<a href="index.html">Home</a>
<a href="field.html">FTC Field Path Planner</a>
</div>
<div class="container">
<h1>Solitaire</h1>
<canvas id="gameCanvas" width="1000" height="850"></canvas>
<button id="newGameBtn">New Game</button>
<div class="instructions">
<h2>Instructions:</h2>
<ul>
<li>Arrange cards in descending order (King to Ace) in alternating colors (black and red).</li>
<li>Click on the deck to draw cards to the waste pile. You can move cards from the waste pile to the tableau or foundation piles.</li>
<li>Foundation piles must start with an Ace and build up to King, all in the same suit.</li>
<li>Tableau piles build down in alternating colors.</li>
<li>Click on cards to move them around.</li>
<li>Goal: Move all cards to the foundation piles to win!</li>
</ul>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Constants
const CARD_WIDTH = 70;
const CARD_HEIGHT = 100;
const CARD_GAP = 20;
const TABLEAU_OFFSET_Y = 150;
const TABLEAU_OFFSET_X = 130;
const FOUNDATION_OFFSET_Y = 20;
const FOUNDATION_OFFSET_X = 560;
const CARD_BACK_COLOR = '#999';
const CARD_COLORS = {
'♠': 'black',
'♣': 'black',
'♥': 'red',
'♦': 'red'
};
const DECK_OFFSET_X = 20;
const DECK_OFFSET_Y = 20;
const WASTE_OFFSET_X = 110;
const WASTE_OFFSET_Y = 20;
const CARD_SPACING = CARD_HEIGHT / 2;
// Game state
let gameState;
function initializeGameState() {
return {
deck: [],
tableau: [[], [], [], [], [], [], []],
foundation: [[], [], [], []],
stock: [],
waste: [],
won: false
};
}
function shuffleDeck() {
let deck = [];
const suits = ['♠', '♣', '♥', '♦'];
const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
for (let suit of suits) {
for (let rank of ranks) {
deck.push({ suit, rank, isFaceUp: false });
}
}
// Fisher-Yates shuffle algorithm
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 dealCards() {
for (let i = 0; i < 7; i++) {
for (let j = 0; j <= i; j++) {
let card = gameState.deck.pop();
if (j === i) {
card.isFaceUp = true;
}
gameState.tableau[i].push(card);
}
}
}
function drawCard(card, x, y) {
if (card.isFaceUp) {
ctx.fillStyle = 'white';
ctx.fillRect(x, y, CARD_WIDTH, CARD_HEIGHT);
ctx.strokeStyle = 'black';
ctx.strokeRect(x, y, CARD_WIDTH, CARD_HEIGHT);
ctx.fillStyle = CARD_COLORS[card.suit];
ctx.font = '20px Arial';
ctx.fillText(card.rank + card.suit, x + 5, y + 25);
} else {
ctx.fillStyle = CARD_BACK_COLOR;
ctx.fillRect(x, y, CARD_WIDTH, CARD_HEIGHT);
ctx.strokeStyle = 'black';
ctx.strokeRect(x, y, CARD_WIDTH, CARD_HEIGHT);
}
}
function drawTableau() {
for (let i = 0; i < 7; i++) {
const pile = gameState.tableau[i];
const x = (i * (CARD_WIDTH + CARD_GAP)) + TABLEAU_OFFSET_X;
let y = TABLEAU_OFFSET_Y;
for (let j = 0; j < pile.length; j++) {
const card = pile[j];
if (j === pile.length - 1) {
// Draw the full top card
drawCard(card, x, y);
} else if (card.isFaceUp) {
// Draw just the top part of face-up cards
ctx.fillStyle = 'white';
ctx.fillRect(x, y, CARD_WIDTH, CARD_SPACING);
ctx.strokeStyle = 'black';
ctx.strokeRect(x, y, CARD_WIDTH, CARD_SPACING);
ctx.fillStyle = CARD_COLORS[card.suit];
ctx.font = '20px Arial';
ctx.fillText(card.rank + card.suit, x + 5, y + 25);
} else {
// Draw just the top part of face-down cards
ctx.fillStyle = CARD_BACK_COLOR;
ctx.fillRect(x, y, CARD_WIDTH, CARD_SPACING);
ctx.strokeStyle = 'black';
ctx.strokeRect(x, y, CARD_WIDTH, CARD_SPACING);
}
y += CARD_SPACING;
}
}
}
function drawFoundation() {
for (let i = 0; i < 4; i++) {
const x = (i * (CARD_WIDTH + CARD_GAP)) + FOUNDATION_OFFSET_X;
const y = FOUNDATION_OFFSET_Y;
ctx.strokeStyle = '#888';
ctx.strokeRect(x, y, CARD_WIDTH, CARD_HEIGHT);
if (gameState.foundation[i].length > 0) {
drawCard(gameState.foundation[i][gameState.foundation[i].length - 1], x, y);
}
}
}
function drawWaste() {
if (gameState.waste.length > 0) {
const x = WASTE_OFFSET_X;
const y = WASTE_OFFSET_Y;
drawCard(gameState.waste[gameState.waste.length - 1], x, y);
}
}
function drawDeck() {
const x = DECK_OFFSET_X;
const y = DECK_OFFSET_Y;
ctx.fillStyle = CARD_BACK_COLOR;
ctx.fillRect(x, y, CARD_WIDTH, CARD_HEIGHT);
ctx.strokeStyle = 'black';
ctx.strokeRect(x, y, CARD_WIDTH, CARD_HEIGHT);
}
function drawGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawTableau();
drawFoundation();
drawWaste();
drawDeck();
}
function resetGame() {
gameState = initializeGameState();
gameState.deck = shuffleDeck();
dealCards();
drawGame();
}
function handleNewGameClick() {
resetGame();
}
function checkWin() {
if (gameState.won) return true;
return gameState.foundation.every(pile => pile.length === 13);
}
function celebrateWin() {
gameState.won = true;
document.body.style.background = 'linear-gradient(to right, violet, indigo, blue, green, yellow, orange, red)';
createConfetti();
}
function isValidMove(cardToMove, targetPile, isFoundation = false) {
if (isFoundation) {
if (targetPile.length === 0) {
return cardToMove.rank === 'A';
} else {
const lastCard = targetPile[targetPile.length - 1];
return cardToMove.suit === lastCard.suit &&
getRankValue(cardToMove.rank) === getRankValue(lastCard.rank) + 1;
}
} else {
if (targetPile.length === 0) {
return cardToMove.rank === 'K';
} else {
const lastCard = targetPile[targetPile.length - 1];
const isOppositeColor = (CARD_COLORS[lastCard.suit] !== CARD_COLORS[cardToMove.suit]);
return isOppositeColor && (getRankValue(lastCard.rank) === getRankValue(cardToMove.rank) + 1);
}
}
}
function getRankValue(rank) {
const rankValues = {'A': 1, 'J': 11, 'Q': 12, 'K': 13};
return rankValues[rank] || parseInt(rank);
}
function moveCards(sourceIndex, sourcePile, targetPile) {
const cardsToMove = sourcePile.splice(sourceIndex);
targetPile.push(...cardsToMove);
if (sourcePile.length > 0 && !sourcePile[sourcePile.length - 1].isFaceUp) {
sourcePile[sourcePile.length - 1].isFaceUp = true;
}
drawGame();
if (checkWin()) {
celebrateWin();
}
}
function moveCard(sourceIndex, sourcePile, targetPile, isFoundation = false) {
if (isFoundation) {
// Only allow moving the top card to foundation
if (sourceIndex !== sourcePile.length - 1) return;
}
const cardToMove = sourcePile[sourceIndex];
if (isValidMove(cardToMove, targetPile, isFoundation)) {
moveCards(sourceIndex, sourcePile, targetPile);
}
}
function isValidSequence(pile, startIndex) {
for (let i = startIndex; i < pile.length - 1; i++) {
if (getRankValue(pile[i].rank) !== getRankValue(pile[i+1].rank) + 1 ||
CARD_COLORS[pile[i].suit] === CARD_COLORS[pile[i+1].suit]) {
return false;
}
}
return true;
}
canvas.addEventListener('click', function(event) {
const x = event.offsetX;
const y = event.offsetY;
// Check if clicked on the deck
if (x >= DECK_OFFSET_X && x <= DECK_OFFSET_X + CARD_WIDTH && y >= DECK_OFFSET_Y && y <= DECK_OFFSET_Y + CARD_HEIGHT) {
if (gameState.deck.length > 0) {
const card = gameState.deck.pop();
card.isFaceUp = true;
gameState.waste.push(card);
} else {
gameState.deck = gameState.waste.reverse();
gameState.waste = [];
gameState.deck.forEach(card => card.isFaceUp = false);
}
drawGame();
return;
}
// Check if clicked on the waste pile
if (gameState.waste.length > 0) {
const wasteX = WASTE_OFFSET_X;
const wasteY = WASTE_OFFSET_Y;
if (x >= wasteX && x <= wasteX + CARD_WIDTH && y >= wasteY && y <= wasteY + CARD_HEIGHT) {
const card = gameState.waste[gameState.waste.length - 1];
for (let i = 0; i < 4; i++) {
if (isValidMove(card, gameState.foundation[i], true)) {
moveCard(gameState.waste.length - 1, gameState.waste, gameState.foundation[i], true);
return;
}
}
for (let i = 0; i < 7; i++) {
if (isValidMove(card, gameState.tableau[i])) {
moveCard(gameState.waste.length - 1, gameState.waste, gameState.tableau[i]);
return;
}
}
}
}
// Check if clicked on the foundation
for (let i = 0; i < 4; i++) {
const foundationX = (i * (CARD_WIDTH + CARD_GAP)) + FOUNDATION_OFFSET_X;
const foundationY = FOUNDATION_OFFSET_Y;
if (x >= foundationX && x <= foundationX + CARD_WIDTH && y >= foundationY && y <= foundationY + CARD_HEIGHT) {
if (gameState.foundation[i].length > 0) {
const card = gameState.foundation[i][gameState.foundation[i].length - 1];
for (let j = 0; j < 7; j++) {
if (isValidMove(card, gameState.tableau[j])) {
moveCard(gameState.foundation[i].length - 1, gameState.foundation[i], gameState.tableau[j]);
return;
}
}
}
return;
}
}
// Check if clicked on the tableau
for (let i = 0; i < 7; i++) {
const pileX = (i * (CARD_WIDTH + CARD_GAP)) + TABLEAU_OFFSET_X;
const pileY = TABLEAU_OFFSET_Y;
if (x >= pileX && x <= pileX + CARD_WIDTH) {
const pile = gameState.tableau[i];
let cardY = pileY;
for (let j = 0; j < pile.length; j++) {
if (y >= cardY && y <= cardY + CARD_SPACING) {
if (pile[j].isFaceUp) {
// Try to move to foundation first (only top card)
if (j === pile.length - 1) {
for (let k = 0; k < 4; k++) {
if (isValidMove(pile[j], gameState.foundation[k], true)) {
moveCard(j, pile, gameState.foundation[k], true);
return;
}
}
}
// If not possible or not top card, try to move to another tableau pile
for (let k = 0; k < 7; k++) {
if (k !== i && isValidSequence(pile, j) && isValidMove(pile[j], gameState.tableau[k])) {
moveCards(j, pile, gameState.tableau[k]);
return;
}
}
}
return;
}
cardY += CARD_SPACING;
}
}
}
});
function createConfetti() {
const confettiCount = 200;
const confetti = [];
for (let i = 0; i < confettiCount; i++) {
confetti.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: Math.random() * 5 + 5,
color: `hsl(${Math.random() * 360}, 100%, 50%)`,
speed: Math.random() * 3 + 1
});
}
function animateConfetti() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGame();
for (let i = 0; i < confetti.length; i++) {
ctx.fillStyle = confetti[i].color;
ctx.fillRect(confetti[i].x, confetti[i].y, confetti[i].size, confetti[i].size);
confetti[i].y += confetti[i].speed;
if (confetti[i].y > canvas.height) {
confetti[i].y = -confetti[i].size;
}
}
requestAnimationFrame(animateConfetti);
}
animateConfetti();
}
document.getElementById('newGameBtn').addEventListener('click', handleNewGameClick);
resetGame();
</script>
</body>
</html>