Results 1 to 1 of 1
Thread: "Game of Life" Help
- 02-13-2012, 04:50 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
"Game of Life" Help
For my programming class, I have to recreate the "Game of Life" game. Right now, we are only building the foundation for it so it doesn't have to do as much as the real game quite yet. The program should ask the user for input: number of columns, number of rows, seed number for the random gen, birth high, birth low, deathHigh and deathLow. There should be a grid of -'s with random #'s on it to represent the grid (we are going to use graphics later) and each # represents a living cell. The rules are:
1.Any live cell with fewer than deathLow live neighbours dies, as if caused by underpopulation.
2.Any live cell with more than deathHigh live neighbours dies, as if by overcrowding.
3.Any live cell with a number of live neighbours between deathLow and deathHigh lives on to the next generation.
4.Any dead cell with a number of live neighbours between birthLow and birthHigh becomes a live cell.
The "seed" input is to help test the program. If the seed if always the same, then the random gen will always be the same. We have to print out 5 generations. My program works. No errors and it compiles fine. But, my teacher gave us a sample output and mine does not match hers. There is an error somewhere in my alterMatrix method, but I can't find where. Can you guys find it? Thanks a lot.
Java Code:import java.util.Random; import java.util.Scanner; //This program creates a board of -'s with random #'s on it. public class Life { // This method asks for user input and calls the initializing and printing // methods 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<4; i++){ alterMatrix(initMatrix, rows, columns, birthLow, birthHigh, liveLow, liveHigh); printArray(initMatrix, rows, columns); System.out.println(); } } // This method initializes the array with trues and falses 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(); // if x is false, set that array spot as false if (!x) { Matrix[i][j] = false; } // if x is true, set that array spot as true else { Matrix[i][j] = true; } } } } // This method prints the array 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 } } public static void 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] = matrixUpdate[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 = 0; // 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 { } } } } } } }
Similar Threads
-
loop "play again" in an 8 ball game, loops but ... (by IareSmart)
By DarrylBurke in forum New To JavaReplies: 4Last Post: 02-03-2012, 01:55 PM -
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
Need help with "constructing" Game of Life
By Otakon in forum New To JavaReplies: 21Last Post: 01-20-2010, 05:25 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks