diff --git a/components/SortingVisualization.tsx b/components/SortingVisualization.tsx deleted file mode 100644 index da18df6..0000000 --- a/components/SortingVisualization.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import { SortingStep } from "@/lib/types"; - -interface SortingVisualizationProps { - step: SortingStep; - maxValue: number; -} - -export default function SortingVisualization({ - step, - maxValue, -}: SortingVisualizationProps) { - const { array, comparing, swapped, completed } = step; - - return ( -
- {array.map((value, index) => { - const height = (value / maxValue) * 100; - - // Determine the bar color based on its state - let barColor = "bg-blue-400"; - if (completed.includes(index)) { - barColor = "bg-green-400"; - } else if (comparing.includes(index)) { - barColor = swapped ? "bg-red-400" : "bg-yellow-400"; - } - - return ( -
-
{value}
-
- ); - })} -
- ); -} diff --git a/components/visualizer/AlgorithmVisualizer.tsx b/components/visualizer/AlgorithmVisualizer.tsx index 716dc47..95fcb15 100644 --- a/components/visualizer/AlgorithmVisualizer.tsx +++ b/components/visualizer/AlgorithmVisualizer.tsx @@ -9,11 +9,10 @@ import ColorLegend from "./ColorLegend"; import { useAlgorithm } from "@/context/AlgorithmContext"; import { getAlgorithmByName } from "@/lib/algorithms"; import { SearchStep, SortingStep } from "@/lib/types"; -import { getRandomValueFromArray } from "@/lib/utils"; export default function AlgorithmVisualizer() { const { state, dispatch } = useAlgorithm(); - const { currentStep, algorithm, data, target, visualizationData } = state; + const { currentStep, algorithm, data, visualizationData } = state; // Generate a new random array const handleGenerateNewArray = () => { @@ -22,20 +21,12 @@ export default function AlgorithmVisualizer() { payload: { min: 5, max: 95, length: 15 }, }); - // Set a new random target for search algorithms - if (algorithm.includes("search")) { - const newData = data.slice(); - const newTarget = getRandomValueFromArray(newData); - dispatch({ type: "SET_TARGET", payload: newTarget }); - } - - // Regenerate visualization with the new data/target + // Regenerate visualization with the new data const algorithmFunction = getAlgorithmByName(algorithm); if (algorithmFunction) { try { - const viz = algorithm.includes("search") - ? algorithmFunction(data, target) - : algorithmFunction(data); + // For search algorithms, the target will be set in the reducer + const viz = algorithmFunction(data, state.target); dispatch({ type: "GENERATE_VISUALIZATION", payload: viz }); } catch (error) { console.error("Error generating visualization:", error); @@ -43,31 +34,9 @@ export default function AlgorithmVisualizer() { } }; - // Set a new target value for search algorithms - const handleSetTarget = () => { - // Only show for search algorithms - if (!algorithm.includes("search")) return; - - const newTarget = prompt( - "Enter a target value to search for:", - target?.toString() - ); - if (newTarget === null) return; // User canceled - - const parsedTarget = parseInt(newTarget); - if (isNaN(parsedTarget)) { - alert("Please enter a valid number"); - return; - } - - dispatch({ type: "SET_TARGET", payload: parsedTarget }); - - // Regenerate visualization with the new target - const algorithmFunction = getAlgorithmByName(algorithm); - if (algorithmFunction) { - const viz = algorithmFunction(data, parsedTarget); - dispatch({ type: "GENERATE_VISUALIZATION", payload: viz }); - } + // Check if the current algorithm is a search algorithm + const isSearchAlgorithm = () => { + return visualizationData?.category === "searching"; }; // Find the maximum value in the array for scaling @@ -84,8 +53,7 @@ export default function AlgorithmVisualizer() { ); } - const isSearchAlgorithm = algorithm.toLowerCase().includes("search"); - console.log(algorithm, isSearchAlgorithm); + const isSearching = isSearchAlgorithm(); return (
@@ -94,7 +62,7 @@ export default function AlgorithmVisualizer() {
- {isSearchAlgorithm ? ( + {isSearching ? ( - +
diff --git a/components/visualizer/SearchVisualization.tsx b/components/visualizer/SearchVisualization.tsx index 13a36d1..47bb8c0 100644 --- a/components/visualizer/SearchVisualization.tsx +++ b/components/visualizer/SearchVisualization.tsx @@ -1,4 +1,7 @@ import { SearchStep } from "@/lib/types"; +import { useAlgorithm } from "@/context/AlgorithmContext"; +import { getAlgorithmByName } from "@/lib/algorithms"; +import { useState, useEffect } from "react"; interface SearchVisualizationProps { step: SearchStep; @@ -9,13 +12,61 @@ export default function SearchVisualization({ step, }: SearchVisualizationProps) { const { array, current, target, found, visited } = step; + const { state, dispatch } = useAlgorithm(); + const [inputTarget, setInputTarget] = useState(target.toString()); + + // Update the input field when the target changes externally + useEffect(() => { + setInputTarget(target.toString()); + }, [target]); + + const handleTargetChange = (e: React.ChangeEvent) => { + const newValue = e.target.value; + setInputTarget(newValue); + + // Only process valid numbers + const newTarget = parseInt(newValue); + if (!isNaN(newTarget) && newTarget !== target) { + updateTarget(newTarget); + } + }; + + const updateTarget = (newTarget: number) => { + console.log(newTarget); + // Update the target value + dispatch({ type: "SET_TARGET", payload: newTarget }); + + // Regenerate visualization with the new target + const algorithmFunction = getAlgorithmByName(state.algorithm); + console.log(algorithmFunction); + if (algorithmFunction) { + try { + const viz = algorithmFunction(state.data, newTarget); + console.log(viz); + dispatch({ type: "GENERATE_VISUALIZATION", payload: viz }); + } catch (error) { + console.error("Error generating visualization:", error); + } + } + }; return (
-
-
Searching for value:
-
{target}
-
+
+
+ + +
+ +
Status: void; - onSetTarget?: () => void; } export default function VisualizerControls({ currentStep, totalSteps, onGenerateNewArray, - onSetTarget, }: VisualizerControlsProps) { const { state, dispatch } = useAlgorithm(); const { isPlaying, speed } = state; @@ -124,9 +122,9 @@ export default function VisualizerControls({
-
+
- Progress + Progress Step {currentStep + 1} of {totalSteps} @@ -210,30 +208,6 @@ export default function VisualizerControls({
- - {onSetTarget && ( -
- -
- )}
); }