Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
Members have full access to the forums. Advertisements are removed for registered users.
Can you use an if test to detect if the location is already occupied before changing it?it overwrites the array even if there is a car already in that specific location.
Please edit your post and wrap your code with
<YOUR CODE HERE>
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:
System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
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.