"Game of Life" (Method help)
917245Feb 13 2012 — edited Feb 14 2012Ok, I am creating the "Game of Life" for those who have heard of it and if you haven't it's no big deal. I have the working program, but my teacher gave us all a separate class in the same project and told us to change OUR code to be used with the code she gave us. Here is the code I have.
import java.util.Random;
import java.util.Scanner;
//Nick Weidinger
//Life Matrix Lab
//1/24/12
//Elzer, 162
/**This program creates a board of -'s with random #'s on it
**/
public class Life {
/** these instance varibales are initialized to 0
**/
private int birthHigh = 0;
private int birthLow = 0;
private int liveHigh = 0;
private int liveLow = 0;
private int rows = 0;
private int columns = 0;
private boolean[][]Matrix;
/** constructor calls the initializing method and sets each instance variable to the users input
* @param seed is the user given seed number
* @param rows is the user given number of rows
* @param columns is the user given number of columns
* @param birthLow2 is the user given birth minimum
* @param birthHigh2 is the user given birth maximum
* @param liveLow 2 is the user given death minimum
* @param liveHigh2 is the user given death maximum
* **/
public Life(long seed, int rows, int columns, int birthLow2, int birthHigh2,
int liveLow2, int liveHigh2) {
boolean initMatrix[][] = new boolean[rows][columns]; // creates new
// boolean array sized according to user input
array(initMatrix, rows, columns, seed); // calls the initializing method
birthHigh = birthHigh2;
birthLow = birthLow2;
liveHigh = liveHigh2;
liveLow = liveLow2;
Matrix = initMatrix;
}
/** returns a copy of the boolean matrix at its current state**/
public boolean[][] world() {
boolean matrixClone[][] = Matrix.clone();
for (int row = 0; row < Matrix.length; row++) {
matrixClone[row] = Matrix[row].clone();
}
return matrixClone;
}
/** updates the world by one generation**/
public void update() {
Matrix = alterMatrix(
Matrix, rows, columns, birthLow,
birthHigh, liveLow, liveHigh);
}
/**This method asks for user input and calls the initializing and printing
methods
* @param args unused
**/
public static void main(String[] args) {
Scanner console = new Scanner(System.in); // Creates new scanner
System.out.println("Number of rows?");
int rows = console.nextInt(); // Stores user input as number of rows
System.out.println("Number of columns?");
int columns = console.nextInt(); // Stores user input as number of
// columns
System.out.println("Seed number?");
long seed = console.nextLong(); // Stores user input as number of seed
System.out.println("Birth minimum?");
int birthLow = console.nextInt(); // Store user input as the minimum
// number for birth
System.out.println("Birth maximum?");
int birthHigh = console.nextInt(); // Stores user input as maximum
// number for birth
System.out.println("Live minimum?");
int liveLow = console.nextInt(); // Stores user input as minimum for
// death
System.out.println("Live maximum?");
int liveHigh = console.nextInt(); // stores user input as maximum for
// death
boolean initMatrix[][] = new boolean[rows][columns]; // creates new
// boolean array
// sized
// according to
// user input
array(initMatrix, rows, columns, seed); // calls the initializing method
printArray(initMatrix, rows, columns); // calls the printing method
System.out.println();
for (int i = 0; i < 5; i++) {
initMatrix = alterMatrix(initMatrix, rows, columns, birthLow,
birthHigh, liveLow, liveHigh);
printArray(initMatrix, rows, columns);
System.out.println();
}
}
/**This method initializes the array with trues and falses
* @param Matrix is the matrix declared in main
* @param rows is the user given number of rows
* @param columns is the user given number of columns
* @param seed is the user given seed number for random gen
* **/
public static void array(boolean[][] Matrix, int rows, int columns,
long seed) {
Random generator = new Random(seed); // creates random number according
// to user seed
// loop goes through every row starting at 2nd and ending at 2nd to last
for (int i = 1; i < rows - 1; i++) {
// loop goes through every column starting at 2nd and ending at 2nd
// to last
for (int j = 1; j < columns - 1; j++) {
// generates random value
boolean x = generator.nextBoolean();
Matrix[i][j] = x;
}
}
}
/**This method prints the array
* @param Matrix is the initMatrix from main
* @param rows is the number of rows
* @param columns is the number of columns
* **/
public static void printArray(boolean[][] Matrix, int rows, int columns) {
// these loops go through every value in the array
for (int k = 0; k < rows; k++) {
for (int m = 0; m < columns; m++) {
// if the array is false, print a -
if (!Matrix[k][m]) {
System.out.print("- ");
}
// if the array is true, print a #
else {
System.out.print("# ");
}
}
System.out.println(); // starts a new row
}
}
/** alterMatrix updates the matrix by one generation
*
* @param initialMatrix is the initMatrix from main
* @param rows is the number of rows
* @param columns is the number of columns
* @param birthLow is the birth min
* @param birthHigh is the birth max
* @param liveLow is the death min
* @param liveHigh is the death max
* @return returns the updated matrix
*/
public static boolean[][] alterMatrix(boolean[][] initialMatrix, int rows,
int columns, int birthLow, int birthHigh, int liveLow, int liveHigh) {
boolean matrixUpdate[][] = initialMatrix.clone();
for (int row = 0; row < initialMatrix.length; row++) {
matrixUpdate[row] = initialMatrix[row].clone();
}
// loop goes through every row starting at 2nd and ending at 2nd to
// last
for (int i = 1; i < rows - 1; i++) {
// loop goes through every column starting at 2nd and ending at
// 2nd
// to last
for (int j = 1; j < columns - 1; j++) {
// if initMatrix was false, look to see if life can be born
if (!initialMatrix[i][j]) {
int counter = 0;
// These if statements test each neighboring spot for
// life
// if life is there, counter will increase by 1
if (initialMatrix[i - 1][j - 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i - 1][j] == true) {
counter = counter + 1;
}
if (initialMatrix[i - 1][j + 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i][j - 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i][j + 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i + 1][j + 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i + 1][j - 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i + 1][j] == true) {
counter = counter + 1;
} else {
}
// if the counter is in the birth range, set that spot
// as true
if (counter >= birthLow && counter <= birthHigh) {
matrixUpdate[i][j] = true;
}
}
// if initMatrix was true, look to see if life will die
else {
int counter2 = 1;
// these if statements test each spot neighboring spot
// for
// life
// if life is found, counter will increase by 1
if (initialMatrix[i - 1][j - 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i - 1][j] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i - 1][j + 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i][j - 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i][j + 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i + 1][j + 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i + 1][j - 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i + 1][j] == true) {
counter2 = counter2 + 1;
}
// if counter is outside of the death range, life is
// eliminated
if (counter2 > liveHigh || counter2 < liveLow) {
matrixUpdate[i][j] = false;
} else {
}
}
}
}
return matrixUpdate;
}
If you run the program you can see that it works. It creates a board of -'s with random #'s on it and then deletes and adds new #'s according to the initial board. My teacher gave us this code and told us to create a constructor and methods to have the same output on her class that I have on mine. Her code is:
/**
* The Console class is a console interface to the Life game.
* @author David Hutchens
* @date Sept. 2008
*/
import java.util.Scanner;
public class Console {
/**
* @param args unused
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the size of the matrix(rows, columns) :");
int rows = in.nextInt();
int columns = in.nextInt();
System.out.println("Please enter random seed: ");
long seed = in.nextLong();
System.out.println("Please enter birth range (low, high) :");
int birthLow = in.nextInt();
int birthHigh = in.nextInt();
System.out.println("Please enter live range (low, high): ");
int liveLow = in.nextInt();
int liveHigh = in.nextInt();
try {
Life game = new Life(seed, rows, columns, birthLow, birthHigh, liveLow, liveHigh);
playLife(game);
} catch (IllegalArgumentException e) {
System.out.println("Inappropriate values: " + e.getMessage());
}
}
/**
* Print a boolean matrix
* @param world is a boolean matrix to be printed with # for true and - for false.
*/
public static void printWorld(boolean[][] matrix) {
for (int r=0; r<matrix.length; r++) {
for (int c=0; c<matrix[0].length; c++) {
System.out.print(matrix[r][c] ? " # " : " - ");
}
System.out.println();
}
System.out.println();
}
/**
* Play the game of Life starting with a given state
* @param game is the Life object that provides the current state of Life
*/
public static void playLife(Life game) {
printWorld(game.world());
for (int i=0; i<10; i++) {
game.update();
printWorld(game.world());
}
}
}
Now, when I run hers, it prints out the initial board 11 times instead of updating it 10 times and printing out the updated states. I must have a problem in my "update" method in the first code. I am not sure what is wrong with it. I take Matrix and set it equal to what the array would be if I passed Matrix into the alterMatrix method which should update the state by 1 increment. Does anyone know why it's not updating? Thanks