From d57837d2864ef1c001c9ea6e7942ad93e69ac825 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Thu, 21 Aug 2025 10:47:57 +0000 Subject: [PATCH] Update pebble game rules Allow the user to continue playing the pebble game as long as there are removable pebbles. The pebble at position 1 is always considered removable. --- src/components/PebblePlacementGame.tsx | 50 +++++++++++++++----------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/components/PebblePlacementGame.tsx b/src/components/PebblePlacementGame.tsx index 9369c40..a8300a9 100644 --- a/src/components/PebblePlacementGame.tsx +++ b/src/components/PebblePlacementGame.tsx @@ -51,41 +51,43 @@ const PebblePlacementGame: React.FC = ({ showSocialSha }; const canRemovePebble = (position: number): boolean => { - // Can only remove if pebble is placed and predecessor has pebble (or position 1) + // Can only remove if pebble is placed if (!placedPebbles.has(position)) return false; + // Position 1 is always removable if (position === 1) return true; + // For other positions, predecessor must have pebble return placedPebbles.has(position - 1); }; + const hasAnyRemovablePebble = (): boolean => { + return Array.from(placedPebbles).some(pos => canRemovePebble(pos)); + }; + const handlePositionClick = (position: number) => { if (!gameStarted) return; - const newPlacedPebbles = new Set(placedPebbles); - if (placedPebbles.has(position)) { // Try to remove pebble if (canRemovePebble(position)) { - // Check if removing this pebble would make any later pebbles invalid - const pebblesAfter = Array.from(placedPebbles).filter(p => p > position); - let canRemove = true; + const newPlacedPebbles = new Set(placedPebbles); + newPlacedPebbles.delete(position); - for (const laterPebble of pebblesAfter) { - if (laterPebble === position + 1) { - // Direct successor would become invalid - canRemove = false; - break; + // Cascade removal: remove any pebbles that become invalid + const sortedPebbles = Array.from(newPlacedPebbles).sort((a, b) => a - b); + for (const pebblePos of sortedPebbles) { + if (pebblePos > 1 && !newPlacedPebbles.has(pebblePos - 1)) { + newPlacedPebbles.delete(pebblePos); } } - - if (canRemove) { - newPlacedPebbles.delete(position); - setPlacedPebbles(newPlacedPebbles); - setPebblesInHand(pebblesInHand + 1); - } + + const pebblesRemoved = placedPebbles.size - newPlacedPebbles.size; + setPlacedPebbles(newPlacedPebbles); + setPebblesInHand(pebblesInHand + pebblesRemoved); } } else { // Try to place pebble if (pebblesInHand > 0 && canPlacePebble(position)) { + const newPlacedPebbles = new Set(placedPebbles); newPlacedPebbles.add(position); setPlacedPebbles(newPlacedPebbles); setPebblesInHand(pebblesInHand - 1); @@ -168,6 +170,8 @@ const PebblePlacementGame: React.FC = ({ showSocialSha
  • • You can always place a pebble on position 1
  • • You can place a pebble on position i only if position (i-1) has a pebble
  • • You can remove a pebble from position i only if position (i-1) has a pebble
  • +
  • • The pebble on position 1 is always removable
  • +
  • • Removing a pebble may cascade and remove other pebbles that become invalid
  • • Goal: Place a pebble on the largest possible position
  • @@ -218,9 +222,15 @@ const PebblePlacementGame: React.FC = ({ showSocialSha

    Your furthest position: {furthestReached}

    -

    - Can you rearrange your pebbles to reach even further? -

    + {hasAnyRemovablePebble() ? ( +

    + You can still rearrange your pebbles to try reaching further! +

    + ) : ( +

    + No more moves possible - great job! +

    + )} )}