基于JAVA文字的迷宫游戏 [英] JAVA text based maze game

查看:67
本文介绍了基于JAVA文字的迷宫游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

X表示起点,E表示出口.问题是我无法移动X.有人可以帮我弄这个吗.我们只允许使用main类和main方法,除此之外别无其他.每次用户输入时,都会显示迷宫地图,显示玩家的当前位置.当用户选择向左或向右时,当前位置不应更改.

X signifies the starting point and E is the Exit. The problem is I cannot make my X move. Can someone help me with this. We are only allowed to used the main class and main method, no any other than that. Every time the user input, the maze map will display showing the current position of the player. When the user choose to face left or right, the current position should not change.

import java.util.*;
public class g7 {
    public static void main(String []args) throws Exception {
        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

任务:

  • 将M从主要方法移至常数-static String[][] M = ...;
  • 在键盘输入后绘制的设置循环-static boolean isDone
  • 创建一种绘制迷宫的方法static void printMaze()
  • 创建用于处理输入static void readInput()
  • 的方法
  • 使用变量-static int xPlayer& static int yPlayer
  • move M from the main method into a constant - static String[][] M = ...;
  • setup loop that draws after keyboard input - static boolean isDone
  • create a method for drawing the maze static void printMaze()
  • create a method for handling input static void readInput()
  • handle the player position with variables - 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;
    }
}

这篇关于基于JAVA文字的迷宫游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆