I'm working on a school assignment where I am supposed to create a maze from a textfile. My problem is creating the maze. I know it's probably an easy fix, but I am really stuck and need to get this done.. Any suggestions?
package application;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
public class Main extends Application {
LabyrintRute[][] Labyrint;
int X;
int Y;
int startx;
int starty;
Spiller spilleren;
int sizeX;
int sizeY;
@Override
public void start(Stage primaryStage) {
try {
GridPane root = new GridPane();
Spiller spilleren = new Spiller(startx, starty);
filLeser();
root.add(spilleren.getUtseende(), spilleren.getxPossisjon(), spilleren.getyPossisjon());
for(int x = 0; x<X; x++){
for(int y = 0; y<Y; y++){
root.add(Labyrint[x][y].getUtseende(), x, y);
}
}
Scene scene = new Scene(root, X*10, Y*10);
scene.setOnKeyPressed(new FilLytter(this));
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void filLeser() {
String teksten = "";
File fila;
int rad = 0;
FileChooser filvelger = new FileChooser();
filvelger.setTitle("Åpne en tekstfil");
filvelger.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
fila = filvelger.showOpenDialog(null);
try (Scanner filleser = new Scanner(fila)) {
X = filleser.nextInt();
Y = filleser.nextInt();
teksten = filleser.nextLine();
Labyrint = new LabyrintRute [X][Y];
while (filleser.hasNext()) {
teksten = filleser.nextLine();
for (int i = 0;i< X;i++) {
char tegn = teksten.charAt(i);
switch (tegn) {
case '#':
Labyrint[i][rad] = new Vegg(i, rad);
break;
case ' ':
Labyrint[i][rad] = new Gang(i, rad);
break;
case '-':
Labyrint[i][rad] = new Utgang(i, rad);
break;
case '*':
Labyrint[i][rad] = new Gang(i, rad);
startx = i;
starty = rad;
break;
}
rad++;
}
}
} catch (FileNotFoundException e) {
System.out.println("Kan ikke åpne fila!");
e.printStackTrace();
}
}
public void flyttSpiller(int deltax, int deltay) {
int nyx = spilleren.getxPossisjon() + deltax;
int nyy = spilleren.getyPossisjon() + deltay;
Labyrint[nyx][nyy].flyttHit(spilleren);
}
public static void main(String[] args) {
launch(args);
}
}
The "Vegg"(wall) class:
package application;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Vegg extends LabyrintRute {
private Node utseende;
public Vegg(int xKoordinat, int yKoordinat) {
super(xKoordinat, yKoordinat);
utseende = new Rectangle(10, 10, Color.MEDIUMPURPLE);
}
@Override
public void flyttHit(Spiller spilleren) {
//spilleren.setxPossisjon(getxKoordinat());
//spilleren.setyPossisjon(getyKoordinat());
}
@Override
public Node getUtseende() {
return utseende;
}
}
Abstract class:
package application;
import javafx.scene.Node;
public abstract class LabyrintRute {
private int xKoordinat;
private int yKoordinat;
public LabyrintRute(int xKoordinat, int yKoordinat) {
this.xKoordinat = xKoordinat;
this.yKoordinat = yKoordinat;
}
public int getxKoordinat() {
return xKoordinat;
}
public int getyKoordinat() {
return yKoordinat;
}
public abstract void flyttHit(Spiller spilleren);
public abstract Node getUtseende();
}
I also have a "Gang" (passage), and a "Utgang" (exit) class. They are similiar to the "Vegg" class.
Would really appreciate inputs! Sorry for the norwegian code..
When you read the file and create the lab, your variable rad
is incremented in a loop that goes to from 0
to X-1
. However, you use it to index the Y
dimension. If X > Y, you will run out of bounds there.
Since your code does not fully reveal the rules of the file and how the lab can be formed,I cannot provide a fix directly.
I think you most probably need some kind of nested loop. Maybe you should keep and increment i with the nextLine's and have the inner loop somehow like for (int rad = 0; rad < Y; rad++)
but again, it's impossible for me to say without further inside.