To create a program that runs Conway\'s Game of Life. * It consists of a two dim
ID: 3543435 • Letter: T
Question
To create a program that runs Conway's Game of Life.
* It consists of a two dimensional grid of cells that evolve over generations.
* In a given generation, each cell may be "alive" or "dead", and whether the cell
* is alive or dead in the next generation is completely determined by both its
* current state and the current states of cells adjacent to it. If x is alive
* and has exactly 2 or 3 live neighbors, then x survives to the next generation.
* Otherwise, it dies. If x is dead and has exactly 3 live neighbors, then x
* is alive in the next generation. Otherwise, it remains dead.
Code I have so far:
public class Life {
import java.util.Scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
int gridSize = 200;
int cellSize = 3;
Grid grid = new Grid(gridSize, cellSize, "The Game of Life");
grid.setDelay(10);
int aliveColor = 1;
int deadColor = 0;
for (int row = 0; row < 3; row++)
for(int col = 0; col < 200; col++)
grid[row][col] = setPos(row, col, int v);
}
}
Please help with code and give very discriptive answers of the steps and why they are needed.
I'm not sure what question to ask.
How do I color a particular cell of the grid using the grid object's setPos method?
Then I have to invoke grid.update().
Use the grid method clearGrid() to erae the grid.
Before the game can begin, must prepopulate the grid with random values. That is, each cell should be randomly assigned either a 1 or 0.
A random integer can be obtained using the Random class of the java.util package (not sure how to do this).
In order to populate the gride with random values, must use a nested loop--an outer plop to iterate over the rows of the grid, and an inner loop to iterate over the columns (or vice versa). Both while loops or for loops could be used, but it's more natural in this case to use for loops.
Since the grid in this case is a square, the lower bounds for both loops should be 0, and the upper bounds should gridSize-1. That is, you should color all cells from (0, 0) to (gridSize -1, gridSize -1).
Next, must invoke the method intitialize();
grid.initialize();
This will cause the pseudorandom values you have set to be recorded and displayed.
First not the game of life runs in cycles (generations), and the game never terminates (unless the user ends the program's process). Means should use an infinite loop.
In order to update a cell, will need the following methods in addition to setPos:
int getPos(int row, int col) //Retrieve the value of cell
//Return the # of neighbors of (row, col) having value val
int matchingNeighbors(int row, int col, int val)
val should be aliveColor
In each generation, every cell of the grid must be updated. A nested loop is again needed to do this. Must write code to do this. When updating a cell value, you must use decision statements to determine what value it should be assigned in the next generation (based upon its current value and the current values of its neighbors)
The Grid class has been defined so that it is possible to set a given cell to a new value without affecting the calculations determining whether other cells live or die. Changing the value of a cell does not take effect until the update() method is called and so it is possible to set a cell's value but still use its previous value in calculations about other cells.
You should invoke the grid.update() method immediately after the loop statement assigning new values to the grid has completed.
Explanation / Answer
function GameOfLife () {
this.init = function (turns,width,height) {
this.board = new Array(height);
for (var x = 0; x < height; x++) {
this.board[x] = new Array(width);
for (var y = 0; y < width; y++) {
this.board[x][y] = Math.round(Math.random());
}
}
this.turns = turns;
}
this.nextGen = function() {
this.boardNext = new Array(this.board.length);
for (var i = 0; i < this.board.length; i++) {
this.boardNext[i] = new Array(this.board[i].length);
}
for (var x = 0; x < this.board.length; x++) {
for (var y = 0; y < this.board[x].length; y++) {
var n = 0;
for (var dx = -1; dx <= 1; dx++) {
for (var dy = -1; dy <= 1; dy++) {
if ( dx == 0 && dy == 0){}
else if (typeof this.board[x+dx] !== 'undefined'
&& typeof this.board[x+dx][y+dy] !== 'undefined'
&& this.board[x+dx][y+dy]) {
n++;
}
}
}
var c = this.board[x][y];
switch (n) {
case 0:
case 1:
c = 0;
break;
case 2:
break;
case 3:
c = 1;
break;
default:
c = 0;
}
this.boardNext[x][y] = c;
}
}
this.board = this.boardNext.slice();
}
this.print = function() {
for (var x = 0; x < this.board.length; x++) {
var l = "";
for (var y = 0; y < this.board[x].length; y++) {
if (this.board[x][y])
l += "X";
else
l += " ";
}
print(l);
}
}
this.start = function() {
for (var t = 0; t < this.turns; t++) {
print("--- Turn "+(t+1));
this.print();
this.nextGen()
}
}
}
var game = new GameOfLife();
print("--- 3x3 Blinker over three turns.");
game.init(3);
game.board = [
[0,0,0],
[1,1,1],
[0,0,0]];
game.start();
print("--- 10x6 Glider over five turns.");
game.init(5);
game.board = [
[0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0],
[0,1,1,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]];
game.start();
print("--- Random 5x10");
game.init(5,5,10);
game.start();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.