通过读取文件创建房间(用于游戏)

Creating a room by reading from a file (for a game)

基本上我必须制作一个游戏,我需要通过读取文件来创建房间,这个:

#
# 0 D room0.txt 0
# 1 E room1.txt 0
#
WWWW0WWWWW
W        W
W        W
W        W
W        W
W      W W
W      W W
W      W W
W      W 1
WWWWWWWWWW

“#”表示我可以跳过这些行(它们仅供参考),"W" 是我需要放墙的地方,数字是门和空白空间是地板。

我认为创建房间的最佳方法是创建一个接收文件并读取文件并将其 "information" 放入字符串 [10][10] 的方法,然后创建另一个方法(或者只是在我的 Main 中执行此操作)接收创建的 String[10][10] 并创建房间(将图像添加到房间),但我在读取文件时遇到一些困难所以如果你们可以帮助我我将不胜感激。

当我 运行 程序在这一行时出现的错误:

if(r[x][y].等于("W"))

如果你们需要另一个 class,游戏应该是什么样子的图像或我忘记展示的其他东西,请告诉我并感谢您的帮助。

public void generateRoom(File file){     
    if(file.exists()){
        Scanner sc = null;     
        String[][] r = new String[10][];
        int row = 0;
        try {
            sc = new Scanner(file); 
            String line = sc.nextLine(); 
            while(sc.hasNextLine() && row < 10){
                if(line.startsWith("#"))
                    sc.nextLine();
                else{
                    String[] s0 = line.split("");
                    if(s0.length==10){
                        r[row]=s0;

                    row++;
                }         
            }
        }
        for(int x = 0; x < 10; x++){    
         for(int y = 0; y < 10; y++){
             if(r[x][y].equals("W"))
                   tiles.add(new Wall(new Position(x,y)));      
             if(r[x][y].equals("1") || r[x][y].equals("2"))
                   tiles.add(new Door(new Position(x,y)));
             if(r[x][y].equals("0")){
                 tiles.add(new Door(new Position(x,y)));
                 hero.setPosition(new Position(x,y));
                 tiles.add(hero);
             }
             else tiles.add(new Floor(new Position(x,y)));
             gui.newImages(tiles);
             gui.update();
         }
        }
        }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. ");          

}

您永远不会重新分配 line

这个:

if(line.startsWith("#"))
    sc.nextLine();

应该阅读

if(line.startsWith("#"))
    line = sc.nextLine();

但是你的程序太复杂了。您可以一次性读取文件并生成房间。考虑以下版本的程序:

public static void generateRoom(File file) throws FileNotFoundException {
    if (file.exists()) {
        Scanner sc = new Scanner(file);
        int row = 0;
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            if (line.startsWith("#"))
                continue;
            for (int col = 0; col < line.length(); col++) {
                switch (line.charAt(col)) {
                case 'W':
                    System.out.println("new wall @" + row + '/' + col);
                    break;
                case '0':
                    System.out.println("hero @" + row + '/' + col);
                    // fall-through
                case '1':
                case '2':
                    System.out.println("new door @" + row + '/' + col);
                    break;
                case ' ':
                    System.out.println("new floor tile @" + row + '/' + col);
                    break;
                default:
                    System.out.println("invalid char @" + row + '/' + col);
                }
            }
            row++;
        }
        sc.close();
    } else
        System.out.println("Ficheiro " + file.getAbsolutePath() + " nã£o existe.");
}

它完全避免了临时字符串数组。此外,您可以使用 switch 语句对文件中的字符进行解码。

String line;

while (sc.hasNextLine() && row < 10) {
    line = sc.nextLine();
    if (line.startsWith("#")) {
        line = sc.nextLine();
    } else {
        String[] s0 = line.split("");       
        if (s0.length == 10) {
            r[row] = s0;
            row++;
        }
    }}

没有前进线。