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.
This commit is contained in:
parent
8917e1a66f
commit
d57837d286
1 changed files with 30 additions and 20 deletions
|
|
@ -51,41 +51,43 @@ const PebblePlacementGame: React.FC<PebblePlacementGameProps> = ({ 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;
|
||||
|
||||
for (const laterPebble of pebblesAfter) {
|
||||
if (laterPebble === position + 1) {
|
||||
// Direct successor would become invalid
|
||||
canRemove = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (canRemove) {
|
||||
const newPlacedPebbles = new Set(placedPebbles);
|
||||
newPlacedPebbles.delete(position);
|
||||
setPlacedPebbles(newPlacedPebbles);
|
||||
setPebblesInHand(pebblesInHand + 1);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
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<PebblePlacementGameProps> = ({ showSocialSha
|
|||
<li>• You can always place a pebble on position 1</li>
|
||||
<li>• You can place a pebble on position i only if position (i-1) has a pebble</li>
|
||||
<li>• You can remove a pebble from position i only if position (i-1) has a pebble</li>
|
||||
<li>• The pebble on position 1 is always removable</li>
|
||||
<li>• Removing a pebble may cascade and remove other pebbles that become invalid</li>
|
||||
<li>• Goal: Place a pebble on the largest possible position</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
|
@ -218,9 +222,15 @@ const PebblePlacementGame: React.FC<PebblePlacementGameProps> = ({ showSocialSha
|
|||
<p className="text-muted-foreground">
|
||||
Your furthest position: <span className="font-semibold text-foreground">{furthestReached}</span>
|
||||
</p>
|
||||
{hasAnyRemovablePebble() ? (
|
||||
<p className="text-sm text-muted-foreground mt-2">
|
||||
Can you rearrange your pebbles to reach even further?
|
||||
You can still rearrange your pebbles to try reaching further!
|
||||
</p>
|
||||
) : (
|
||||
<p className="text-sm text-muted-foreground mt-2">
|
||||
No more moves possible - great job!
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue