Difference between revisions of "Game of Life"

From CompSciWiki
Jump to: navigation, search
m (Removed <pre> tags from SolutionCode. Note: problem is incomplete)
m (Fixed photo)
Line 22: Line 22:
 
|SideSectionTitle=More with Arrays
 
|SideSectionTitle=More with Arrays
 
|SideSection=
 
|SideSection=
[[Image:Wiki_method01.jpg|center]]
+
[[Image:Wiki_method01.jpg|center]]<BR>
<BR>
+
  
 
|Solution=
 
|Solution=

Revision as of 14:29, 9 April 2010

Back to the Program-A-Day homepage

Problem

For this problem we will code a simple version of Conway's Game of Life. You will want to have a method that prints arrays before tackling this challenge.

This is a zero-player game developed by John Horton Conway as a cellular automaton. More information can easily be found around the internet if you want to look into it in more depth.

To start off with, make a 2-dimensional grid of Strings. Each String will represent a cell. A cell is alive if it contains a * and dead if it contains a space. This is normally done with booleans but Strings are easier to use with our printArray method.

Make your array size 10x10. We will hard code the array with starting values.

Your program will calculate which cells are dead or alive in the next "time unit" by looking at each cell's neighbors. Neighbors are cells adjacent vertically, horizontally, or diagonally. Use the following rules:

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

Watch out for edges and corners. You don't want to check out of bounds.

 

More with Arrays

Wiki method01.jpg

Solution

My solution

Code

Solution Code

Back to the Program-A-Day homepage