Results 1 to 3 of 3
  1. #1
    MatthijsJava is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default Re: Rush hour: array overwriting

    Welcome!! Hey can you try using the coding tags. It really done help make the post readable.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Rush hour: array overwriting

    And when you've added [code] tags [/code] so that we can read your code, stick some debugging in the moveCar method so you can see what the value of everything is, and see what route through the code it is taking.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. 24 hour clock to 12 hour clock project.
    By bs3ac in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 10:10 AM
  2. File overwriting?
    By OllyHal in forum New To Java
    Replies: 1
    Last Post: 02-08-2012, 09:11 PM
  3. ascending order using array rush
    By jca in forum New To Java
    Replies: 2
    Last Post: 01-03-2011, 04:24 AM
  4. Array overwriting data
    By hobo in forum New To Java
    Replies: 7
    Last Post: 10-27-2010, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •