diff --git a/src/components/Board.tsx b/src/components/Board.tsx index f7b6471..0c69f54 100644 --- a/src/components/Board.tsx +++ b/src/components/Board.tsx @@ -6,8 +6,8 @@ import PlayArrowIcon from "@mui/icons-material/PlayArrow" import StopIcon from "@mui/icons-material/Stop" import { - BOARD_HEIGHT, - BOARD_WIDTH, + DEFAULT_WIDTH, + DEFAULT_HEIGHT, DEFAULT_TILE_COLOR, SELECTED_TILE_COLOR, SIDEBAR_WIDTH, @@ -19,7 +19,7 @@ import { iterate, newProgram, ProgramState, resetProgram } from "../interpreter" function Board() { let key = 0 - const [program, setProgram] = useState(newProgram()) + const [program, setProgram] = useState(newProgram(DEFAULT_WIDTH, DEFAULT_HEIGHT)) const [playing, setPlaying] = useState(false) const intervalRef = useRef() @@ -111,7 +111,11 @@ function Board() { {/* BOARD */} {program.board.map((row, rowNo): JSX.Element => { return ( - + {row.map((tile, colNo): JSX.Element => { return (
{ +export const newProgram = (width: number, height: number) => { const program: ProgramState = { instructionPointer: { x: 0, y: 0 }, direction: Direction.Right, + width: width, + height: height, stack: [], - board: newBoard(), + board: newBoard(width, height), output: "", stringmode: false, done: true @@ -69,10 +73,10 @@ export const resetProgram = (program: ProgramState) => { * Initializes a 2D array of instructions with all elements set to the NOOP instruction * @returns Instruction[BOARD_HEIGHT][BOARD_WIDTH] */ -export const newBoard = () => { - return new Array(BOARD_HEIGHT) +export const newBoard = (width: number, height: number) => { + return new Array(height) .fill(0) - .map(() => new Array(BOARD_WIDTH).fill(0).map(() => structuredClone(instructions[0]))) + .map(() => new Array(width).fill(0).map(() => structuredClone(instructions[0]))) } /** @@ -99,9 +103,9 @@ const step = (program: ProgramState) => { program.instructionPointer.y += program.direction.y if ( - program.instructionPointer.x >= BOARD_WIDTH || + program.instructionPointer.x >= DEFAULT_WIDTH || program.instructionPointer.x < 0 || - program.instructionPointer.y >= BOARD_HEIGHT || + program.instructionPointer.y >= DEFAULT_HEIGHT || program.instructionPointer.y < 0 ) { console.log("pointer out of bounds, resetting program")