# Help with understanding my current coding of boolean array

• (2 Pages)
• 1
• 2

## 27 Replies - 889 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=49bd4d30dbbce1a3579784ba4ccf5b1d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 Cuzzie

• D.I.C Regular

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

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

Posted 13 September 2012 - 04:54 AM

programmingdoesmyheadin, on 13 September 2012 - 11:23 AM, said:

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

Which methods are you having problems with? nextGeneration? countNeighbours? Or something else?

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

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

Posted 13 September 2012 - 05:21 AM

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

int count = countNeighbours(newBoard, 1, 1);
if (count < 2)
{
newBoard[row][column] = board[row][column]
newBoard[row][column] =".";

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

```

would this be right? better yet, does it answer the question? lol
Sorry, I'm just trying to figure this out considering I can't seem to get my head around this language at all

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

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

Posted 13 September 2012 - 05:27 AM

Cuzzie, on 13 September 2012 - 04:54 AM, said:

programmingdoesmyheadin, on 13 September 2012 - 11:23 AM, said:

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

Which methods are you having problems with? nextGeneration? countNeighbours? Or something else?

I was having trouble with nextGeneration but I think I figured it out.. do you mind checking my code (my last post) and telling me if it's right?

### #19 Judison

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

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

Posted 13 September 2012 - 06:58 AM

programmingdoesmyheadin, on 13 September 2012 - 05:27 AM, said:

Cuzzie, on 13 September 2012 - 04:54 AM, said:

programmingdoesmyheadin, on 13 September 2012 - 11:23 AM, said:

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

Which methods are you having problems with? nextGeneration? countNeighbours? Or something else?

I was having trouble with nextGeneration but I think I figured it out.. do you mind checking my code (my last post) and telling me if it's right?

Helllooo I finished my code, but your code is wrong, I'll give you a hint don't use 1,1 , does you "." "*" any of the strings, use true or false instead

If your unsw like me, read the last 3 weeks slides, you'll get it

### #20 pbl

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

Reputation: 8066
• Posts: 31,310
• Joined: 06-March 08

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

Posted 13 September 2012 - 02:14 PM

Quote

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 have already told you, you can't do it with just boolean
You need a class for every cell that contains actualState, nextGenerationState
when you are all dione you copy the nextGenerationState into the actualState
The same class can hold the number of neighbors this simplifies the coding
and at construction time you can even build a list of the neineighbors... thats simplify a lot the coding

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

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

Posted 13 September 2012 - 05:30 PM

[/quote]

Helllooo I finished my code, but your code is wrong, I'll give you a hint don't use 1,1 , does you "." "*" any of the strings, use true or false instead

[/quote]

Heyy Sorry, but I don't understand what you are trying to say here So the bit where i put 1,1 is wrong.. but is that the only thing i need to change? is everything else right?

Do i just get rid of the 1,1 without replacing it? and are you saying to replace ".""*" with true and false?

Sorry i just need clarification

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

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

Posted 13 September 2012 - 06:07 PM

Okay, I think i've figured it out...
Except, i just don't know what to put instead of 1,1 considering we dont know how many rows and cols there are? any suggestions?

```
public static boolean[][] nextGeneration(boolean[][] board1)
{
int row=0;
int col=0;
boolean [][] newBoard = new boolean[board1.length]
[board1[row].length];

int count = countNeighbours(newBoard, 1, 1);
if (count < 2 || count > 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = false;

}
else if (count == 2 || count == 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = true;
}
return newBoard;
}

```

DOES THAT ANSWER THE QUESTION THOUGH? It compiles but I'm not sure if it answers the question

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

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

Posted 13 September 2012 - 06:18 PM

Sorry, last post i promise! haha

is this bit necessary within both if statements or not?

```
newBoard[row][col] = board1[row][col];

```

### #24 Cuzzie

• D.I.C Regular

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

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

Posted 13 September 2012 - 06:38 PM

programmingdoesmyheadin, on 14 September 2012 - 01:07 AM, said:

Okay, I think i've figured it out...
Except, i just don't know what to put instead of 1,1 considering we dont know how many rows and cols there are? any suggestions?

```
public static boolean[][] nextGeneration(boolean[][] board1)
{
int row=0;
int col=0;
boolean [][] newBoard = new boolean[board1.length]
[board1[row].length];

int count = countNeighbours(newBoard, 1, 1);
if (count < 2 || count > 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = false;

}
else if (count == 2 || count == 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = true;
}
return newBoard;
}

```

DOES THAT ANSWER THE QUESTION THOUGH? It compiles but I'm not sure if it answers the question

You are assigning 0 to both row and col in the method, so that means every time you call the method, you will only be modifying newBoard[0][0].

programmingdoesmyheadin, on 14 September 2012 - 01:18 AM, said:

is this bit necessary within both if statements or not?

```
newBoard[row][col] = board1[row][col];

```

```newBoard[row][col] = board1[row][col];
newBoard[row][col] = false;

```

The value of board1[row][col] that you assign to newBoard[row][col] will be replaced by "false" straight away.

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

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

Posted 13 September 2012 - 06:55 PM

Quote

You are assigning 0 to both row and col in the method, so that means every time you call the method, you will only be modifying newBoard[0][0].

okay, if that's the case then how am i supposed to initialise row and col correctly?

Quote

The value of board1[row][col] that you assign to newBoard[row][col] will be replaced by "false" straight away.

So are you saying that I don't need 'newBoard[row][col] = board1[row][col];'? or that my if statement is wrong altogether?

### #26 Judison

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

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

Posted 13 September 2012 - 08:08 PM

programmingdoesmyheadin, on 13 September 2012 - 06:07 PM, said:

Okay, I think i've figured it out...
Except, i just don't know what to put instead of 1,1 considering we dont know how many rows and cols there are? any suggestions?

```
public static boolean[][] nextGeneration(boolean[][] board1)
{
int row=0;
int col=0;
boolean [][] newBoard = new boolean[board1.length]
[board1[row].length];

int count = countNeighbours(newBoard, 1, 1);
if (count < 2 || count > 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = false;

}
else if (count == 2 || count == 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = true;
}
return newBoard;
}

```

DOES THAT ANSWER THE QUESTION THOUGH? It compiles but I'm not sure if it answers the question

change 1,1 to row and col not sure sure about the rest of the code..... don't really understand it sorry

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

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

Posted 13 September 2012 - 09:15 PM

Judison, on 13 September 2012 - 08:08 PM, said:

programmingdoesmyheadin, on 13 September 2012 - 06:07 PM, said:

Okay, I think i've figured it out...
Except, i just don't know what to put instead of 1,1 considering we dont know how many rows and cols there are? any suggestions?

```
public static boolean[][] nextGeneration(boolean[][] board1)
{
int row=0;
int col=0;
boolean [][] newBoard = new boolean[board1.length]
[board1[row].length];

int count = countNeighbours(newBoard, 1, 1);
if (count < 2 || count > 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = false;

}
else if (count == 2 || count == 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = true;
}
return newBoard;
}

```

DOES THAT ANSWER THE QUESTION THOUGH? It compiles but I'm not sure if it answers the question

change 1,1 to row and col not sure sure about the rest of the code..... don't really understand it sorry

Thanks for your help Did you end up finding a way to implement the rules? because i cant figure out a way to code whether a cell is alive or dead in the nextGeneration method

### #28 Judison

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

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

Posted 13 September 2012 - 10:51 PM

programmingdoesmyheadin, on 13 September 2012 - 09:15 PM, said:

Judison, on 13 September 2012 - 08:08 PM, said:

programmingdoesmyheadin, on 13 September 2012 - 06:07 PM, said:

Okay, I think i've figured it out...
Except, i just don't know what to put instead of 1,1 considering we dont know how many rows and cols there are? any suggestions?

```
public static boolean[][] nextGeneration(boolean[][] board1)
{
int row=0;
int col=0;
boolean [][] newBoard = new boolean[board1.length]
[board1[row].length];

int count = countNeighbours(newBoard, 1, 1);
if (count < 2 || count > 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = false;

}
else if (count == 2 || count == 3)
{
newBoard[row][col] = board1[row][col];
newBoard[row][col] = true;
}
return newBoard;
}

```

DOES THAT ANSWER THE QUESTION THOUGH? It compiles but I'm not sure if it answers the question

change 1,1 to row and col not sure sure about the rest of the code..... don't really understand it sorry

Thanks for your help Did you end up finding a way to implement the rules? because i cant figure out a way to code whether a cell is alive or dead in the nextGeneration method

true and false is the way of determining if the cell is alive or dead for our assignment, you don't use any of the strings, you might want to add if board is true in the if function