如何从 java 中的文本文件中 load/create level/room?

How to load/create a level/room from a text file in java?

基本上每个房间的大小都是 10 x 10,"W" 代表墙壁,空格 -" " 代表地板,数字是 Doors.I 最好的计算方式创建房间是创建一个接收文件并读取它并将其 "information" 放入 String[10][10] 的方法,然后创建另一个方法(或者只是在我的 Main 中执行)接收String[10][10] 创建并创建了房间(将图像添加到房间),但我在读取文件时遇到了一些困难,所以如果你们能帮助我解决这部分问题,我将不胜感激。 这是我要用来创建我的房间的文本文件类型:

    WWWW0WWWWW
    W        W
    W        W
    W        W
    W        W
    W       WW
    W       WW
    W       WW
    W       W1
    WWWWWWWWWW

这是门、墙和地板 类:

public class Door implements ImageTile {


 private Position position;

public Door(Position position) {
    this.position = position;
}

@Override
public String getName() {
    return "Door";
}

@Override
public Position getPosition() {
    return position;
}

}

 public class Floor implements ImageTile {


private Position position;

public Floor(Position position) {
    this.position = position;
}

@Override
public String getName() {
    return "Floor";
}

@Override
public Position getPosition() {
    return position;
}

}

public class Wall implements ImageTile {


private Position position;

public Wall(Position position) {
    this.position = position;
}

@Override
public String getName() {
    return "Wall";
}

@Override
public Position getPosition() {
    return position;
}

}

这是我将图像添加到相框的方法:

    public void newImages(final List<ImageTile> newImages) {
    if (newImages == null)
        return;
    if (newImages.size() == 0)
        return;
    for (ImageTile i : newImages) {
        if (!imageDB.containsKey(i.getName())) {
            throw new IllegalArgumentException("No such image in DB " + i.getName());
        }
    }
    images.addAll(newImages);
    frame.repaint();
}

如果你们能帮助我,我将不胜感激,谢谢 guys.Her 我现在拥有的是:

     public class Room {
       private String[][]room;

       public Room(){
        room = new String[10][10]; }

     public static Room fromFile(File file){     
    if(file.exists()){
        Scanner sc = null;     
        Room room = new Room();
        try {
            sc = new Scanner(file);
            while(sc.hasNextLine()){
                if(sc.nextLine().startsWith("#"))
                    sc.nextLine();
                else{  
                    String[] s0 = sc.nextLine().split("");
                 //Here is where my trouble is, i dont know how to add the content of this String s0 to the String s
                   if(s0.length==10){
                       for(int x = 0; x < 10; x++){
                                for(int y = 0; y < 10; y++){
                                    String[x][y] s= String[x] s0;
                }         
            }
        } catch (FileNotFoundException e) {
            System.out.println("Ficheiro "+file.getAbsolutePath()+
                    " não existe. ");           }
        finally{
            if(sc!=null)sc.close();
        }
    }
    else
        System.out.println("Ficheiro "+file.getAbsolutePath()+
                " não existe. ");       
    return null;        
}

每次调用 sc.nextLine() 都会从您的文件中读取另一行。您需要将该行保存到一个临时变量中,然后引用该临时变量。

维护已处理行的计数器,以便您可以在 s[][] 矩阵中填写适当的行。

您只需为 10 行分配存储空间,因为拆分您读入的行会导致为您分配一个字符串数组(一行中的列)。

int row = 0;
String[][] s = new String[10][];
while(sc.hasNextLine() && i < 10) {
    String line = sc.nextLine();
    if( !line.startsWith("#")) {
        String[] s0 = sc.line.split("");
        s[row] = s0;
        row++;
    }         
}

顺便说一句,使用 String[][] 太过分了;因为每个字符串只会包含一个字符。也许您可以考虑使用 char[][]line.toCharArray() 会为您将 line 拆分为 char[]


编辑:(由于编辑了您的问题代码)

而不是:

if(s0.length==10){
    for(int x = 0; x < 10; x++){
        for(int y = 0; y < 10; y++){
            String[x][y] s= String[x] s0;
        }         
    }
}

你想要:

if(s0.length==10){
    for(int y = 0; y < 10; y++){
        s[row][y] = s0[y];
    }
    row++;
}

row 是我上面代码中的行计数器。