From d01d9e82907938f601657d2cf5f60c7104ba7019 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Fri, 22 Aug 2025 06:55:10 +0000 Subject: [PATCH] Fix Stacking Blocks base cases Addressed bugs in the Stacking Blocks interactive, specifically concerning base cases. Here are the files that were edited by the AI in that message: -edited src/components/StackingBlocks.tsx --- src/components/StackingBlocks.tsx | 44 ++++++++++++++++++------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/components/StackingBlocks.tsx b/src/components/StackingBlocks.tsx index 2ccc84b..b1a81a6 100644 --- a/src/components/StackingBlocks.tsx +++ b/src/components/StackingBlocks.tsx @@ -39,20 +39,22 @@ const StackingBlocks: React.FC = ({ showSocialShare = false setHoveredSplitPoint(null); }, []); - const handleStackClick = useCallback((stackIndex: number, splitPoint: number) => { - if (hoveredStack !== stackIndex || hoveredSplitPoint !== splitPoint) return; + const handleStackClick = useCallback((stackIndex: number, bottomSize: number) => { + if (hoveredStack !== stackIndex || hoveredSplitPoint !== bottomSize) return; const stackSize = stacks[stackIndex]; - const size1 = splitPoint; - const size2 = stackSize - splitPoint; + const topSize = stackSize - bottomSize; + + // Validate the split + if (bottomSize <= 0 || topSize <= 0) return; // Calculate points gained - const pointsGained = size1 * size2; + const pointsGained = bottomSize * topSize; // Update stacks const newStacks = [...stacks]; newStacks.splice(stackIndex, 1); // Remove the original stack - newStacks.push(size1, size2); // Add the two new stacks + newStacks.push(bottomSize, topSize); // Add the two new stacks newStacks.sort((a, b) => b - a); // Sort in descending order for better visualization setStacks(newStacks); @@ -87,11 +89,17 @@ const StackingBlocks: React.FC = ({ showSocialShare = false
Stack {stackIndex + 1}
{Array.from({ length: size }).map((_, blockIndex) => { - const splitPoint = size - blockIndex; // How many blocks would be in the top stack if we split here - const bottomSize = blockIndex + 1; // How many blocks would be in the bottom stack - const pointsGained = canSplit && splitPoint > 0 && bottomSize > 0 ? splitPoint * bottomSize : 0; - const isHovered = hoveredStack === stackIndex && hoveredSplitPoint === splitPoint; - const showSplitLine = canSplit && splitPoint > 0 && bottomSize > 0 && isHovered; + // Split happens ABOVE the hovered block + // Bottom stack gets blocks 0 to blockIndex (inclusive) + // Top stack gets blocks blockIndex+1 to size-1 (inclusive) + const bottomSize = blockIndex + 1; + const topSize = size - bottomSize; + + // Only allow splits that create two non-empty stacks + const canSplitHere = canSplit && topSize > 0 && bottomSize > 0; + const pointsGained = canSplitHere ? topSize * bottomSize : 0; + const isHovered = hoveredStack === stackIndex && hoveredSplitPoint === bottomSize; + const showSplitLine = canSplitHere && isHovered; return ( @@ -99,13 +107,13 @@ const StackingBlocks: React.FC = ({ showSocialShare = false
0 && bottomSize > 0 ? 'cursor-pointer hover:brightness-90' : '' + canSplitHere ? 'cursor-pointer hover:brightness-90' : '' } ${showSplitLine ? 'border-t-4 border-t-red-500' : ''} relative`} style={{ marginBottom: '-1px' }} onMouseEnter={() => { - if (canSplit && splitPoint > 0 && bottomSize > 0) { + if (canSplitHere) { setHoveredStack(stackIndex); - setHoveredSplitPoint(splitPoint); + setHoveredSplitPoint(bottomSize); } }} onMouseLeave={() => { @@ -113,15 +121,15 @@ const StackingBlocks: React.FC = ({ showSocialShare = false setHoveredSplitPoint(null); }} onClick={() => { - if (canSplit && splitPoint > 0 && bottomSize > 0) { - handleStackClick(stackIndex, splitPoint); + if (canSplitHere) { + handleStackClick(stackIndex, bottomSize); } }} /> - {canSplit && pointsGained > 0 && ( + {canSplitHere && ( -

Split into {splitPoint} + {bottomSize} = {pointsGained} points

+

Split into {bottomSize} + {topSize} = {pointsGained} points

)}