RushHour

Kopki

Mitglied
Hallo,

ich muss das Spiel RushHour Programmieren. Für die, die es nicht kennen: man muss auf einem 6x6 Feld Autos verschieben und so ein Auto an die rechte Seite des Spielfelds bewegen.
Ich bekomme die Autos momentan aber nicht in mein Array platziert. Es kommt ein Exception Error. Kann mir da jemand helfen und einmal sagen was ich falsch mache?

Java:
lic class RushHour {

    private static int[][] Spielfeld;

    public static void main(String[] args) {
   
        int rows = 6;
        int columns = 6;
               
        int[][] Spielfeld = new int[rows][columns];
   
    Auto(0,2,1,3,1);
    Auto(2,3,2,2,0);
    Auto(2,5,3,3,1);
    Auto(3,1,4,2,0);
    Auto(3,3,5,3,1);
    Auto(4,1,6,2,1);
    Auto(5,4,7,2,0);
   
    for(int x = 0; x<rows; x++)
        for(int y = 0; y<columns; y++)
            Spielfeld[x][y] = 0;

    for(int x = 0; x<rows; x++)
    {
        for(int y = 0; y<columns; y++)
        {
            System.out.print(Spielfeld[x][y]);
        }
        System.out.println();
    }
    }
   
    private static void Auto(int pos_x, int pos_y, int nummer, int laenge, int richtung) {
            if (richtung == 0) {
                for (int i = pos_y; i < pos_y+laenge; i++)
                    Spielfeld[pos_x][i] = nummer;           
            }
            else {
                for (int i = pos_x; i < pos_x+laenge; i++)
                    Spielfeld[i][pos_y] = nummer;
            }
        }
   
}

Code:
public class Auto {
        int pos_x;
        int pos_y;
        int laenge;
        char nummer;
        int richtung;
       
        Auto(int pos_x, int pos_y, char nummer, int laenge, int richtung){
        this.pos_x = pos_x;
        this.pos_y = pos_y;
        this.laenge = laenge;
        this.nummer = nummer;
        this.richtung = richtung;
    }
    public void setLaenge() {
        this.laenge = laenge;
    }
    public double getLaenge() {
        return laenge;
    }
    public void setNummer() {
        this.nummer = nummer;
    }
    public int getNummer() {
        return nummer;
    }
    public void setRichtung() {
        this.richtung = richtung;
    }
    public int getRichtung() {
        return richtung;
    }
    public void setX() {
        this.pos_x = pos_x;
    }
    public int getX() {
        return pos_x;
    }
    public void setY() {
        this.pos_y = pos_y;
    }
    public int getY() {
        return pos_y;
    }
}
 

krgewb

Top Contributor
Es wird eine NullPointerException geworfen.
Java:
Exception in thread "main" java.lang.NullPointerException
    at RushHour.Auto(RushHour.java:39)
    at RushHour.main(RushHour.java:13)

Zeile 13:
Java:
Auto(0, 2, 1, 3, 1);

Zeile 39:
Java:
Spielfeld[i][pos_y] = nummer;
 

Neue Themen


Oben