# Help with understanding my current coding of boolean array

• (2 Pages)
• 1
• 2

## 27 Replies - 1504 Views - Last Post: 13 September 2012 - 10:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291686&amp;s=c72c2e339929035a1ddaec0d1bb2e004&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Judison

Reputation: 1
• Posts: 32
• Joined: 08-August 12

# Help with understanding my current coding of boolean array

Posted 12 September 2012 - 12:04 AM

For homework I'm suppose to design some codes for the "game of life"

For this task I have to
Create a new 2D boolean array that has the same size as the
parameter array.
Implement the reproduction rules to fill in the new array
based on the parameter array
return the new array

Rules of game of life is
Any live cell with fewer than two live neighbours dies, as if caused by under-population.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overcrowding.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

I'm stuck on programming, this a piece of code I've done for the first rule and a a bit on the second

```    public static boolean[][] nextGeneration(boolean[][] board1)
{

boolean [][] newBoard;

if (board[row][column] == "*" && count < 2)
{

newBoard[row][column] = board[row][column];
newBoard[row][column] =".";

}
else if (board[r][c] == "*" && count== 2)
{
newBoard[row][column] = board[row][column];
newBoard[row][column] ="*";

}

```

I'm having so much trouble understanding coding, i used to vairable board instead of board1 because I already assigned for board as you see here

```public static int countNeighbours(boolean[][] board, int row, int col)
{
int count = 0;

if (row -1 >= 0 && col -1 >= 0)
{ if (board[row-1][col-1] == true) {count++;} }
if (row -1 >= 0 && col > 0)
{ if (board[row-1][col] == true){count++;}}
if (row -1 >= 0 && col + 1 > 1)
{ if (board[row-1][col+1] == true){count++;}}
if (row > 0 && col - 1 >= 0)
{ if (board[row][col-1] == true){count++;}}
if (row > 0 && col + 1 > 1)
{ if (board[row][col+1] == true){count++;}}
if (row + 1 > 0 && col -1 >= 0)
{ if (board[row+1][col-1] == true){count++;}}
if (row + 1 > 1 && col > 0)
{ if (board[row+1][col] == true){count++;}}
if (row + 1 > 0 && col + 1 > 1)
{ if (board[row+1][col+1] == true){count++;}}
return count;
}
```

obviously board doesn't work in my coding since board1 is defined not board, but i'm soo confused on what to assign, can you please explain to me and help give me a solution to my problem. all opinions welcomed

Is This A Good Question/Topic? 0

## Replies To: Help with understanding my current coding of boolean array

### #2 Cuzzie

• D.I.C Regular

Reputation: 72
• Posts: 341
• Joined: 16-July 10

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 12:20 AM

First of all, in your nextGeneration method, you haven't initialized your 2D boolean array yet...

```// That is if your newBoard has 10 rows and 10 columns
boolean[][] newBoard = new boolean[10][10];

```

Also, I see that you're using the variable "board" in nextGeneration method. Is "board" a global variable that you're using throughout the entire class, or are you supposed to use the "board1" argument that's passed into the method?

```if (board[row][column] == "*" && count < 2)

```

"board" is a 2D boolean array, so how can you compare it with a string? It would only make sense if you compare it with either true or false.

```newBoard[row][column] =".";

```

Same thing here, you declared "newBoard" as a 2D boolean array, so it is impossible for you to assign a String value to it.

You should probably fix these first, and try to understand what the variables you've declared are for.

### #3 Judison

Reputation: 1
• Posts: 32
• Joined: 08-August 12

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 12:32 AM

Cuzzie, on 12 September 2012 - 12:20 AM, said:

First of all, in your nextGeneration method, you haven't initialized your 2D boolean array yet...

```// That is if your newBoard has 10 rows and 10 columns
boolean[][] newBoard = new boolean[10][10];

```

Also, I see that you're using the variable "board" in nextGeneration method. Is "board" a global variable that you're using throughout the entire class, or are you supposed to use the "board1" argument that's passed into the method?

```if (board[row][column] == "*" && count < 2)

```

"board" is a 2D boolean array, so how can you compare it with a string? It would only make sense if you compare it with either true or false.

```newBoard[row][column] =".";

```

Same thing here, you declared "newBoard" as a 2D boolean array, so it is impossible for you to assign a String value to it.

You should probably fix these first, and try to understand what the variables you've declared are for.

Thanks for your reply, in the task number of row and columns isn't specified, in the initial parameter it's defined at row and col, these variables were defined in another method "count neighbours". I was not sure how to implement this into the current method "new gen". I was given board1 [][] I assumed it was the parameter array they were referring to... but.... there is no parameters, i'm absolutely lost...

on your second point, I'm confused! I assumed you use board1 but everything is already defined in board.... as in the neighbour count is in another method so the rule is

Any live cell with fewer than two live neighbours dies, as if caused by under-population.

And so count<2 but all of that is stored into board so I feel dead...

and 3rdly, sorry my mistake i meant to write '*' I have live defined as '*' in another method

### #4 Cuzzie

• D.I.C Regular

Reputation: 72
• Posts: 341
• Joined: 16-July 10

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 01:14 AM

Okay, if the number of rows and columns of the board isn't specified, then I guess you have the freedom to set how big you want it to be.

Quote

these variables were defined in another method "count neighbours"

No, you did not declare any "row" or "col" variable in your countNeighbours method, "row" and "col" are both parameters that are passed into the method.

Quote

on your second point, I'm confused! I assumed you use board1 but everything is already defined in board....

I don't really get what you're saying. The "board" variable in public static int countNeighbours(boolean[][] board, int row, int col) is a parameter of countNeighbours method, meaning "board" can only be used inside the countNeighbours method itself. You cannot use it in your nextGeneration method like that.

Seems like you are pretty confused with the basics of method parameters. I think you should read some of the Java tutorials on Methods.

Quote

and 3rdly, sorry my mistake i meant to write '*' I have live defined as '*' in another method

The thing is, "*" is a String, whereas your "board" is a boolean array. You need to use a String array if you want to store "*" in it.

This post has been edited by Cuzzie: 12 September 2012 - 01:15 AM

### #5 Judison

Reputation: 1
• Posts: 32
• Joined: 08-August 12

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 02:34 AM

Cuzzie, on 12 September 2012 - 01:14 AM, said:

Okay, if the number of rows and columns of the board isn't specified, then I guess you have the freedom to set how big you want it to be.

Quote

these variables were defined in another method "count neighbours"

No, you did not declare any "row" or "col" variable in your countNeighbours method, "row" and "col" are both parameters that are passed into the method.

Quote

on your second point, I'm confused! I assumed you use board1 but everything is already defined in board....

I don't really get what you're saying. The "board" variable in public static int countNeighbours(boolean[][] board, int row, int col) is a parameter of countNeighbours method, meaning "board" can only be used inside the countNeighbours method itself. You cannot use it in your nextGeneration method like that.

Seems like you are pretty confused with the basics of method parameters. I think you should read some of the Java tutorials on Methods.

Quote

and 3rdly, sorry my mistake i meant to write '*' I have live defined as '*' in another method

The thing is, "*" is a String, whereas your "board" is a boolean array. You need to use a String array if you want to store "*" in it.

Ok I do get where you're coming from now, thanks for that, I am reading into now but i'm still alittle confused on what bit
I am supposed to make a new array that has the same parameters of the old array, for the old array i was just given boolean[][] board1, no where in my question does it give me any parameters, initially I assumed [board1.length] but it's a two d array and does not work, what do you recommend?

It works as
``` boolean [][] newBoard = new boolean [board1.length];
```
is it right?

This post has been edited by Judison: 12 September 2012 - 02:36 AM

### #6 Cuzzie

• D.I.C Regular

Reputation: 72
• Posts: 341
• Joined: 16-July 10

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 04:02 AM

Quote

I am supposed to make a new array that has the same parameters of the old array, for the old array i was just given boolean[][] board1

Where do you want to make a new array? In your nextGeneration method? If that's the case, you can declare a "newBoard" and assign "board1" to it.

```public static boolean[][] nextGeneration(boolean[][] board1)
{

boolean [][] newBoard = board1;

// ...

}

```

So in your main method, let's say you are passing in a 2D boolean array into nextGeneration method..

```public static void main(String[] args){

boolean[][] myBoard = new boolean[10][10];

nextGeneration(myBoard);

}

```

The "myBoard" boolean array will be passed into the method, and then assigned to "newBoard". "newBoard" will have the same data as "myBoard", and then it will be up to you how you want to play with it in the method.

Once you have done whatever you need to do with "newBoard", you will have to return it from your method.

```return newBoard;

```

To pick up the returned object, you can edit the main method I posted above..

```boolean[][] myNewBoard = nextGeneration(myBoard);

```

Hope you understand what I'm talking about.

Reputation: 0
• Posts: 11
• Joined: 12-September 12

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 04:37 AM

Cuzzie, on 12 September 2012 - 04:02 AM, said:

Quote

I am supposed to make a new array that has the same parameters of the old array, for the old array i was just given boolean[][] board1

Where do you want to make a new array? In your nextGeneration method? If that's the case, you can declare a "newBoard" and assign "board1" to it.

```public static boolean[][] nextGeneration(boolean[][] board1)
{

boolean [][] newBoard = board1;

// ...

}

```

So in your main method, let's say you are passing in a 2D boolean array into nextGeneration method..

```public static void main(String[] args){

boolean[][] myBoard = new boolean[10][10];

nextGeneration(myBoard);

}

```

The "myBoard" boolean array will be passed into the method, and then assigned to "newBoard". "newBoard" will have the same data as "myBoard", and then it will be up to you how you want to play with it in the method.

Once you have done whatever you need to do with "newBoard", you will have to return it from your method.

```return newBoard;

```

To pick up the returned object, you can edit the main method I posted above..

```boolean[][] myNewBoard = nextGeneration(myBoard);

```

Hope you understand what I'm talking about.

Reputation: 0
• Posts: 11
• Joined: 12-September 12

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 04:43 AM

programmingdoesmyheadin, on 12 September 2012 - 04:37 AM, said:

Cuzzie, on 12 September 2012 - 04:02 AM, said:

Quote

I am supposed to make a new array that has the same parameters of the old array, for the old array i was just given boolean[][] board1

Where do you want to make a new array? In your nextGeneration method? If that's the case, you can declare a "newBoard" and assign "board1" to it.

```public static boolean[][] nextGeneration(boolean[][] board1)
{

boolean [][] newBoard = board1;

// ...

}

```

So in your main method, let's say you are passing in a 2D boolean array into nextGeneration method..

```public static void main(String[] args){

boolean[][] myBoard = new boolean[10][10];

nextGeneration(myBoard);

}

```

The "myBoard" boolean array will be passed into the method, and then assigned to "newBoard". "newBoard" will have the same data as "myBoard", and then it will be up to you how you want to play with it in the method.

Once you have done whatever you need to do with "newBoard", you will have to return it from your method.

```return newBoard;

```

To pick up the returned object, you can edit the main method I posted above..

```boolean[][] myNewBoard = nextGeneration(myBoard);

```

Hope you understand what I'm talking about.

Sorry about the previous post, I'm kind of new to this! lol
Do you mind applying the changes to the code so I can see what you've actually done? because now it just looks like a whole bunch of individual statements which my brain is finiding difficult to process haha

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 05:20 AM

Don't want to be a party breaker but really don't see why you would need an array of boolean for a game of life
You need a Cell class that knows for every Cell:
- it's state (that one may be a boolean)
- the number of neighbourg (obvioulsly an int)
- its state in the next generation

### #10 Judison

Reputation: 1
• Posts: 32
• Joined: 08-August 12

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 05:21 AM

programmingdoesmyheadin, on 12 September 2012 - 04:43 AM, said:

programmingdoesmyheadin, on 12 September 2012 - 04:37 AM, said:

Cuzzie, on 12 September 2012 - 04:02 AM, said:

Quote

I am supposed to make a new array that has the same parameters of the old array, for the old array i was just given boolean[][] board1

Where do you want to make a new array? In your nextGeneration method? If that's the case, you can declare a "newBoard" and assign "board1" to it.

```public static boolean[][] nextGeneration(boolean[][] board1)
{

boolean [][] newBoard = board1;

// ...

}

```

So in your main method, let's say you are passing in a 2D boolean array into nextGeneration method..

```public static void main(String[] args){

boolean[][] myBoard = new boolean[10][10];

nextGeneration(myBoard);

}

```

The "myBoard" boolean array will be passed into the method, and then assigned to "newBoard". "newBoard" will have the same data as "myBoard", and then it will be up to you how you want to play with it in the method.

Once you have done whatever you need to do with "newBoard", you will have to return it from your method.

```return newBoard;

```

To pick up the returned object, you can edit the main method I posted above..

```boolean[][] myNewBoard = nextGeneration(myBoard);

```

Hope you understand what I'm talking about.

Sorry about the previous post, I'm kind of new to this! lol
Do you mind applying the changes to the code so I can see what you've actually done? because now it just looks like a whole bunch of individual statements which my brain is finiding difficult to process haha

Sorry for my confusing you haha!
Well I've defined the new array

Now i Just have to figure out a code to apply the rules
I figured
```public static boolean[][] nextGeneration(boolean[][] board1)
{
// Create a new 2D boolean array that has the same size as the
// parameter array.

boolean [][] newBoard = new boolean[board1.length][board1[0].length];

// Implement the reproduction rule 1 to fill the new array
```

here is my main problem, so one of the rules was that if a cell has fewer than two live neighbours then it's dies. A live neighbour is defined as true, I'm really uncertain how to implement this because I would need to count the neighbours right? But I'm not supposed to count it in this method, as I have done it for another separate method.

I wanted a code like
``` if (board1 == true & count != 2)
```
or something similar, but count is not defined and has no numerical value for this method, but does for another method.

### #11 Judison

Reputation: 1
• Posts: 32
• Joined: 08-August 12

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 05:21 AM

programmingdoesmyheadin, on 12 September 2012 - 04:43 AM, said:

programmingdoesmyheadin, on 12 September 2012 - 04:37 AM, said:

Cuzzie, on 12 September 2012 - 04:02 AM, said:

Quote

I am supposed to make a new array that has the same parameters of the old array, for the old array i was just given boolean[][] board1

Where do you want to make a new array? In your nextGeneration method? If that's the case, you can declare a "newBoard" and assign "board1" to it.

```public static boolean[][] nextGeneration(boolean[][] board1)
{

boolean [][] newBoard = board1;

// ...

}

```

So in your main method, let's say you are passing in a 2D boolean array into nextGeneration method..

```public static void main(String[] args){

boolean[][] myBoard = new boolean[10][10];

nextGeneration(myBoard);

}

```

The "myBoard" boolean array will be passed into the method, and then assigned to "newBoard". "newBoard" will have the same data as "myBoard", and then it will be up to you how you want to play with it in the method.

Once you have done whatever you need to do with "newBoard", you will have to return it from your method.

```return newBoard;

```

To pick up the returned object, you can edit the main method I posted above..

```boolean[][] myNewBoard = nextGeneration(myBoard);

```

Hope you understand what I'm talking about.

Sorry about the previous post, I'm kind of new to this! lol
Do you mind applying the changes to the code so I can see what you've actually done? because now it just looks like a whole bunch of individual statements which my brain is finiding difficult to process haha

Sorry for my confusing you haha!
Well I've defined the new array

Now i Just have to figure out a code to apply the rules
I figured
```public static boolean[][] nextGeneration(boolean[][] board1)
{
// Create a new 2D boolean array that has the same size as the
// parameter array.

boolean [][] newBoard = new boolean[board1.length][board1[0].length];

// Implement the reproduction rule 1 to fill the new array
```

here is my main problem, so one of the rules was that if a cell has fewer than two live neighbours then it's dies. A live neighbour is defined as true, I'm really uncertain how to implement this because I would need to count the neighbours right? But I'm not supposed to count it in this method, as I have done it for another separate method.

I wanted a code like
``` if (board1 == true & count != 2)
```
or something similar, but count is not defined and has no numerical value for this method, but does for another method.

### #12 Cuzzie

• D.I.C Regular

Reputation: 72
• Posts: 341
• Joined: 16-July 10

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 05:54 AM

Gee, I'm really confused now.. and why do you have 2 accounts?

```public static boolean[][] nextGeneration(boolean[][] board1)
{
// Create a new 2D boolean array that has the same size as the
// parameter array.

boolean [][] newBoard = new boolean[board1.length][board1[0].length];

// Pass in newBoard into countNeighbours method
// and then assign the returned value to "count"
// I put row and column as 1 here for testing purposes
int count = countNeighbours(newBoard, 1, 1);

if(count < 2){

}

}

```

Not sure if I understand your problems right..

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Help with understanding my current coding of boolean array

Posted 12 September 2012 - 06:41 AM

Look here
http://www.dreaminco...snippet4501.htm
it is not to give you the code (mine is a GUI) but the logic you are looking for is in the class that extends JLabel. It takges care of the generation and to display the JLabel.
Have fun

Reputation: 0
• Posts: 11
• Joined: 12-September 12

## Re: Help with understanding my current coding of boolean array

Posted 13 September 2012 - 04:23 AM

Cuzzie, on 12 September 2012 - 05:54 AM, said:

Gee, I'm really confused now.. and why do you have 2 accounts?

```public static boolean[][] nextGeneration(boolean[][] board1)
{
// Create a new 2D boolean array that has the same size as the
// parameter array.

boolean [][] newBoard = new boolean[board1.length][board1[0].length];

// Pass in newBoard into countNeighbours method
// and then assign the returned value to "count"
// I put row and column as 1 here for testing purposes
int count = countNeighbours(newBoard, 1, 1);

if(count < 2){

}

}

```

Not sure if I understand your problems right..

That's basically what I had done so far but the problem I'm really having with the code is the method ! I don't know how to go about writing it.. I dont even know how to start
I seriously don't get this.. The first two codes were much easier haha
P.S. This is another person(not the one who originally posted the question.. but I'm having the same problem).. haha sorry for the confusion
And thank you so much for the explanation! You're a million times better/ more helpful than my tutor haha

Reputation: 0
• Posts: 11
• Joined: 12-September 12

## Re: Help with understanding my current coding of boolean array

Posted 13 September 2012 - 04:47 AM

```
int count = countNeighbours(newBoard, 1, 1);

```

Also, you know how you used the 1's.. what are they for cause you used them for testing purposed but we dont have actual dimensions to work off(if that makes sense)