Conway’s Game of Life in C# WPF From http://en.wikipedia.org/wiki/Conway%27s_Gam
ID: 3844861 • Letter: C
Question
Conway’s Game of Life in C# WPF
From http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life:
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead. Every cell interacts with its eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
Any live cell with more than three live neighbours dies, as if by overcrowding.
Any live cell with two or three live neighbours lives, unchanged, to the next generation.
Any dead cell with exactly three live neighbours becomes a live cell.
The initial pattern constitutes the 'seed' of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed — births and deaths happen simultaneously, and the discrete moment at which this happens is sometimes called a tick. (In other words, each generation is a pure function of the one before.) The rules continue to be applied repeatedly to create further generations.
Requirements:
For this assignment, you will provide a way for a user to define the size of a grid on your window. Each dimension has a minimum of 10 and a maximum of 100. Each grid cell will have some representation (a Button, Label, Canvas or something else) of a single Cell. Initially all the Cells will be dead, but you will provide a way for the user to populate the grid with a random distribution of live Cells. Additionally, if the user clicks the Cell in the UI, it will change the state of the cell from live to dead or vice versa.
You will provide a way for the user to step ahead a single generation, and a way to press “play” that will progress through the generations at a maximum speed of 15 generations per second.
You must create a Cell class that holds at least the information of whether the cell is alive or dead. The Cell’s representation must be data-bound to instances of the Cell class. The representations’ change in appearance must be done by some kind of ValueConverter. Any logic done for the game in your application must be done on the Cell objects, not the on-screen representation.
Rubric:
(30 pt.) Game of Life is implemented with the correct rules, and it functions correctly.
If you want to test your implementation, draw a straight line (horizontal or vertical, not diagonal) with 10 live Cells in a row, then run it. You should see it morph into a repeating pattern. Alternately, you can try any of the patterns documented in the Wikipedia article.
(10 pt.) User can create a grid of any size between 10x10 to 100x100. The grid can be rectangular instead of only square. You may allow the user to create smaller or larger grids, but the program must allow at least these sizes.
(5 pt.) Button to step ahead one generation is correctly implemented.
(15 pt.) Button to “play” is correctly implemented, including speed throttle.
Some people like to let their simulations run at maximum speed. If you’re one of those people, be sure to include some sort of throttle option that the grader can enable so that they can test if you’ve implemented the 15 generations / second requirement.
(10 pt.) Provide a way to populate the grid randomly with living cells.
(10 pt.) Cell representations on the grid use data binding to update themselves.
(10 pt.) Cell representations can be clicked to toggle a cell’s state between alive and dead.
(10 pt.) User interface is easy to use, code is clean and well-documented.
Explanation / Answer
// filename should be GameOfLife.java
// import Random class to generate numbers
import java.util.Random;
// import Scanner class to read input from user
import java.util.Scanner;
// Grid class which defines a grid of cells
class Grid
{
// variable to keep track of size of grid
private int SIZE = 30;
// two dimensional array to store grid contents
private int grid[][] = new int[SIZE][SIZE];
// Constructor which initializes the grid to random 0's and 1's
Grid()
{
Random random = new Random();
for(int i=0; i<SIZE; i++)
for(int j=0; j<SIZE; j++)
grid[i][j] = random.nextInt(2);
}
// generationChange() method updates the levliness of all chells for one cycle
public void generationChange()
{
for(int i=0; i<SIZE; i++)
{
for(int j=0; j<SIZE; j++)
{
// initialize neighbour_live_count of current cell to 0
int neighbour_live_count = 0;
// try to add the value of all the eight neighbours to neighbour_live_count
// if the cell is in the corner or edge of the grid, ArrayIndexOutOfBoundsException is raised
// Catch the exception and don't bother about it
try{
neighbour_live_count += grid[i-1][j-1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i-1][j];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i-1][j+1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i][j-1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i][j+1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i+1][j-1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i+1][j];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i+1][j+1];
}
catch(ArrayIndexOutOfBoundsException ref){}
// if the current cell is alive
if(grid[i][j] == 1)
// if the less than 2 or more than 3 neighbours are alive then this cell dies
if(neighbour_live_count < 2 || neighbour_live_count >3)
grid[i][j] = 0;
//else it remains same
else // if cell is dead
// if 3 neighbours are alive then current cell come to lifee
if(neighbour_live_count ==3)
grid[i][j] = 1;
// else remains dead
}
}
}
// displayGrid() method to print the status of all cells in grid form
public void displayGrid()
{
for(int i=0; i<SIZE; i++)
{
for(int j=0; j<SIZE; j++)
System.out.print(grid[i][j] + " ");
System.out.println();
}
System.out.println();
}
}
// GameOfLife class which creates an object of Grid and call methods of Grid
class GameOfLife
{
public static void main(String[] args)
{
// create an objecg of Scanner
Scanner scanner = new Scanner(System.in);
// create an object of Grid
// Grid's constructor will initialize the cell values to 0 or 1
Grid g = new Grid();
int input = 1;
// display initial grid
g.displayGrid();
// loop as long as input is 1
while(input == 1)
{
// change one generation
g.generationChange();
// display grid after changing generation
g.displayGrid();
// Ask user if another generational change is to be carried out
System.out.println("Do you want go for another Generatinal change??");
// Ask user to enter 1 to continue and 0 to exit
System.out.print("Enter Yes or No (1|0) : ");
// read user input
input = scanner.nextInt();
}
}
}