Thursday, February 10, 2005

Conway Game World

I think the world can easily be represented as a set of Cells, and we can think about a layout or topography as just a rule for where a Cell is allowed to be placed. I don't like the idea of using any 2 dimensional lists, arrays, or the like, because that becomes a mess and is much harder to change later if it's in use.

Thinking about the features of a Cell, a Cell should probably have a Position. A Position could be a point in 2 or 3 dimensional space (or maybe additional dimensions as well in case I'm being short sighted). The important feature of a Position should be that it can calculate distance from another Position. Cells can use this distance to figure out who their neighbors are. Consider this code:

class Cell {


public boolean isNeighbor(Cell c){
/* this assumes the neigbor distance limit is 1 unit
as in Conway's Life */
return (this.position().distanceTo(c.position()) <= 1.0)

public Position position(){
return p;

private Position p;

Can also create subsets of our Game World's Cell set by filtering on Cell's isNeighbor(Cell) function, thereby just having to get the size of the subset to determine the number of neighbors.

And thinking about what the presentation will need to display the Game World, I think the model will need to have a query to get a copy of the Cell set.

No comments: