Results 1 to 20 of 27
Thread: Game of life methods help
- 12-09-2009, 09:45 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
Game of life methods help
I have been working on this project for a while now and its due soon. I can't seem to get the genNextgeneration method to work in my program. This is Conway's Game of Life and this method is suppose to generate the next generation. It needs to use the 2 dimensional array board that is passed to it as the "current generation" then it should create a second temporary 2 dimensional array that will hold the next generation. This method also needs to call on the method countNeighbors() to help decide if the cell will be alive or dead in the next generation. Once the next generation of the simulation into your temporary 2 dimensional array it needs to copy that array back over the current generation (called board) so the next generation can leave the method through the parameter....I am so confused I don't even know if I didn't it right. If anyone could help that'd be awesome!
here's my code:
Java Code:public static void clearGrid ( boolean[][] board ) { for( int row = 0; row < board.length; row++ ) { for( int col = 0; col < board[row].length; col++ ) { board[row][col] = false; } } } public static void genNextGrid ( boolean[][] board ) { boolean[][]temp = new boolean[GRIDSIZE][GRIDSIZE]; for( int row = 0; row < 18; row++) { for( int col = 0; col < 18; col++ ) { int count = countNeighbors ( board, row, col); } } for( int row = 0; row < 18; row++ ) { for( int col = 0; col < 18; col++ ) { int count = countNeighbors( temp, row, col ); board[row][col] = temp[row][col]; switch( count) { 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; } board[row][col] = temp[row][col]; } } } 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; }
-
You're doing the same thing as before, only now you're trying to do both set temp's value and board's value in the second set of nested for loops. Again, don't do this. Set your temp array values in the first set and use this info to set your board values in the second set. This way, you won't be changing the board array prematurely which would prevent you from messing up the countNeighbors calculation. e.g.,
A cool way to simplify your code is to use a truth table. Something like so:Java Code:// the first set of nested for loops calculates the values for the temp // array and nothing else. It DOES NOT CHANGE the board array for (int row = 0; row < 18; row++) { for (int col = 0; col < 18; col++) { int count = countNeighbors(board, row, col); // here use count to calc the proper value for the temp // temp[row][col] = .....etc... } } // the second set of nested for loops does nothing but // sets the board with the values held by temp for (int row = 0; row < 18; row++) { for (int col = 0; col < 18; col++) { // this is all that the second set of for loops should do board[row][col] = temp[row][col]; } }
Java Code:public class ConwayLifeFoo { private static final boolean[] TRUTH_GRID_ALIVE = { false, false, true, true, false, false, false, false // alive to start with }; private static final boolean[] TRUTH_GRID_DEAD = { false, false, true, false, false, false, false, false // dead to start with }; private static Map<Boolean, boolean[]> truthMap = new HashMap<Boolean, boolean[]>(); static { truthMap.put(Boolean.TRUE, TRUTH_GRID_ALIVE); truthMap.put(Boolean.FALSE, TRUTH_GRID_DEAD); } public static void genNextGrid(boolean[][] board) { // the safest way to initialize temp is to use the lengths from board. boolean[][] temp = new boolean[board.length][board[0].length]; // the first set of nested for loops calculates the values for the temp // array and nothing else. It DOES NOT CHANGE the board array for (int row = 0; row < 18; row++) { for (int col = 0; col < 18; col++) { int count = countNeighbors(board, row, col); // here use count to calc the proper value for the temp temp[row][col] = truthMap.get(board[row][col])[count]; // use count to calc whether cell should be alive or dead in next gen // update the temp array only. } } // the second set of nested for loops does nothing but // sets the board with the values held by temp for (int row = 0; row < 18; row++) { for (int col = 0; col < 18; col++) { board[row][col] = temp[row][col]; } } }
- 12-09-2009, 10:37 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
okay thank you for your help!!! I just have one question I understand the 2 loops better now, but in the first one you say to use count to to calc the value for temp. I just don't know how to do that...is that where I use my switch statement or is that all wrong? or would something like this work:
then use the second loop.Java Code:for( int row = 0; row < GRIDSIZE; row++) { for( int col = 0; col < GRIDSIZE; col++ ) { int count = countNeighbors( board, row, col ); if( board[row][col] < 2 ) { temp[row][col] = false; } if( board[row][col] == 3 ) { temp[row][col] = true; } if( board[row][col] >= 4 ) { temp[row][col] = false; } board[row][col] = 0; }board[row][col]= temp[row][col]
and very nice work on simplifying my code...if I could use that way I would...I'm just a beginner.
-
Yes, that's where you can use your switch statement, but again in that loop, use board, but just set temp, not board.
- 12-09-2009, 11:06 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
okay here is where I'm at now...i don't know if I did it right but I set temp to board and then at the end set board to temp. but it still doesn't give me the next generation, ughhh I'm kind of confused with this assignment.
Java Code:public static void genNextGrid ( boolean[][] board ) { boolean[][]temp = new boolean[GRIDSIZE][GRIDSIZE]; for( int row = 0; row < GRIDSIZE; row++) { for( int col = 0; col < GRIDSIZE; col++ ) { temp[row][col] = board[row][col]; int count = countNeighbors( temp, row, col ); switch(count) { 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; } board[row][col]= temp[row][col]; } } for( int row = 0; row < GRIDSIZE; row ++ ) { for( int col = 0; col < GRIDSIZE; col++ ) { board[row][col] = temp[row][col]; } } }
-
You're still setting the board from within the first set of loops!!!:Java Code:for( int row = 0; row < GRIDSIZE; row++) { for( int col = 0; col < GRIDSIZE; col++ ) { temp[row][col] = board[row][col]; int count = countNeighbors( temp, row, col ); switch(count) { 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; } board[row][col]= temp[row][col]; ///// **** WTF ???? **** } }
:eek:
- 12-09-2009, 11:34 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
sorry I sent you the wrong code. I fixed that mistake and it still doesn't do anything. Is any of my other methods wrong to make it not produce the next generation...or some advice you could give me...thanks for your help. This is suppose to be our hardest project...I'm just frustrated since I've been working on it forever it seems. I'm thinking maybe my countNeighbors method
-
- 12-09-2009, 11:59 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
okay now here is my code same except I deleted where I set board in the for loop. From what I could understand what I needed to do was use board but set temp not board and I believe that is what I did. let me know if I am wrong
Java Code:for( int row = 0; row < GRIDSIZE; row++) { for( int col = 0; col < GRIDSIZE; col++ ) { temp[row][col] = board[row][col]; int count = countNeighbors( temp, row, col ); switch(count) { 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; } } }
-
Shouldn't this:
be this?:Java Code:int count = countNeighbors(temp, row, col );
Big difference. Again, board holds the state of your grid now. temp is to hold the state of the grid in the future. You use the now state to help you determine the future state. Make sense?Java Code:int count = countNeighbors(board, row, col );
If this is still failing, you may need to post the whole code here. I'd recommend zipping it and uploading it if need be.
- 12-10-2009, 12:13 AM #11
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
I don't know how to zip it...The code is really long the only methods we have to write is the last 3 methods. I'm thinking my countNeighbors method isn't right.
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(); } } public static void clearGrid ( boolean[][] board ) { for( int row = 0; row < board.length; row++ ) { for( int col = 0; col < board[row].length; col++ ) { board[row][col] = false; } } } public static void genNextGrid ( boolean[][] board ) { boolean[][]temp = new boolean[GRIDSIZE][GRIDSIZE]; for( int row = 0; row < GRIDSIZE; row++) { for( int col = 0; col < GRIDSIZE; col++ ) { temp[row][col] = board[row][col]; int count = countNeighbors( board, row, col ); switch(count) { 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; } } } for( int row = 0; row < GRIDSIZE; row ++ ) { for( int col = 0; col < GRIDSIZE; col++ ) { board[row][col] = temp[row][col]; } } } 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; } }
-
I think you're right. Note the comment:I'm thinking my countNeighbors method isn't right.
instead ofJava Code: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] ) // *********** here **** { ncount++; } } } return ncount; }
shouldn't that be:Java Code:if (board[row][col] )
?Java Code:if (board[r][c] )
For me, cleaner is
Java Code: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 (r >= 0 && r < GRIDSIZE) { for (int c = col - 1; c <= col + 1; c++) { if (c >= 0 && c < GRIDSIZE && (r != row || c != col)) { if (board[r][c]) { ncount++; } } } } } return ncount; }
Note that my debugging technique was to add println statements in the code, in particular here:
well, actually a printf statement with a println statement after the inner loop.Java Code:public static void genNextGrid(boolean[][] board) { boolean[][] temp = new boolean[GRIDSIZE][GRIDSIZE]; for (int row = 0; row < GRIDSIZE; row++) { for (int col = 0; col < GRIDSIZE; col++) { temp[row][col] = board[row][col]; int count = countNeighbors(board, row, col); System.out.printf(" %1s%d, ", board[row][col]?"t":"f", count); switch (count) {
- 12-10-2009, 06:03 AM #13
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
YOU ARE MY HERO :) thank you so much I can't thank you enough!!!
- 12-10-2009, 06:06 AM #14
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
YOU ARE MY HERO :) thank you so much I can't thank you enough!!!
one more question before I leave you alone
my grid looks like this but with all the other stuff it's suppose to
my problem is it's suppose to 18 by 18 how do I fix that?
111111
1 2 3 4 5 6 7 8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-
Hm, have you tried sprinkling the code with judiciously labeled println statements and see what is happening at different locations in your code? Your best bet is to spend some time playing around with debugging techniques.
- 12-10-2009, 06:42 AM #16
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
okay I'll do that thank you!
- 12-12-2009, 03:53 AM #17
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
okay I hope this is the last time I have to ask for help :( this is due soon! I fixed my countNeighbors method because the one I had doesn't work for when it is checking row/ col 18 because you can't 18+1 when the grid is only 18..This is what I get when I click q and instead of asking for the next generation I get the error message. Thanks again!
Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? q
Viewing generation #1:
111111111
123456789012345678
1
2
3
4
5
6 **
7 *
8 *
9 *
10 *
11 *
12 **
13
14
15
16
17
18
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Life.countNeighbors(Life.java:260)
at Life.genNextGrid(Life.java:144)
at Life.main(Life.java:34)
here's my code: my cleargrid and genNextGeneration methods haven't chaged.
Java Code:public static int countNeighbors ( final boolean[][] board, final int row, final int col ) { int n = 0; //checks upper left, cells around row=0, col=0, corners if( row == 0 && col ==0 ) { if(board[row][col + 1] == true ) { n++; } if( board[row+ 1][col] == true ) { n++; } if( board[row + 1][col + 1] == true ) { n++; } } //bottom left else if( col == 0 && row == GRIDSIZE - 1 ) { if( board[row - 1][col] == true ) { n++; } if( board[row][col + 1] == true ) { n++; } if( board[row - 1][col + 1] == true ) { n++; } } //upper right else if( row == 0 && col == GRIDSIZE - 1 ) { if( board[row][col - 1] == true ) { n++; } if( board[row + 1][col] == true ) { n++; } if( board[row + 1][col - 1] == true ) { n++; } } //bottom right else if( row == GRIDSIZE - 1 && col == GRIDSIZE -1) { if( board[row - 1][col] == true ) { n++; } if( board[row][col - 1] == true ) { n++; } if( board[row - 1][col - 1] == true ) { n++; } } //edges(not corners), top edge if( row == 0 ) { if( board[row][col - 1] == true ) { n++; } if( board[row][col + 1] == true ) { n++; } if( board[row + 1][col] == true ) { n++; } if( board[row + 1][col - 1] == true ) { n++; } if( board[row + 1][col + 1] == true ) { n++; } } //bottom edge else if( row == GRIDSIZE - 1 ) { if( board[row][col - 1] == true ) { n++; } if( board[row][col + 1] == true ) { n++; } if( board[row - 1][col] == true ) { n++; } if( board[row - 1][col - 1] == true ) { n++; } if(board[row - 1][col + 1] == true ) { n++; } } //left edge else if( col == 0 ) { if( board[row][col + 1] == true ) { n++; } if( board[row - 1][col] == true ) { n++; } if( board[row - 1][col +1] == true ) { n++; } if( board[row + 1][col] == true ) { n++; } if( board[row + 1][col + 1] == true ) { n++; } } //right edge else if( col == GRIDSIZE - 1 ) { if(board[row - 1][col - 1] == true ) { n++; } if( board[row - 1][col] == true ) { n++; } if( board[row][col - 1] == true ) { n++; } if( board[row + 1][col - 1] == true ) { n++; } if( board[row + 1][col] == true ) { n++; } } //cell with 8 surronding cells else { //below if( board[row - 1][col - 1] == true ) { n++; } if( board[row - 1][col] == true ) { n++; } if( board[row - 1][col + 1] == true ) { n++; } // same if( board[row][col - 1] == true ) { n++; } if( board[row][col + 1] == true ) { n++; } //above if( board[row + 1][col - 1] == true ) { n++; } if( board[row + 1][col + 1] == true ) { n++; } if( board [row + 1][col] == true ) { n++; } } return n; } }Last edited by bigskers76; 12-12-2009 at 03:59 AM.
-
Your code? The line with the error message?
-
I see that you've modified your post to add some code. I suggest that you post it all, and that again, you show us where the error occurred. The key is to make it as easy as possible for someone to help you.
Edit: also, one other suggestion is to reduce your indentation size a little bit (3 or four spaces work well), and make sure your indentation is true. Currently there's some ugliness in your code. Again, the easier your code is to read, the easier it is to help. Much luck.
- 12-12-2009, 07:16 AM #20
Member
- Join Date
- Dec 2009
- Posts
- 15
- Rep Power
- 0
okay I hope this makes it clearer.
The thing about this program is the program was given to me except the last 3 methods (cleargrid, genNextgeneration, and countNeighbors) those are the ones I had to write.
here is my whole code and the errors again...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Life.countNeighbors(Life.java:260)
at Life.genNextGrid(Life.java:144)
at Life.main(Life.java:34)
I'll write where it says the codes is wrong
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); <-- This is line 34 error 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(); } } public static void clearGrid ( boolean[][] board ) { for( int row = 0; row < board.length; row++ ) { for( int col = 0; col < board[row].length; col++ ) { board[row][col] = false; } } } public static void genNextGrid ( boolean[][] board ) { boolean[][]temp = new boolean[GRIDSIZE][GRIDSIZE]; for( int row = 0; row < GRIDSIZE; row++) { for( int col = 0; col < GRIDSIZE; col++ ) { temp[row][col] = board[row][col]; int count = countNeighbors( board, row, col ); <--This is the Line 144 error switch(count) { 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; } } } for( int row = 0; row < GRIDSIZE; row ++ ) { for( int col = 0; col < GRIDSIZE; col++ ) { board[row][col] = temp[row][col]; } } } public static int countNeighbors ( final boolean[][] board, final int row, final int col ) { int n = 0; //checks upper left, cells around row=0, col=0, corners if( row == 0 && col ==0 ) { if(board[row][col + 1] == true ) { n++; } if( board[row+ 1][col] == true ) { n++; } if( board[row + 1][col + 1] == true ) { n++; } } //bottom left else if( col == 0 && row == GRIDSIZE - 1 ) { if( board[row - 1][col] == true ) { n++; } if( board[row][col + 1] == true ) { n++; } if( board[row - 1][col + 1] == true ) { n++; } } //upper right else if( row == 0 && col == GRIDSIZE - 1 ) { if( board[row][col - 1] == true ) { n++; } if( board[row + 1][col] == true ) { n++; } if( board[row + 1][col - 1] == true ) { n++; } } //bottom right else if( row == GRIDSIZE - 1 && col == GRIDSIZE -1) { if( board[row - 1][col] == true ) { n++; } if( board[row][col - 1] == true ) { n++; } if( board[row - 1][col - 1] == true ) { n++; } } //edges(not corners), top edge if( row == 0 ) { if( board[row][col - 1] == true ) <--This is the 260 line { n++; } if( board[row][col + 1] == true ) { n++; } if( board[row + 1][col] == true ) { n++; } if( board[row + 1][col - 1] == true ) { n++; } if( board[row + 1][col + 1] == true ) { n++; } } //bottom edge else if( row == GRIDSIZE - 1 ) { if( board[row][col - 1] == true ) { n++; } if( board[row][col + 1] == true ) { n++; } if( board[row - 1][col] == true ) { n++; } if( board[row - 1][col - 1] == true ) { n++; } if(board[row - 1][col + 1] == true ) { n++; } } //left edge else if( col == 0 ) { if( board[row][col + 1] == true ) { n++; } if( board[row - 1][col] == true ) { n++; } if( board[row - 1][col +1] == true ) { n++; } if( board[row + 1][col] == true ) { n++; } if( board[row + 1][col + 1] == true ) { n++; } } //right edge else if( col == GRIDSIZE - 1 ) { if(board[row - 1][col - 1] == true ) { n++; } if( board[row - 1][col] == true ) { n++; } if( board[row][col - 1] == true ) { n++; } if( board[row + 1][col - 1] == true ) { n++; } if( board[row + 1][col] == true ) { n++; } } //cell with 8 surronding cells else { //below if( board[row - 1][col - 1] == true ) { n++; } if( board[row - 1][col] == true ) { n++; } if( board[row - 1][col + 1] == true ) { n++; } // same if( board[row][col - 1] == true ) { n++; } if( board[row][col + 1] == true ) { n++; } //above if( board[row + 1][col - 1] == true ) { n++; } if( board[row + 1][col + 1] == true ) { n++; } if( board [row + 1][col] == true ) { n++; } } return n; } }
Similar Threads
-
game of life
By bigskers76 in forum New To JavaReplies: 10Last Post: 12-09-2009, 05:21 AM -
2d array help- Game of Life
By n3philim in forum New To JavaReplies: 2Last Post: 07-23-2009, 12:01 AM -
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


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks