import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.lang.*; /** Conway's Game of Life * GameofLife.java * * @author J Scott Cameron */ public class GameofLife extends Applet implements ActionListener, MouseListener, Runnable{ /* awt components */ Button randomInit = new Button("Random"); Button clear = new Button("Clear"); Button pause = new Button("Pause"); Button step = new Button("Step"); Checkbox cursor = new Checkbox("Cursor draws",true); /* lattice that holds state of the CA*/ boolean board[][]; Dimension size; Image buffer; /** buffer image for animated graphics **/ Graphics bufferGraphics; /** Graphics object for animated graphics **/ Thread animator; /** Thread that controls animation of applet **/ Random random; boolean restart, paused, oneStep; /** booleans that hold information on *animation control **/ int cellSize; /** size, in pixels, of cells on the board **/ int boardWidth,boardHeight; /** dimensions of the board **/ /** * init() sets up the applet, sets all the bounds for companents * and default values for those components that require values **/ public void init() { random = new Random(); setLayout(null); restart = false; paused = false; oneStep = false; randomInit.setBounds(5,405,60,20); clear.setBounds(70,405,60,20); pause.setBounds(135,405,60,20); step.setBounds(200,405,60,20); cursor.setBounds(265,405,100,20); cellSize = 4; randomInit.addActionListener(this); pause.addActionListener(this); step.addActionListener(this); clear.addActionListener(this); add(randomInit); add(pause); add(step); add(cursor); add(clear); size = new Dimension(600,400); boardWidth=size.width/cellSize; boardHeight=size.height/cellSize; addMouseListener(this); buffer = createImage(size.width, size.height); bufferGraphics = buffer.getGraphics(); board= new boolean[boardWidth][boardHeight]; animator = new Thread(this); for(int i=0;i<(boardWidth);i++) { for(int j=0;j<(boardHeight);j++) { if(Math.random() < .5) { board[i][j] = false; } else { board[i][j] = true; } } } animator.start(); } /** *returns number of neighbors which are alive for latice *point (i,j) **/ int neighborsAlive(int i, int j) { int count=0; count += isAlive(i-1,j-1); count += isAlive(i-1,j); count += isAlive(i-1,j+1); count += isAlive(i+1,j-1); count += isAlive(i+1,j); count += isAlive(i+1,j+1); count += isAlive(i,j-1); count += isAlive(i,j+1); return count; } /** *returns whether or not lattice point (i,j) is alive **/ int isAlive(int i, int j) { if(board[(i+boardWidth)%boardWidth][(j+boardHeight)%boardHeight]) return 1; return 0; } /** * updates the applet's board * using the rules of Conway's Game of Life **/ synchronized void updateBoard() { boolean temp[][] = new boolean[(boardWidth)][(boardHeight)]; int j,k,i; for( i=0; i<(boardWidth); i++) { for( j=0; j<(boardHeight); j++) { k = neighborsAlive(i,j); if(board[i][j]) { if(k < 2 || k > 3)temp[i][j]=false; else temp[i][j]=true; } else { if(k==3)temp[i][j]=true; else temp[i][j]=false; } } } for(i=0;i<(boardWidth);i++) { for(j=0;j<(boardHeight);j++) { board[i][j]=temp[i][j]; } } } /** ActionPerformed() catches the actionEvents (pressed buttons) * and sends it on to whatever function needs to be called * @param evt description of event */ public void actionPerformed(ActionEvent evt) { Button theItem = (Button) evt.getSource(); if (theItem == randomInit) randomPressed(); else if (theItem == pause) pausePressed(); else if (theItem == step) stepPressed(); else if (theItem == clear) clearPressed(); } /** * Generates a board with a random configuration **/ public void randomPressed() { paused = true; for(int i=0;i<(size.width/cellSize);i++) { for(int j=0;j<(size.height/cellSize);j++) { if(Math.random() < .5) { board[i][j] = false; } else { board[i][j] = true; } } } repaint(0,0,size.width,size.height); } /** * toggles paused boolean **/ public void pausePressed() { paused = !paused; } /** * advances board one step and pauses again **/ public void stepPressed() { if(paused) paused = false; oneStep = true; } /** * clears board **/ public void clearPressed() { paused = true; for(int i=0 ; i