8 package org.bitstorm.gameoflife;
10 import java.awt.Dimension;
11 import java.util.Enumeration;
12 import java.util.Hashtable;
44 currentShape =
new Hashtable();
45 nextShape =
new Hashtable();
50 grid[c][r] =
new Cell( c, r );
56 public synchronized void clear() {
65 public synchronized void next() {
75 enum = currentShape.keys();
76 while (
enum.hasMoreElements() ) {
77 cell = (
Cell)
enum.nextElement();
83 enum = currentShape.keys();
84 while (
enum.hasMoreElements() ) {
85 cell = (
Cell)
enum.nextElement();
100 enum = currentShape.keys();
101 while (
enum.hasMoreElements() ) {
102 cell = (
Cell)
enum.nextElement();
105 currentShape.remove( cell );
109 enum = nextShape.keys();
110 while (
enum.hasMoreElements() ) {
111 cell = (
Cell)
enum.nextElement();
127 Cell cell = (
Cell)nextShape.get( grid[col][row] );
128 if ( cell == null ) {
130 Cell c = grid[col][row];
137 }
catch (ArrayIndexOutOfBoundsException e) {
147 return currentShape.keys();
156 public synchronized boolean getCell(
int col,
int row ) {
158 return currentShape.containsKey(grid[col][row]);
159 }
catch (ArrayIndexOutOfBoundsException e) {
171 public synchronized void setCell(
int col,
int row,
boolean c ) {
173 Cell cell = grid[col][row];
175 currentShape.put(cell, cell);
177 currentShape.remove(cell);
179 }
catch (ArrayIndexOutOfBoundsException e) {
197 return new Dimension( cellCols, cellRows );
204 public synchronized void resize(
int cellColsNew,
int cellRowsNew) {
205 if ( cellCols==cellColsNew && cellRows==cellRowsNew )
209 Cell[][] gridNew =
new Cell[cellColsNew][cellRowsNew];
210 for (
int c=0; c<cellColsNew; c++)
211 for (
int r=0; r<cellRowsNew; r++ )
212 if ( c < cellCols && r < cellRows )
213 gridNew[c][r] = grid[c][r];
215 gridNew[c][r] =
new Cell( c, r );
218 int colOffset = (cellColsNew-
cellCols)/2;
219 int rowOffset = (cellRowsNew-
cellRows)/2;
223 enum = currentShape.keys();
224 while (
enum.hasMoreElements() ) {
225 cell = (
Cell)
enum.nextElement();
226 int colNew = cell.
col + colOffset;
227 int rowNew = cell.
row + rowOffset;
229 nextShape.put( gridNew[colNew][rowNew], gridNew[colNew][rowNew] );
230 }
catch ( ArrayIndexOutOfBoundsException e ) {
237 currentShape.clear();
238 enum = nextShape.keys();
239 while (
enum.hasMoreElements() ) {
240 cell = (
Cell)
enum.nextElement();
241 currentShape.put( cell, cell );
244 cellCols = cellColsNew;
245 cellRows = cellRowsNew;
synchronized void resize(int cellColsNew, int cellRowsNew)
synchronized boolean getCell(int col, int row)
synchronized void setCell(int col, int row, boolean c)
synchronized void addNeighbour(int col, int row)
synchronized void clear()
GameOfLifeGrid(int cellCols, int cellRows)