From 325b0dd139360cc53a2973a7e107805ad02d373e Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sat, 19 Jul 2025 14:55:51 +0000 Subject: [PATCH] Refactor Binary Search Trick interactive - Move share elements to the bottom. - Center "How it works" button. - Add one-by-one and all-at-once modes. - Randomize card numbers on load. --- src/components/BinarySearchTrick.tsx | 139 +++++++++++++++++++-------- 1 file changed, 101 insertions(+), 38 deletions(-) diff --git a/src/components/BinarySearchTrick.tsx b/src/components/BinarySearchTrick.tsx index 8909738..0745eae 100644 --- a/src/components/BinarySearchTrick.tsx +++ b/src/components/BinarySearchTrick.tsx @@ -1,8 +1,10 @@ -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; +import { Switch } from '@/components/ui/switch'; +import { Label } from '@/components/ui/label'; import { HelpCircle, RefreshCw } from 'lucide-react'; import SocialShare from '@/components/SocialShare'; @@ -14,35 +16,78 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) = const [selectedCards, setSelectedCards] = useState([]); const [result, setResult] = useState(null); const [showResult, setShowResult] = useState(false); + const [isOneByOne, setIsOneByOne] = useState(false); + const [visibleCards, setVisibleCards] = useState([]); + const [cards, setCards] = useState>([]); - // Define the 5 binary cards - const cards = [ - { - id: 16, - title: "Card 16", - numbers: [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] - }, - { - id: 8, - title: "Card 8", - numbers: [8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31] - }, - { - id: 4, - title: "Card 4", - numbers: [4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31] - }, - { - id: 2, - title: "Card 2", - numbers: [2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31] - }, - { - id: 1, - title: "Card 1", - numbers: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31] + // Function to shuffle array + const shuffleArray = (array: T[]): T[] => { + const shuffled = [...array]; + for (let i = shuffled.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; } - ]; + return shuffled; + }; + + // Generate cards with randomized numbers but maintaining binary logic + const generateCards = () => { + const baseCards = [ + { + id: 16, + title: "Card 16", + numbers: [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + }, + { + id: 8, + title: "Card 8", + numbers: [8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31] + }, + { + id: 4, + title: "Card 4", + numbers: [4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31] + }, + { + id: 2, + title: "Card 2", + numbers: [2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31] + }, + { + id: 1, + title: "Card 1", + numbers: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31] + } + ]; + + // Randomize the order of numbers within each card + return baseCards.map(card => ({ + ...card, + numbers: shuffleArray(card.numbers) + })); + }; + + // Initialize cards on component mount + useEffect(() => { + setCards(generateCards()); + setVisibleCards([16, 8, 4, 2, 1]); // All cards visible by default + }, []); + + // Handle mode toggle + const handleModeToggle = (checked: boolean) => { + setIsOneByOne(checked); + if (checked) { + // Start one-by-one mode - show first card + setVisibleCards([16]); + setTimeout(() => setVisibleCards(prev => [...prev, 8]), 500); + setTimeout(() => setVisibleCards(prev => [...prev, 4]), 1000); + setTimeout(() => setVisibleCards(prev => [...prev, 2]), 1500); + setTimeout(() => setVisibleCards(prev => [...prev, 1]), 2000); + } else { + // Show all cards immediately + setVisibleCards([16, 8, 4, 2, 1]); + } + }; const handleCardToggle = (cardId: number) => { setSelectedCards(prev => @@ -72,7 +117,7 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) = Binary Search Magic Trick -
+
- - {showSocialShare && ( - - )}
@@ -136,15 +174,31 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =

Think of a number between 1 and 31, then select all the cards that contain your number:

+ +
+ + + +
- {cards.map((card) => ( + {cards + .filter(card => visibleCards.includes(card.id)) + .map((card) => ( )} + + {showSocialShare && ( +
+ +
+ )}
);