简体   繁体   English

JAVA 文字迷宫游戏

[英]JAVA text based maze game

X signifies the starting point and E is the Exit. X 表示起点,E 表示出口。 The problem is I cannot make my X move.问题是我不能让我的 X 移动。 Can someone help me with this.有人可以帮我弄这个吗。 We are only allowed to used the main class and main method, no any other than that.我们只允许使用 main class 和 main 方法,除此之外别无他法。 Every time the user input, the maze map will display showing the current position of the player.每次用户输入时,迷宫 map 会显示玩家当前的 position。 When the user choose to face left or right, the current position should not change.当用户选择面向左或向右时,当前的 position 应该不会改变。

import java.util.Scanner;

public class G7 {
    public static void main(String[] args) {
        String[][] M = {{"0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"X", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "E", " ", "0", " "}};
        
        do {
            System.out.println("-------------------------MAZE------------------------------");
            System.out.println();
            for (String[] a : M) {
                for (String b : a) {
                    System.out.print(b);
                }
                System.out.println();
            }
            System.out.println("-----------------------------------------------------------");
            System.out.print("F - move Forward\tB - move Backward\t\nR - face Right\t\tL - face Left\n");
            System.out.print("Enter your move: ");
            String c = new Scanner(System.in).next();
            if (c.equals("F")) {
                M[1][2] = M[1][0];
                M[1][0] = " ";
            }

        } while (true);

    }
    
}

one cheap solution would be that you print the maze with the updated position after every keyboard input一种便宜的解决方案是在每次键盘输入后使用更新的 position 打印迷宫

Tasks:任务:

  • move M from the main method into a constant - static String[][] M =...;将 M 从 main 方法移动到常量 - static String[][] M =...;
  • setup loop that draws after keyboard input - static boolean isDone在键盘输入后绘制的设置循环 - static boolean isDone
  • create a method for drawing the maze static void printMaze()创建一个绘制迷宫的方法static void printMaze()
  • create a method for handling input static void readInput()创建处理输入的方法static void readInput()
  • handle the player position with variables - static int xPlayer & static int yPlayer使用变量处理播放器 position - static int xPlayer & static int yPlayer
static String[][] M = ...
static boolean isDone;
static int xPlayer;
static int yPlayer;

public static void main(String []args){
    //repeated drawing of the screen after input
    while(!isDone){
        printMaze();
        readInput();    
    }
}

static void printMaze(){
    int height = M.length;
    for(int y = 0; y < height; y++){
        int width = M[0].length;
        for(int x = 0; x < width; x++){
            System.out.print(M[x][y]);
            if(x == xPlayer && y == yPlayer){
                System.out.print("X");
            }
        }
        System.out.println();
    }
    System.out.println();
}

static void readInput(){
    System.out.print("F - move Forward\tB - move Backward\t\nR - face Right\t\tL - face Left\n");
    System.out.print("Enter your move: ");
    String c = new Scanner(System.in).next();

    //handle input
    if (c.equalsIgnore("f")){
        yPlayer = yPlayer - 1;
    }
    //TODO: handle the other 3 directions the same way

    //BONUS: quit the loop 
    if (c.equalsIgnore("quit)){
        isDone = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM