The game will be built on two fundamental data types: object trees and grids. Let's make a start on a grid type.
A grid is a rectangular array of cells. We'll start with four operations:
- create a new grid with
g = new Grid(width, height, val); -- val is the default value for a "blank" grid. This should throw RangeError if width or height are less than zero. (A zero-width or zero-height grid is weird, but legal.)
- fetch a value with
g.get(x, y) -- this should throw if x or y are out of their proper range of 0 through width-1 or 0 through height-1
- set a value with
g.set(x, y, newval); -- same error handling
- Convert a grid into a string with
toString.
I've created a grid.js file with nothing in it except a class definition, and a test page at https://ericlippert.github.io/test.html.
How you choose to implement a grid is up to you but I would suggest an array of height rows, where each row is itself an array of width values, would probably be a good place to begin.
We'll add more operations to the grid type soon.
The game will be built on two fundamental data types: object trees and grids. Let's make a start on a grid type.
A grid is a rectangular array of cells. We'll start with four operations:
g = new Grid(width, height, val);--valis the default value for a "blank" grid. This should throwRangeErrorif width or height are less than zero. (A zero-width or zero-height grid is weird, but legal.)g.get(x, y)-- this should throw if x or y are out of their proper range of 0 throughwidth-1or 0 throughheight-1g.set(x, y, newval); -- same error handlingtoString.I've created a
grid.jsfile with nothing in it except a class definition, and a test page athttps://ericlippert.github.io/test.html.How you choose to implement a grid is up to you but I would suggest an array of
heightrows, where each row is itself an array ofwidthvalues, would probably be a good place to begin.We'll add more operations to the grid type soon.