Results 1 to 3 of 3
Thread: Rush hour: array overwriting
- 01-25-2013, 03:05 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 1
- Rep Power
- 0
Rush hour: array overwriting
package Model;
import java.util.ArrayList;
public class Field {
private Car[][] field = new Car[6][6];
private int score = 0;
private boolean isGewonnen;
public Field(Car[][] field) {
this.field = field;
}
public Car[][] getField() {
return field;
}
public int getScore() {
return score;
}
public void moveCar(int carNumber, RichtingVerplaatsen richting) {
Car car = null;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {
if(field[i][j] != null) {
if(field[i][j].getCarNumber() == carNumber) {
car = field[i][j];
}
}
}
}
if(car != null) {
ArrayList<int[]> locations = new ArrayList<int[]>();
Richting richtingAuto = car.getRichting();
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {
if(car == field[i][j]) {
locations.add(new int[]{i, j});
}
}
}
if(richting == RichtingVerplaatsen.RECHTS && car.getRichting() == Richting.HORIZONTAAL && locations.get(0)[1] < 6 - car.getLengte() ) {
for (int[] location : locations) {
field[location[0]][location[1]] = null;
}
for (int[] location : locations) {
field[location[0]][location[1] + 1] = car;
}
score++;
}
When I'm testing the console application. The user gets asked what car he wants to move and in what direction. But when I move the car to a different location, it overwrites the array even if there is a car already in that specific location.
Someone please help.
- 01-29-2013, 06:13 AM #2
Member
- Join Date
- Dec 2012
- Location
- Des Moines, IA
- Posts
- 33
- Rep Power
- 0
Re: Rush hour: array overwriting
Welcome!! Hey can you try using the coding tags. It really done help make the post readable.
- 01-29-2013, 09:35 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,446
- Rep Power
- 16
Similar Threads
-
24 hour clock to 12 hour clock project.
By bs3ac in forum New To JavaReplies: 4Last Post: 01-08-2013, 10:10 AM -
File overwriting?
By OllyHal in forum New To JavaReplies: 1Last Post: 02-08-2012, 09:11 PM -
ascending order using array rush
By jca in forum New To JavaReplies: 2Last Post: 01-03-2011, 04:24 AM -
Array overwriting data
By hobo in forum New To JavaReplies: 7Last Post: 10-27-2010, 01:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks