This week's book giveaway is in the Security forum.We're giving away four copies of Penetration Testing Basics and have Ric Messier on-line!See this thread for details.
Win a copy of Penetration Testing Basics this week in the Security forum!

# 2D Boolean array and randomness

D diller
Greenhorn
Posts: 29
Below is some simple code I am working on to essentially randomly create true or false values in a 2D boolean array. Presently I am using the following code, and it keeps on making the first row (board[0][x]) 'true' and all other values false, which makes me wonder:

1 - Are elements in a boolean array automatically assigned a value?
2 - Will my embedded 'while' loops fail after the first row?
3 - Is there an easier way other than making a random number and testing its value against the mid-ground?

Any help would be appreciated.>

Henry Wong
author
Marshal
Posts: 21702
85

The condition is actually not a condition, but an assignment -- notice the single equals sign.

Henry

D diller
Greenhorn
Posts: 29
D'oh! Stupid me for overlooking that; all is great now, except that it still only changes around my first row, it's like it doesn't ever actually run the outside-most loop again.

Henry Wong
author
Marshal
Posts: 21702
85
D diller wrote:D'oh! Stupid me for overlooking that; all is great now, except that it still only changes around my first row, it's like it doesn't ever actually run the outside-most loop again.

Actually, it doesn't "run" the inner loop again -- notice that you never reset your y index for the next row. It is only set once, hence, it only does one row.

Henry

D diller
Greenhorn
Posts: 29
Got it, just had to use a for loop instead on the inside.. Bingo! Thanks again for helping me identify that = instead of ==.

Campbell Ritchie
Sheriff
Posts: 50638
82
D diller wrote:if (board[x][y] = true)
Henry has already commented, but the correct format would be

if (board[x][y]) . . .

D diller
Greenhorn
Posts: 29
Campbell Ritchie wrote:
D diller wrote:if (board[x][y] = true)
Henry has already commented, but the correct format would be

if (board[x][y]) . . .

Erm, shouldn't I haveinstead of? Won't it error out on the second way?

D diller
Greenhorn
Posts: 29
I'm now having some problems with getting my GUI to display properly - I've looked over all the code I can think of but I just can't narrow down what my problem is.

And the code for the GUI in the only spot I change the color of any cell:
It calls on life.isAlive:
So, simply, it returns true if it's true or false if it's false. No if statements or anything to return; even if I change this to return false; I get the same result.
Does this code look okay for going through and setting a specific number of true values? I tested it on a 3x3 array with numtolive = 2, and it seemed to work fine; my GUI displays all as 'true' though, even if I know 100% for sure the value is false (as in I set everything to false, and even if I didn't the default boolean value is false)

Campbell Ritchie
Sheriff
Posts: 50638
82
D diller wrote:
Erm, shouldn't I haveinstead of? Won't it error out on the second way?
No. The == false is redundant, and you have already seen, it is too easy to write = instead of == which can cause nasty errors later on.

If you want to test false, you use the "not" operator . . .

if (!board[1][2]) . . .

or

if (!(x < 3 && y > 4)) . . .

Note the additional () because ! has a higher precedence than && or < or >.

So you should never write == true or == false.

I think the second version will throw a compiler error because the == false is outside the ().