HOME | Overview | Syllabus | Instructor | Lectures | Laboratories | Readings | Assignments | Resources | Other Links
CS 1711 Recitation Laboratory 12: Game of Life
Working with Two-Dimensional Arrays of Objects
[11/29/99; 12/1/99; 12/6/99; 12/8/99]

 

Objectives:

Check-off procedure:

You must attend the recitation and sign in. Because of student evaluations taking place during the week of November 29, the following check-off rules apply. In order to receive 7 points on the lab, you must attend a lab session during the week of November 29. To earn the remaining credit, you can either:

Laboratory details:

The Game of Life is a famous board game invented by Mathematician John Conway. The board consists of n x m cells. Each cell is either alive or dead. The board starts with an initial set of dead and alive cells. At the next time step, a cell determines whether it is alive or dead by the following rules:

In this assignment, the cells are implemented as Cell objects. The Cell class is given. You are to implement the game of life using a two-dimensional array of Cell objects to represent the board.

The game of life has received a great deal of attention as a simple model of how populations of a species behave. Much work has been done to determine which initial patterns on the board produce interesting behavior. In this laboratory, you will determine whether a particular cell is alive or dead at random. Here are some web sites devoted to the game of life, if you would like to observe it in action or learn more about it:

Part I: The Setup

Part II: Initializing the Board

Part III: The Game of Life

Part IV: Reflecting Boundaries


Cell.java

public class Cell {
    private int myRow;
    private int myColumn;
    private Life myWorld;
    private boolean myExistence;
    private boolean myFutureExistence;
   
    // constructor
     public Cell(Life world, int row, int column, boolean alive) {
        myWorld = world;
        myRow = row;
        myColumn = column;
        myExistence = alive;
        myFutureExistence = false;
    }
   
    // accessors
    public int getRow() {
        return myRow;
    }

    public int getColumn() {
        return myColumn;
    }

    public boolean isAlive() {
        return myExistence;
    }


    // Find out whether cell will be alive at the next time step
    public boolean isAliveInFuture() {
        int neighbors = getNumberNeighbors(); 
        if (!myExistence && neighbors == 3) 
           myFutureExistence = true; 
        else if (myExistence && (neighbors == 2 || neighbors == 3)) 
           myFutureExistence = true; 
        else 
           myFutureExistence = false; 
        return myFutureExistence;
    }   
    
    // modifier - moves the cell ahead one time step
    public void age() {
        myExistence = myFutureExistence;
    }   
    
    //  helper - returns number of live neighbors
    public int getNumberNeighbors() {
        int count = 0;
        for (int i = myRow - 1; i <= myRow + 1; i++) {
            for (int j = myColumn - 1; j <= myColumn + 1; j++) {
                if (myWorld.isAlive(i,j))
                    count++;
            }
        }      
        // If I'm currently alive I counted myself in the above loop
        if (isAlive())
           count--;
        return count;
    }
}

Last revision: December 3, 1999 at 5:40 am.