diff --git a/src/components/BoxTable.tsx b/src/components/BoxTable.tsx index 171e900..e27c3d5 100644 --- a/src/components/BoxTable.tsx +++ b/src/components/BoxTable.tsx @@ -3,7 +3,7 @@ import BoxGraphic from "./BoxGraphic"; import Xarrow from "react-xarrows"; import Chance from "chance"; -import {useState} from "react"; +import {useMemo, useState} from "react"; const chance = new Chance(); const colors = [ @@ -26,11 +26,23 @@ const colors = [ "#F43F5E", ] +const smartShuffle = (n: number[]): number[] => { + let shuffled: number[] | null = null; + while (shuffled == null || shuffled.some((v, i) => v == i + 1)) { + shuffled = chance.shuffle(n) + } + return shuffled; +} + const BoxTable = () => { - const [sources] = useState(range(1, 100)); - const [destinations] = useState(chance.shuffle(sources)); - const [boxes] = useState<[number, number][]>(sources.map((e, i) => [e, destinations[i] as number])) - const [boxesBySource] = useState>(Object.fromEntries(boxes)); + const [count, setCount] = useState(100); + const {sources, boxes, boxesBySource} = useMemo(() => { + const sources: number[] = range(1, count); + const destinations: number[] = smartShuffle(sources); + const boxes: [number, number][] = sources.map((e, i) => [e, destinations[i] as number]); + const boxesBySource: Record = Object.fromEntries(boxes); + return {sources, destinations, boxes, boxesBySource}; + }, [count]) const extractLoop = (start: number): number[] => { const results: number[] = [];