COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Assignment 3 – The Game of Life

Posted by on October 6th, 2014 · Assignments

DUE DATE: 11:59pm Sunday 26 October.

WARNING: This is a draft only. These specifications will be updated.

For this assignment you will write a program that implements Conway’s Game of Life. The board is a rectangular array in which a cell is designated as either live or dead. A few simple rules tell you how to create a new generation (i.e. a new board) based on the old one.

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

You can see an animation of the game at this web site. Two supporting classes are provided for you to display the game. You don’t have to write any graphics code. Your task will only be to implement the rules above.

You will find a project file for the assignment here and an executable sample here.

The project has three classes: Canvas, which handles the primitive drawing on the screen; Grid, is a subclass of canvas and handles drawing the board; and Life, which is the file that you must modify to play the Game of Life. It also has the constructor method that plays the game. Do not, under any circumstances, change the other two classes and do not change the Life constructor method!

A board is represented as a 2-dimensional boolean array:

boolean[][] board = new boolean[numberOfRows][numberOfColumns];

You are required to three methods.

Task 1

The first method you must write is to create a new board with a random number of cells set to true. The chances of a cell being alive (i.e. true) should be 10%.

private boolean[][] newBoard(int height, int width)

The method has two parameters: the width and height of the board. It must return a 2-dimensional boolean array in which approximately 10% of the cells are set to true (i.e. alive).

Task 2

The second method you must write is to count the live neighbours of a cell.

private int countNeighbours(boolean[][] board, int row, int col)

Given a board (i.e. a 2D boolean array) and a row and column position, count the number of immediately surrounding cells that contain the value true. The table below shows the index values of the neighbours of the cell where i is the row number and j is the column number (starting from zero).

i-1, j-1 i-1, j i-1, j+1
i, j-1 i, j i, j+1
i+1, j-1 i+1, j i+1, j+1

Your method should visit each of the surrounding cells and accumulate a count of the cells that contain true, i.e, the cell is live. Note that you do not count the cell itself. If a cell is on the edge of the array (i.e. some of its neighbours are out of bounds), only count the cells that are within the array.

Task 3

The third method you must write is:

private boolean[][] nextGeneration(boolean[][] board1)

The only parameter to the method is a board (i.e. a 2D boolean array). For each cell in the array (i.e. for each column position in each row), apply the four reproduction rules, given above, to create a new board representing the next generation. To do this, you must declare a new array that is the same size as the old one. As you apply the rules to a cell in the old array, you update the corresponding cell in the new array. That is, you must not change the original array.

WARNING: The Game of Life is a popular programming exercise and you will find many versions of it on the web. While you may use these to get an idea of how to write your own program, your code must be your own. Plagiarism is not allowed and will be heavily penalised.

Submission instructions

You will submit your assignment to the Give automated marked system.

  1. From within your assignment 3 BlueJ project, select Project -> Create Jar File…
  2. In the Dialog Box that appears:
    1. Set “Main Class” to none
    2. Ensure “Include Source” is checked
    3. Leave “Include Bluej project files” unchecked
  3. Press “Continue”
  4. Save the filename as “Assignment3.jar”
  5. Open a web browser and go to the give web site
  6. Log-in with you Z-Pass
  7. Either enter “COMP1400″ in for the course, or select COMP1400 from the drop down menu and press “Search for Assignments”.
  8. Select “assignment3″ from the next drop down menu and press “Upload my Assignment”
  9. Accept the student declaration
  10. Press “Choose File” and select the “Assignment3.jar” file that you saved in step 4
  11. Press “Submit my Files”
  12. Wait for the page to completely load. Then carefully read the output. If the assignment is successful you should see the following lines:
    ================================================
    Checking your submission...
    
    Checking for Life.java
        Life.java Compiled Successfully
    Compiling against supplied file Canvas.java
        Canvas.java Compiled Successfully
    Compiling against supplied file Grid.java
        Grid.java Compiled Successfully
    
    All files are OK
    ================================================
  13. If the page doesn’t load properly, that is OK just submit the assignment file again.
  14. If you don’t get the above output check the output to see what went wrong and resubmit.

Submission notes:

  1. You can submit as many times as you wish, even after the deadline. Only your last submission will be marked.
  2. Make sure you name everything the as instructed, including the classes and the submission file or the submission will not work.
  3. If you have any problems with submission, email the class account

 

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.