public void SetNeighbors(GameCell[] newNeighbors) { // can only set neighbors once // just because they must be bound late doesn't mean they're mutable! if (neighbors == null) neighbors = newNeighbors; else throw new InvalidOperationException(ERR_CANT_CHANGE_NEIGHBORS); }
private GameCell[] CollectNeighbors(int x, int y) { // collect all neighboring Cell objects // GetCell() performs coord normalization var neighbors = new GameCell[8]; neighbors[0] = this[x - 1, y - 1]; // left-top neighbors[1] = this[x - 1, y]; // left neighbors[2] = this[x - 1, y + 1]; // left-bottom neighbors[3] = this[x, y - 1]; // top neighbors[4] = this[x, y + 1]; // bottom neighbors[5] = this[x + 1, y - 1]; // right-top neighbors[6] = this[x + 1, y]; // right neighbors[7] = this[x + 1, y + 1]; // right-bottom return neighbors; }
private void InitializeCells() { // here's the meat // messy, but only done once int w, h; // populate each grid point with a cell object for (w = 0; w < FIELD_WIDTH; ++w) for (h = 0; h < FIELD_HEIGHT; ++h) field[w, h] = new GameCell(); // populate the neighbors array for each cell for (w = 0; w < FIELD_WIDTH; ++w) for (h = 0; h < FIELD_HEIGHT; ++h) field[w, h].SetNeighbors(CollectNeighbors(w,h)); }