a
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
a
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
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.
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));
If you don't understand my answer, don't ignore it, ask a question.
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.
If you don't understand my answer, don't ignore it, ask a question.
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.
If you don't understand my answer, don't ignore it, ask a question.