This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# boolean array

DANIEL GRESHAM
Greenhorn
Posts: 21
is there any way to place a charecter on a (primative data type)2d boolean array denoting a position that is not a boolean assigned value? ie if my case returns thaat im moving north, and I want place a "^" in my current index floor[_current_row][_current_col]. any help would be much appreciated
Danny

marc weber
Sheriff
Posts: 11343
If the array is defined to hold boolean values, then it can only hold boolean values.

If you want these array elements to store information beyond "true" or "false," then you might reconsider the array type.

David Harkness
Ranch Hand
Posts: 1646
Can you describe what you're trying to accomplish instead of starting with the how? I'm guessing you're making a maze where the floor matrix (two dimensional array) value at each position is true to represent a wall and false to represent open space. If that's correct, why do you want to store a '^' there? What does that represent?

If you can explain that part, we can suggest other ways to go about it. As Marc said, booleans hold either true or false -- that's it. You could switch to a numeric type like byte/short/int and define appropriate constants.

marc weber
Sheriff
Posts: 11343
Actually, I was wondering whether Java's new enum feature might be helpful here.

DANIEL GRESHAM
Greenhorn
Posts: 21
my boolean array is an imaginary floor that a rat can traverse, the rat can move in 8 directions, north, north east, yada yada yada. The rat has a pen attached to its tail, when the pen is up it moves and I currently have the code
System.out.println("My current position is row," + (_current_row + 1) + " column," + (_current_col + 1) + " and I am facing (//whatever direction respectively//)");
I would like to replace this with a directional arrow of some kind. Ok forgive my ignorace on this issue, but I've heard of something called "overloading?" does this apply? Sorry, Im only on the array chapter in my book, so I have no idea what this really is or hwat it does.

DANIEL GRESHAM
Greenhorn
Posts: 21
By the way thanx for the help thus far
-Danny-

David Harkness
Ranch Hand
Posts: 1646
From your descriptino I gather that on top of the floor array you also track the current row, column, and facing direction of the rat and whether the pen is up or down. These should be stored separate from the floor array in my opinion.

A Rat class could track the first three and a Pen class the pen's state. Do you need to record everywhere the rat went? Is that what the floor array tracks? You need to specify all the things your program needs to "know" (keep track of) at all times and model them somehow.

Can you correct what I have above?

Finally, you also mention you want to replace a line saying "I'm at x, y, facing z" with a "directional arrow". Is that all that you want to display? You've got decent choices for the four cardinal directions -- < > v ^ -- but I'm not sure what you'll do about the diagonal ones.

Marc, I usually don't advise the JDK 1.5 features simply because many people cannot use it yet or will get used to it starting out and then start a job where they can't use it. Have you found this to be changing? My projects have just started using JDK 1.4.

marc weber
Sheriff
Posts: 11343
Originally posted by David Harkness:
...I usually don't advise the JDK 1.5 features simply because many people cannot use it yet or will get used to it starting out and then start a job where they can't use it...

You're absolutely right! I think we just moved to Java 1.4 at work (although I'm technically a Lotus Notes Developer -- not a Java Programmer). I mentioned the enum feature only because I'm curious whether this would actually be an appropriate use of it. I've only read about it, and am still unclear on the benefits.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
If you settle on how to draw your direction, you can definitely make an array of graphics or ascii graphics and pick the right one out. So with the numbers representing directions as David showed above:

System.out.print("I'm facing " + displayDirection[ direction ] );

Ascii graphics may be a bit tough to do on a single line. The strings "east" , "west", "northeast" or whatever would be dead easy, tho.

String[] displayDirection = new String[] { "east", "west" ... }