a
Printable View
a
Can you use an if test to detect if the location is already occupied before changing it?Quote:
it overwrites the array even if there is a car already in that specific location.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
That's the problem, I don't know how to implement that ..
Thanks for your quick reply
How do you know there is a car in the location? What is in the array?
The Arrays class's deepToString() method is useful for printing out the contents of a 2 dim array for debugging:
Code :System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
03,03,00,00,00,04
02,00,00,08,00,04
02,01,01,08,00,04
02,00,00,08,00,00
05,00,00,00,07,07
05,00,06,06,06,00
--->
03,03,00,00,00,04
02,00,00,08,00,04
02,00,01,01,00,04
02,00,00,08,00,00
05,00,00,00,07,07
05,00,06,06,06,00
This is my gameboard, When I move Car 01 to the right, it overwrites a part of 08.
In the code you can find how I move the cars. Now i'm looking for a solution which makes overwriting impossible
The numbers in the array have no meaning for me.
Can you answer this question?
What value(s) in the array means that it is OK to move a car there?
Use a if statement to detect if the array at the location has that value. If the array does not have that value, do not move the car there.
The zeroes are empty spaces, the number are all different cars.
So it's ok to move a car to a zero. As you can see above i made some methodes which move the car.
So now I woud like a if clause which checks if the location which im moving to is 0. If it's 0, it can move, otherwise it shouldn't.
Are you asking how to write an if statement?
See the tutorial: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
--- Update ---
Please make the thread as solved.