Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipChalupa committed Feb 2, 2025
1 parent a9d6d83 commit 6d61874
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/Components/Environment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<{
Expand Down Expand Up @@ -131,6 +133,33 @@ const In: FunctionComponent<ComponentProps<typeof Environment>> = ({
})
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
Expand Down Expand Up @@ -179,18 +208,6 @@ const In: FunctionComponent<ComponentProps<typeof Environment>> = ({
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 {
Expand Down
10 changes: 10 additions & 0 deletions src/utilities/countInstructions.ts
Original file line number Diff line number Diff line change
@@ -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,
)
50 changes: 50 additions & 0 deletions src/utilities/runEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { EnvironmentFoundation, type EnvironmentElement } from '../data/levels'
import type { Instructions } from './decodeCodeInstructions'

export function* runEnvironment(
foundations: Array<Array<EnvironmentFoundation>>,

Check failure on line 5 in src/utilities/runEnvironment.ts

View workflow job for this annotation

GitHub Actions / build

'foundations' is declared but its value is never read.
initialElements: Array<{
id: number
x: number
y: number
type: EnvironmentElement
}>,
instructions: Instructions,

Check failure on line 12 in src/utilities/runEnvironment.ts

View workflow job for this annotation

GitHub Actions / build

'instructions' is declared but its value is never read.
): Generator<
number /* @TODO*/,
| {
success: false
}
| {
success: true
performedNeedlessMove: boolean
},
void
> {
let elements = [...initialElements]

const elementsAtPosition = (x: number, y: number) =>

Check failure on line 26 in src/utilities/runEnvironment.ts

View workflow job for this annotation

GitHub Actions / build

'elementsAtPosition' is declared but its value is never read.
elements
.filter((element) => element.x === x && element.y === y)
.map(({ type }) => type)
const removeElement = (x: number, y: number, type: EnvironmentElement) => {

Check failure on line 30 in src/utilities/runEnvironment.ts

View workflow job for this annotation

GitHub Actions / build

'removeElement' is declared but its value is never read.
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,
}
}

0 comments on commit 6d61874

Please sign in to comment.