Skip to content

Create a Grid abstract data type #5

Description

@ericlippert

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.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions