From a590615cefa5a5c6f314a09ba20f88f51f241c46 Mon Sep 17 00:00:00 2001 From: Lewis Cowles Date: Sat, 14 Sep 2024 17:29:31 +0100 Subject: [PATCH] feat: 3-card mode --- components/klondike-solitaire.tsx | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/components/klondike-solitaire.tsx b/components/klondike-solitaire.tsx index 134d257..743c5d2 100644 --- a/components/klondike-solitaire.tsx +++ b/components/klondike-solitaire.tsx @@ -54,14 +54,22 @@ export function KlondikeSolitaireComponent() { const drawCards = () => { if (deck.length === 0) { + // If the deck is empty, move waste back into deck (reversed and face down) setDeck(waste.reverse().map(card => ({ ...card, faceUp: false }))); setWaste([]); } else { + // Determine how many cards to draw based on the game mode const numCardsToDraw = gameMode === '1-card' ? 1 : 3; + + // Draw the cards from the deck and flip them face up const drawnCards = deck.slice(0, numCardsToDraw).map(card => ({ ...card, faceUp: true })); + + // Add drawn cards to the waste pile, removing them from the deck setWaste([...drawnCards, ...waste]); setDeck(deck.slice(numCardsToDraw)); } + + // Reset the selected waste card after drawing setSelectedWasteCard(false); }; @@ -199,8 +207,8 @@ export function KlondikeSolitaireComponent() {
- - + +
@@ -209,9 +217,11 @@ export function KlondikeSolitaireComponent() { onClick={drawCards}> {deck.length > 0 ? deck.length : 'R'}
-
- {waste.length > 0 && renderCard(waste[0], selectedWasteCard)} -
+ {waste.slice(0, gameMode === '3-card' ? 3: 1).map((wasteCard, k) => ( +
+ {renderCard(wasteCard, selectedWasteCard)} +
+ ))}
{foundation.map((pile, index) => (