1

我正在编写一个看起来像这样的迷宫游戏程序:

import java.util.*;
import java.io.*;


public class MazeGame {

    public static void main(String[] args) throws Exception{

        //Display the maze
        char treasureMaze[][] = {{'P','.','X','X','.'},{'.','X','.','.','.'},{'.','.','.','X','.'},{'X','X','T','.','.'},{'.','.','X','.','.'}}; 
        display(treasureMaze);
        int vertical = 0;
        int horizontal = 0;


        //Give Move Options
        options();

        //Setup a while loop that continues until the user has gotten to the treasure, or 'P'
        while(treasureMaze[vertical][horizontal] != 'T'){
        //Get Users Decision
        Scanner moveChoice = new Scanner(System.in);
        int choice = moveChoice.nextInt();


        if(choice == 1){
            System.out.println("You chose to Move up");
        }
        else if(choice == 2){
            System.out.println("You chose to Move down");
        }
        else if(choice == 3){
            System.out.println("You chose to Move left");
        }
        else if(choice == 4){
            System.out.println("you chose to Move right");
        }
        else{
            return;
        }


        //Move the Player: Each choice will move the player according to their choice and then re-display the map and options
        //so that they can move through the maze
        //Move Up
        if(choice == 1){
            if(vertical - 1 < 0){
                System.out.println("\nCannot move there! Try something else\n");
                display(treasureMaze);
                options();
            }
            else if(treasureMaze[vertical - 1][horizontal] == '.'){
                treasureMaze[vertical - 1][horizontal] = 'P';
                treasureMaze[vertical][horizontal] = '.';
                vertical -= 1;
                display(treasureMaze);
                options();
            }
            else if(treasureMaze[vertical - 1][horizontal] == 'T'){
                System.out.println("\nCongratulations you won!");
                treasureMaze[vertical][horizontal] = 'T';
            }
            else{
                System.out.println("\nCannot move there! Try something else\n");
                display(treasureMaze);
                options();
            }
        }

        //Move Down
        else if(choice == 2){
            if(vertical + 1 < 0){
                System.out.println("\nCannot move there! Try something else\n");
                display(treasureMaze);
                options();
            }
            else if(treasureMaze[vertical + 1][horizontal] == '.'){
                treasureMaze[vertical + 1][horizontal] = 'P';
                treasureMaze[vertical][horizontal] = '.';
                vertical += 1;
                display(treasureMaze);
                options();
            }               
            else if(treasureMaze[vertical + 1][horizontal] == 'T'){
                System.out.println("\nCongratulations you won!");
                treasureMaze[vertical][horizontal] = 'T';
            }
            else{                   
                System.out.println("\nCannot move there! Try something else\n");
                display(treasureMaze);
                options();
                }
            }

        //Move Left
        else if(choice == 3){
            if(horizontal - 1 < 0){
                System.out.println("\nCannot move there! Try something else\n");
                display(treasureMaze);
                options();
            }
            else if(treasureMaze[vertical][horizontal - 1] == '.'){
                treasureMaze[vertical][horizontal - 1] = 'P';
                treasureMaze[vertical][horizontal] = '.';
                horizontal -= 1;
                display(treasureMaze);
                options();
            }
            else if(treasureMaze[vertical][horizontal - 1] == 'T'){
                System.out.println("\nCongratulations you won!");
                treasureMaze[vertical][horizontal] = 'T';
            }
            else{
                System.out.println("\nCannot move there! Try something else\n");
                display(treasureMaze);
                options();
            }
        }

        //Move Right
        else if(choice == 4){
            if(horizontal + 1 < 0){
                System.out.println("\nCannot move there! Try something else\n");
                display(treasureMaze);
                options();
            }
            else if(treasureMaze[vertical][horizontal + 1] == '.'){
                treasureMaze[vertical][horizontal + 1] = 'P';
                treasureMaze[vertical][horizontal] = '.';
                horizontal += 1;
                display(treasureMaze);
                options();
            }
            else if(treasureMaze[vertical][horizontal + 1] == 'T'){
                System.out.println("\nCongratulations you won!");
                treasureMaze[vertical][horizontal] = 'T';
            }
            else{
                System.out.println("\nCannot move there! Try something else\n");
                display(treasureMaze);
                options();
            }
        }
        else{
            return;
        }

    }
    }




    //Display Object: prints out the maze for the user
    public static void display(char x[][]){
        for(int row = 0; row < x.length; row++){
            for(int column = 0; column < x[row].length; column++){
                System.out.print(x[row][column] + "\t");
            }
            System.out.println();
        }
    }

    //Options Object: gives the options menu to the user
     static void options(){
         System.out.println("You may:");
            System.out.println("\t1) Move up");
            System.out.println("\t2) Move down");
            System.out.println("\t3) Move left");
            System.out.println("\t4) Move right");
            System.out.println("\t0) Quit");

    }
}

当我给它的藏宝迷宫数组时,这个程序运行良好,但我希望能够从文本文件中读取不同的迷宫,但我不知道如何去做。我是否需要使用扫描仪将数组读取到变量或其他内容?提前感谢您的建议。

4

1 回答 1

2

这应该只涉及从 a 读取行FileReader并将它们转换为 achar[]

// Open "filename.txt" for reading
BufferedReader in = new BufferedReader(new FileReader("filename.txt"));

// Get a single line from the file (you can call this repeatedly for multiple lines)
String line = in.readLine();

// Convert that string to a character array
char[] array = line.toCharArray();

我不推荐使用Scanner,因为它很慢而且毫无意义的复杂。我将它包装FileReader在 a 中是BufferedReader因为它更快一点,并且它提供了有用的readLine()方法,但如果你愿意,你可以直接从 a 中读取FileReader

Scanner.nextLine()如果您对 感到不舒服,您也可以使用BufferedReader,但我建议您熟悉各种Reader

由于 2D 数组只是一个数组数组,因此您应该能够将 2D 数组的每个索引设置为您从文件中读取的数组:

char[][] map = new char[][10];
for(i = 0; i < 10; ++i) {
    String line = in.readLine();
    if(line == null) {
        // Realistically, you'd want better error handling than this
        System.out.println("Uh oh, the file doesn't have 10 lines!");
        return;
    }
    map[i] = line.toCharArray();
}
于 2012-09-25T20:36:36.193 回答