Results 1 to 3 of 3
Thread: 2d array help- Game of Life
- 07-22-2009, 08:53 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
2d array help- Game of Life
so I'm having problems, the boards completely erases on generation 2 and I have no clue as to why.
Java Code:import java.util.Scanner; import java.util.Random; public class Life { // the size of the grid (GRIDSIZE x GRIDSIZE) final private static int GRIDSIZE = 18; /********************************************************************************/ public static void main ( String args[] ) { boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE]; char choice; int x = 1; Scanner sc = new Scanner ( System.in ); do { System.out.print ( "Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? "); choice = sc.next().charAt(0); } while ( choice != 'r' && choice != 'q' && choice != 'g' ); clearGrid (board); setup(board,choice); do { System.out.printf ("Viewing generation #%d:\n\n", x++); displayGrid(board); genNextGrid(board); System.out.print ("\n(q)uit or any other key + ENTER to continue: "); choice = sc.next().charAt(0); } while ( choice != 'q' ); } /********************************************************************************/ public static void setup (boolean[][] board, char which ) { Random randomNumbers = new Random(); clearGrid(board); if ( which == 'q' ) { // Set up the Queen Bee Shuttle pattern board[5][1] = true;board[5][2] = true;board[6][3] = true;board[7][4] = true; board[8][4] = true;board[9][4] = true;board[10][3] = true;board[11][2] = true; board[11][1] = true; } else if ( which == 'g' ) { // Set up a Glider board [17][0] = true; board[16][1] = true; board[15][1] = true; board[16][2] = true; board [17][2] = true; } else { // set up random for (int row = 0; row < board.length; row++ ) { for (int col = 0; col < board[row].length; col++ ) { if ( randomNumbers.nextInt() % 2 == 0 ) board[row][col] = true; } } } } /********************************************************************************/ public static void displayGrid (boolean[][] grid) { // Start printing the top row of numbers System.out.print (" "); for (int x = 1; x <= grid.length; x++) { if ((x / 10) != 0) System.out.printf ( "%d", x / 10 ); else System.out.print ( " " ); } System.out.println(); System.out.print( " " ); for (int x = 1; x <= grid.length; x++) { System.out.printf ( "%d", x % 10 ); } System.out.println(); for (int r = 0; r < grid.length; r++) { System.out.printf ( "%d", r+1 ); if (r + 1 < 10) System.out.print ( " " ); else System.out.print ( " " ); for (int c = 0; c < grid.length; c++) { if (grid[r][c] == true) System.out.print ( "*" ); else System.out.print ( " " ); } System.out.println(); } } /*******************************************************************************/ // Method Name : clearGrid // Parameters : board(boolean) // Return value(s) : none // Partners : // Description : resets the grid to false public static void clearGrid ( boolean[][] board ) { int col=0; for (int row = 0; row <=18; row++) { while(col<18) { board [row][col] = false ; col++; } } } // Method Name : genNextGrid // Parameters : board(boolean) // Return value(s) : none // Partners : // Description : applies rules to temp array and save it under old array /*******************************************************************************/ public static void genNextGrid ( boolean[][] board ) { boolean[][] temp = new boolean[GRIDSIZE][GRIDSIZE]; for (int row=0; row<GRIDSIZE; row++)//start of row { for (int col=0; col<GRIDSIZE; col++) { temp[row][col]=board[row][col]; int count = countNeighbors ( temp , row, col ); switch (count) {//start of count switch case 0: case 1: temp[row][col]=false; break; case 2: if(temp[row][col]) temp[row][col]=true; if(!temp[row][col]) temp[row][col]=false; break; case 3: temp[row][col]=true; break; case 4: case 5: case 6: case 7: case 8: temp[row][col]=false; break; }//end of switch statement board [row][col]=temp[row][col]; }//end of for row }//end of for col } /*******************************************************************************/ // Method Name : conutNieghborrs // Parameters : board(boolean), int row, int col // Return value(s) : ncount // Partners : // Description : get data fro array and counts number of neighbors around array public static int countNeighbors ( final boolean[][] board, final int row, final int col ) { int ncount = 0; for(int r = row-1; r <= row+1; r++) { if(row < 0 || row >= GRIDSIZE ) { continue; } for(int c = col - 1; c <= col + 1; c++) { if(col < 0 || col >= GRIDSIZE || (r == row && c == col)) { continue; } if(board[row][col]) { ncount++; } } } return ncount; } }
- 07-22-2009, 09:52 PM #2
The argument board is a local variable, it has no relationship to the member variable in class scope of the same name.
You could return an array from this method, or eliminate the argument and refer to the member variable in the method body or you could reassign the member variable to temp, like this:Java Code:public static void genNextGrid ( boolean[][] board )
Java Code:}//end of switch statement // board [row][col]=temp[row][col]; }//end of for row }//end of for col board = temp;
- 07-23-2009, 12:01 AM #3
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
thanks but if i put this in
I kind of get what I want, but the only problem is that its the only way i can get it to run , but it does it all automatically, then at end it as user to go to next generation.Java Code:}//end of switch statement board[row][col] = temp[row][col]; displayGrid(board); }//end of for row }/Last edited by Fubarable; 07-23-2009 at 06:27 PM. Reason: code tag repaired
Similar Threads
-
Coded Life Lesson :)
By MuslimCoder in forum Forum LobbyReplies: 7Last Post: 07-29-2009, 08:16 PM -
A more efficient Game of Life
By unreal4evr in forum New To JavaReplies: 3Last Post: 03-27-2009, 03:08 AM -
Game of Life assignment
By javan00b in forum New To JavaReplies: 4Last Post: 04-28-2008, 05:49 AM -
Servlet Life Cycle
By Java Tip in forum Java TipReplies: 0Last Post: 11-28-2007, 09:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks