1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { encodePosition } from "https://esm.town/v/beneskildsen/encodePosition";
import { getNextDir } from "https://esm.town/v/beneskildsen/getNextDir";
import { config } from "https://esm.town/v/beneskildsen/config";
export const genGrid = (state) => {
const locStack = [];
let loc = { ...state.initialPosition };
let gridMap = {};
let dir = "UP";
let i = 0;
for (let c of state.grammar) {
switch (c) {
case "[":
locStack.push({ ...loc });
break;
case "]":
loc = locStack.pop();
break;
case "^":
dir = "UP";
loc.y -= 1;
break;
case "V":
dir = "DOWN";
loc.y += 1;
break;
case "<":
dir = "LEFT";
loc.x -= 1;
break;
case ">":
dir = "RIGHT";
loc.x += 1;
break;
default:
const symbol = config.symbols[c];
let nextDir = getNextDir(state.grammar, i);
if (symbol) {
gridMap[encodePosition(loc)] = {
index: i,
dir,
nextDir,
symbol: c,
};
}
}
i++;
}
return gridMap;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023