diff --git a/src/Components/Environment.tsx b/src/Components/Environment.tsx index 9bd86ed..dc39d60 100644 --- a/src/Components/Environment.tsx +++ b/src/Components/Environment.tsx @@ -29,11 +29,13 @@ import { Level, } from '../data/levels' import { ConditionValue } from '../utilities/blocks' +import { countInstructions } from '../utilities/countInstructions' import { InstructionBlock, Instructions, } from '../utilities/decodeCodeInstructions' import type { EditorXml } from '../utilities/editorXml' +import { runEnvironment } from '../utilities/runEnvironment' import styles from './Environment.module.css' export const Environment: FunctionComponent<{ @@ -131,6 +133,33 @@ const In: FunctionComponent> = ({ }) const [elements, setElements] = useState(elementsWithIds) + useEffect(() => { + if (instructions === null) { + return + } + const runtime = runEnvironment( + completeFoundations, + elementsWithIds, + instructions.instructions, + ) + while (instructions !== null) { + const { value, done } = runtime.next() + if (done) { + if (value.success) { + onSuccess( + instructions.xml, + value.performedNeedlessMove, + countInstructions(instructions.instructions), + ) + } else { + onFail() + } + break + } + console.log('Yielded:', value) + } + }, [completeFoundations, elementsWithIds, instructions, onSuccess, onFail]) + useEffect(() => { if (isDoneRunningRef.current) { return @@ -179,18 +208,6 @@ const In: FunctionComponent> = ({ const loop = (lastRunSuccess: null | boolean) => { if (lastRunSuccess !== null) { if (lastRunSuccess) { - const countInstructions = ( - instructions: InstructionBlock[], - ): number => - instructions.reduce( - (count, instruction) => - count + - 1 + - ('blocks' in instruction - ? countInstructions(instruction.blocks) - : 0), - 0, - ) const instructionsCount = countInstructions(instructions.instructions) onSuccess(instructions.xml, performedNeedlessMove, instructionsCount) } else { diff --git a/src/utilities/countInstructions.ts b/src/utilities/countInstructions.ts new file mode 100644 index 0000000..163f758 --- /dev/null +++ b/src/utilities/countInstructions.ts @@ -0,0 +1,10 @@ +import type { InstructionBlock } from './decodeCodeInstructions' + +export const countInstructions = (instructions: InstructionBlock[]): number => + instructions.reduce( + (count, instruction) => + count + + 1 + + ('blocks' in instruction ? countInstructions(instruction.blocks) : 0), + 0, + ) diff --git a/src/utilities/runEnvironment.ts b/src/utilities/runEnvironment.ts new file mode 100644 index 0000000..3c27792 --- /dev/null +++ b/src/utilities/runEnvironment.ts @@ -0,0 +1,50 @@ +import { EnvironmentFoundation, type EnvironmentElement } from '../data/levels' +import type { Instructions } from './decodeCodeInstructions' + +export function* runEnvironment( + foundations: Array>, + initialElements: Array<{ + id: number + x: number + y: number + type: EnvironmentElement + }>, + instructions: Instructions, +): Generator< + number /* @TODO*/, + | { + success: false + } + | { + success: true + performedNeedlessMove: boolean + }, + void +> { + let elements = [...initialElements] + + const elementsAtPosition = (x: number, y: number) => + elements + .filter((element) => element.x === x && element.y === y) + .map(({ type }) => type) + const removeElement = (x: number, y: number, type: EnvironmentElement) => { + elements = elements.filter( + (element) => element.x !== x || element.y !== y || element.type !== type, + ) + } + + yield 1 + yield 2 + yield 3 + yield 4 + + if (Math.random() > 0) { + return { + success: false, + } + } + return { + success: true, + performedNeedlessMove: false, + } +}