Conway`s Game of Life - Computer Science

Conway’s Game of Life
Computer Science 1710:
Object-Oriented Programming I
Department of Computer Science
Memorial University of Newfoundland
July 30, 2007
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
1 / 18
1
Conway’s game of life
1
The rules
1
Implementation
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
2 / 18
Conway’s game of life
This “game” is not so much a game, as it is a simulation. Ideally, the
game is carried out on an infinite two-dimensional array of cells.
The cells are considered either alive or dead. At each step of the game a
cell can either stay alive (or stay dead), die, or be reborn. What occurs
depends upon the number of living neighbours of the cell during the
previous step.
The cell’s neighbours are other cells that are either vertically, horizontally,
or diagonally adjacent.
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
3 / 18
The rules
At each step, we count the number of living neighbours of each cell:
A live cell with ≤ 1 neighbours dies (of loneliness)
A live cell with ≥ 4 neighbours dies (of overcrowding)
A dead cell with exactly 3 neighbours becomes alive
If none of the above apply, the cell maintains its current state.
These simple rules lead to very interesting and complex behaviour that has
the attracted the attention of mathematicians, physicists, biologists,
economists, and (of course) computer scientists...
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
4 / 18
Implementation
The board:
We cannot represent the game board using an infinite array. Instead we
use an array of some fixed size (e.g. 100 × 100).
The borders of the array present a challenge. We handle them simply by
not updating any cell that borders on the edge of the array.
The classes:
The GameGUI class from the stick man game example has been modified to
incorporate mouse input and provide a start/stop button. The new class is
called LifeGUI.
Again, we have not gone far enough in COMP 1710 to fully understand
this code. The following slide gives the documentation for the class and its
constructor.
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
5 / 18
/∗ ∗
A g r a p h i c a l u s e r i n t e r f a c e f o r Conway ’ s game o f l i f e ( s e e
Life . java ).
P r o v i d e s a JFrame f o r g r a p h i c a l d i s p l a y and
t h e a b i l i t y t o r e c e i v e mouse i n p u t from t h e u s e r when t h e
JFrame h a s t h e f o c u s .
An i n s t a n c e o f L i f e G U I w i l l c a l l t h e ’ s t e p ’ method o f t h e
L i f e o b j e c t e v e r y DELAY m i l l i s e c o n d s . The ’ s t e p ’ method
must be d e f i n e d a s f o l l o w s :
p u b l i c v o i d s t e p ( G r a p h i c s g , i n t w id th , i n t h e i g h t ,
b o o l e a n mouseEvent , i n t x , i n t y , b o o l e a n r u n ) ;
where g i s t h e g r a p h i c s c o n t e x t o f t h e GameGUI ’ s JComponent ,
w i d t h and h e i g h t a r e t h e d i m e n s i o n s o f t h e JComponent ,
mouseEvent i s t r u e i f t h e u s e r h a s j u s t p r e s s e d t h e l e f t
mouse b u t t o n w i t h ( x , y ) b e i n g t h e c o o r d i n a t e s o f t h e mouse
event in p i x e l s .
Final , run i n d i c a t e s that the s i m u l a t i o n
should proceed .
∗/
public class LifeGUI
{
/∗ ∗
Constructor .
@param i n L i f e t h e i n s t a n c e o f L i f e whose ’ s t e p ’ method
w i l l be c a l l e d e v e r y DELAY m i l l i s e c o n d s
@param w i d t h t h e w i d t h o f t h e JFrame i n p i x e l s
@param h e i g h t t h e h e i g h t o f t h e JFrame i n p i x e l s
@param t i t l e t h e t e x t t o a p p e a r i n t h e t i t l e b a r o f
t h e JFrame
∗/
public LifeGUI ( Life inLife , int width , int height ,
String title ) {
...
The Life class creates the LifeGUI and Board objects and manages
communication between the two. It also contains the main method (so the
program is run by executing java Life.
The following shows the code for Life (import statements and comments
removed):
public class Life
{
public Life ( ) {
board = new Board ( ) ;
lifeGUI = new LifeGUI ( this , WIDTH , HEIGHT , " The Game of
}
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
8 / 18
Here is the code for the very important step method which is repeatedly
called by LifeGUI and includes information about mouse input (mouseEvent,
x, y) and indicates whether the rules of the game should be applied (run).
public void step ( Graphics g , int width , int height ,
boolean mouseEvent , int x , int y , boolean run )
{
Graphics2D g2d = ( Graphics2D ) g ;
if ( mouseEvent )
board . setByScreenPos ( width , height , x , y , true ) ;
if ( run )
board . update ( ) ;
board . draw ( g2d , width , height ) ;
}
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
9 / 18
The remainder of the class consists of the main method and the class’s
instance fields:
public static void main ( String [ ] args )
{
new Life ( ) ;
}
private
private
private
private
LifeGUI lifeGUI ;
Board board ;
static final int WIDTH = 6 0 0 ;
static final int HEIGHT = 6 0 0 ;
}
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
10 / 18
Finally, we have the Board class. The job of this class is to store the
contents of the board as a 2-D array.
Board
has the following fields:
private
private
private
private
static final int ROWS = 8 0 ;
static final int COLS = 8 0 ;
boolean [ ] [ ] array ;
boolean [ ] [ ] tempArray ;
will be used in the update method. It is needed because update
determines the next state of the array based on the previous state. Two
arrays are required so that cells updated for the next state don’t affect the
update of other cells.
tempArray
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
11 / 18
/∗ ∗
Constructor .
C r e a t e s t h e b o a r d a r r a y and a t e m p o r a r y a r r a y .
∗/
public Board ( )
{
array = new boolean [ ROWS ] [ COLS ] ;
tempArray = new boolean [ ROWS ] [ COLS ] ;
}
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
12 / 18
/∗ ∗
D e t e r m i n e s t h e c e l l s e l e c t e d by a mouse p r e s s on p o s i t i o n
( x , y ) when t h e b o a r d i t s e l f i s drawn on a w i d t h x h e i g h t
surface , then s e t s the value of t h i s c e l l to the given
value .
∗/
public void setByScreenPos ( int width , int height ,
int x , int y , boolean value )
{
int cellWidth = width / COLS ;
int cellHeight = height / ROWS ;
int r = y / cellHeight ;
int c = x / cellWidth ;
if ( r < ROWS && c < COLS )
{
array [ r ] [ c ] = value ;
tempArray [ r ] [ c ] = value ;
}
}
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
13 / 18
/∗ ∗
A p p l i e s t h e r u l e s o f Conway ’ s Game o f L i f e .
∗/
public void update ( )
{
// Update a l l c e l l s n o t a d j a c e n t t o t h e b o u n d a r i e s
for ( int i = 1 ; i < ROWS −1; i++)
{
for ( int j = 1 ; j < COLS −1; j++)
{
int liveCount = 0 ;
if ( array [ i − 1 ] [ j −1]) liveCount++;
if ( array [ i − 1 ] [ j ] ) liveCount++;
if ( array [ i − 1 ] [ j +1]) liveCount++;
if ( array [ i ] [ j −1]) liveCount++;
if ( array [ i ] [ j +1]) liveCount++;
if ( array [ i + 1 ] [ j −1]) liveCount++;
if ( array [ i + 1 ] [ j ] ) liveCount++;
if ( array [ i + 1 ] [ j +1]) liveCount++;
...
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
14 / 18
...
if ( array [ i ] [ j ] )
{
if ( liveCount <= 1 | | liveCount >= 4 )
// E x i s t i n g c e l l : d i e s u n l e s s i t i s
// s u r r o u n d e d by e x a c t l y 2 o r 3 l i v i n g
// n e i g h b o u r s .
tempArray [ i ] [ j ] = false ;
else
tempArray [ i ] [ j ] = true ;
}
else
{
if ( liveCount == 3 )
// Dead c e l l : r e b o r n i f s u r r o u n d e d by
// e x a c t l y 3 l i v i n g n e i g h b o u r s .
tempArray [ i ] [ j ] = true ;
else
tempArray [ i ] [ j ] = false ;
}
}
}
. . . big loop . . .
// C ou l d now copy t e m p A r r a y i n t o a r r a y . But t h i s i s
// e x p e n s i v e . We swap t h e i r r e f e r e n c e s i n s t e a d .
boolean [ ] [ ] tempRef = array ;
array = tempArray ;
tempArray = tempRef ;
}
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
16 / 18
/∗ ∗
Draw t h e game b o a r d a s a g r i d .
@param g2 t h e g r a p h i c s c o n t e x t
@param w i d t h t h e w i d t h o f t h e d r a w i n g component
@param h e i g h t t h e h e i g h t o f t h e d r a w i n g component
∗/
public void draw ( Graphics2D g2 , int width , int height )
{
int cellWidth = width / COLS ;
int cellHeight = height / ROWS ;
for ( int i = 0 ; i < ROWS ; i++)
{
for ( int j = 0 ; j < COLS ; j++)
{
if ( array [ i ] [ j ] )
g2 . setColor ( Color . BLACK ) ;
else
g2 . setColor ( Color . WHITE ) ;
Rectangle rect = new Rectangle ( j ∗ cellWidth , i ∗ cellHeight ,
cellWidth , cellHeight ) ;
g2 . fill ( rect ) ;
}
}
}
Interesting facts:
The Game of Life was invented by mathematician John Conway in
1970
Initially, the game was run on paper
The game is the most well-known instance of a cellular automaton
— a grid of cells whose states evolve through time using simple rules
The game can be set up as general-purpose computer that is able to
compute anything that a regular computer can
COMP 1710 (MUN)
Conway’s Game of Life
July 30, 2007
18 / 18