Game of Life
Back to the Program-A-Day homepage
ProblemFor this problem we will code a simple version of Conway's Game of Life. You will want to have a method that prints 2D 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.
|
By Students...It always amazes me what can be represented by computer data. When I was first learning about programming it seemed like you could do anything with it, especially using arrays. The concept that a 2D array is just an array of arrays really opened my mind to new ways of thinking about things. I couldn't help but try to think of situations where you would use an array of more than two dimensions, especially since each dimension doesn't even have to relate to a physical dimension in the real world. I thought of it more like stuffing small boxes into larger boxes. I started thinking about how I would store a Sudoku puzzle in 4-dimensions, a 3x3 grid of 3x3 grids, and solve it in that manner. Of course, at that point I had another problem. | |
---|---|---|
SolutionFirst we need to set up a grid of cells. I have a sample here, but you can change it however you want to experiment with it later. We will also need another grid to be our solution and an integer to count neighbors. public class Life { public static void main(String[] args) { //The arrays String[][] cells = {{" ","*"," ","*","*"," ","*","*","*","*"},//feel free to {" "," "," "," "," "," ","*"," ","*"," "},//put in whatever {" ","*","*"," "," "," "," "," ","*"," "},//values you feel like. {" ","*","*"," "," "," ","*"," "," "," "}, {" "," "," "," "," ","*","*","*"," "," "}, {" "," "," "," "," "," ","*"," "," "," "}, {" "," ","*","*","*"," "," "," "," "," "}, {" "," ","*"," ","*"," "," "," "," "," "}, {" "," ","*","*","*"," "," "," "," "," "}, {" "," "," "," "," "," "," "," "," "," "}}; String[][] newCells = new String[10][10]; //counts how many living neighbors a cell has. int neighbors; } }
for(int y = 0;y < cells.length;y++) { for(int x = 0;x < cells.length;x++) { neighbors = 0; //Find out how many living neighbors. //Use rules to see if current cell is alive. } }
Since we are using Strings, remember to do comparisons with the .equals() method. for(int y = 0;y < cells.length;y++) { for(int x = 0;x < cells.length;x++) { neighbors = 0; //Find out how many living neighbors. if(x > 0) {//check left of cell if( cells[y][x-1].equals("*") ) neighbors++; if( y > 0 && cells[y-1][x-1].equals("*") )//check top-left neighbors++; if( y < cells.length && cells[y+1][x-1].equals("*") )//check bottom-left neighbors++; } if(x < cells.length) {//check right of cell if( cells[y][x+1].equals("*") ) neighbors++; if( y > 0 && cells[y-1][x+1].equals("*") )//check top-right neighbors++; if( y < cells.length && cells[y+1][x+1].equals("*") )//check bottom-right neighbors++; } //check top and bottom if(y > 0) { if( cells[y-1][x].equals("*") ) neighbors++; } if(y < cells.length) { if( cells[y+1][x].equals("*") ) neighbors++; } //Use rules to see if current cell is alive. } }
for(int y = 0;y < cells.length;y++) { for(int x = 0;x < cells.length;x++) { neighbors = 0; //Find out how many living neighbors. if(x > 0) {//check left of cell if( cells[y][x-1].equals("*") ) neighbors++; if( y > 0 && cells[y-1][x-1].equals("*") )//check top-left neighbors++; if( y < cells.length && cells[y+1][x-1].equals("*") )//check bottom-left neighbors++; } if(x < cells.length) {//check right of cell if( cells[y][x+1].equals("*") ) neighbors++; if( y > 0 && cells[y-1][x+1].equals("*") )//check top-right neighbors++; if( y < cells.length && cells[y+1][x+1].equals("*") )//check bottom-right neighbors++; } //check top and bottom if(y > 0) { if( cells[y-1][x].equals("*") ) neighbors++; } if(y < cells.length) { if( cells[y+1][x].equals("*") ) neighbors++; } //Use rules to see if current cell is alive. if( cells[y][x].equals("*") ) { if(neighbors < 2) newCells[y][x] = " "; else if(neighbors > 3) newCells[y][x] = " "; else newCells[y][x] = "*"; } else { if(neighbors == 3) newCells[y][x] = "*"; else newCells[y][x] = " "; } } }
| ||
Code |