make width and height dynamic

This commit is contained in:
Ole Morud
2022-08-25 23:54:25 +02:00
parent d8d0126662
commit c1bdbc7060
3 changed files with 22 additions and 14 deletions

View File

@@ -6,8 +6,8 @@ import PlayArrowIcon from "@mui/icons-material/PlayArrow"
import StopIcon from "@mui/icons-material/Stop" import StopIcon from "@mui/icons-material/Stop"
import { import {
BOARD_HEIGHT, DEFAULT_WIDTH,
BOARD_WIDTH, DEFAULT_HEIGHT,
DEFAULT_TILE_COLOR, DEFAULT_TILE_COLOR,
SELECTED_TILE_COLOR, SELECTED_TILE_COLOR,
SIDEBAR_WIDTH, SIDEBAR_WIDTH,
@@ -19,7 +19,7 @@ import { iterate, newProgram, ProgramState, resetProgram } from "../interpreter"
function Board() { function Board() {
let key = 0 let key = 0
const [program, setProgram] = useState<ProgramState>(newProgram()) const [program, setProgram] = useState<ProgramState>(newProgram(DEFAULT_WIDTH, DEFAULT_HEIGHT))
const [playing, setPlaying] = useState<boolean>(false) const [playing, setPlaying] = useState<boolean>(false)
const intervalRef = useRef<NodeJS.Timer>() const intervalRef = useRef<NodeJS.Timer>()
@@ -111,7 +111,11 @@ function Board() {
{/* BOARD */} {/* BOARD */}
{program.board.map((row, rowNo): JSX.Element => { {program.board.map((row, rowNo): JSX.Element => {
return ( return (
<Grid key={key++} container columns={BOARD_WIDTH} sx={{ width: `${6 * BOARD_WIDTH}rem` }}> <Grid
key={key++}
container
columns={program.width}
sx={{ width: `${6 * program.width}rem` }}>
{row.map((tile, colNo): JSX.Element => { {row.map((tile, colNo): JSX.Element => {
return ( return (
<div <div

View File

@@ -1,8 +1,8 @@
export const SIDEBAR_WIDTH = 8 export const SIDEBAR_WIDTH = 8
export const SELECTED_TILE_COLOR = "Coral" export const SELECTED_TILE_COLOR = "Coral"
export const DEFAULT_TILE_COLOR = "0xFFFFFF" export const DEFAULT_TILE_COLOR = "0xFFFFFF"
export const BOARD_WIDTH = 20 export const DEFAULT_WIDTH = 20
export const BOARD_HEIGHT = 10 export const DEFAULT_HEIGHT = 10
export const TILE_SIZE = 5 export const TILE_SIZE = 5
export const STEP_DELAY = 300 export const STEP_DELAY = 300

View File

@@ -1,9 +1,11 @@
import { BOARD_HEIGHT, BOARD_WIDTH } from "./const" import { DEFAULT_HEIGHT, DEFAULT_WIDTH } from "./const"
import instructions, { Instruction } from "./instructions" import instructions, { Instruction } from "./instructions"
export interface ProgramState { export interface ProgramState {
instructionPointer: Point instructionPointer: Point
direction: Point direction: Point
width: number
height: number
stack: number[] stack: number[]
board: Instruction[][] board: Instruction[][]
output: string output: string
@@ -27,12 +29,14 @@ const Direction = {
* Creates a new program * Creates a new program
* @returns new program object with initialized attributes * @returns new program object with initialized attributes
*/ */
export const newProgram = () => { export const newProgram = (width: number, height: number) => {
const program: ProgramState = { const program: ProgramState = {
instructionPointer: { x: 0, y: 0 }, instructionPointer: { x: 0, y: 0 },
direction: Direction.Right, direction: Direction.Right,
width: width,
height: height,
stack: [], stack: [],
board: newBoard(), board: newBoard(width, height),
output: "", output: "",
stringmode: false, stringmode: false,
done: true 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 * Initializes a 2D array of instructions with all elements set to the NOOP instruction
* @returns Instruction[BOARD_HEIGHT][BOARD_WIDTH] * @returns Instruction[BOARD_HEIGHT][BOARD_WIDTH]
*/ */
export const newBoard = () => { export const newBoard = (width: number, height: number) => {
return new Array(BOARD_HEIGHT) return new Array(height)
.fill(0) .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 program.instructionPointer.y += program.direction.y
if ( if (
program.instructionPointer.x >= BOARD_WIDTH || program.instructionPointer.x >= DEFAULT_WIDTH ||
program.instructionPointer.x < 0 || program.instructionPointer.x < 0 ||
program.instructionPointer.y >= BOARD_HEIGHT || program.instructionPointer.y >= DEFAULT_HEIGHT ||
program.instructionPointer.y < 0 program.instructionPointer.y < 0
) { ) {
console.log("pointer out of bounds, resetting program") console.log("pointer out of bounds, resetting program")