Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2D Boolean array and randomness

 
D diller
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Henry
 
D diller
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic