java - 如何标记玩家在迷宫游戏(二维数组)中去过的每个位置?

标签 java arrays multidimensional-array maze

我正在制作一个迷宫游戏,它使用 2D 数组来表示游戏板。目前,无论玩家当前位置在哪里,控制台都会输出“P”。但是,我想更改玩家“访问过”的数组中的每个元素并显示它以显示他们的路径。我该怎么做?我是否需要创建第二个重复数组以便对其进行更改?

这是我的代码:

import java.util.Scanner;

public class MazeGame {

private static char[][] maze = {

{'1','1','1','0','1','1','0','0','0','1','1','1','1'},

{'1','0','1','1','1','0','1','1','1','1','0','0','1'},

{'0','0','0','0','1','0','1','0','1','0','1','0','0'},

{'1','1','1','1','1','1','1','0','1','0','1','1','1'},

{'1','0','0','0','0','0','1','1','1','1','0','0','1'},

{'1','1','1','1','1','1','1','0','1','1','1','1','1'},

{'1','0','1','0','0','0','0','1','0','0','0','0','1'},

{'1','1','1','1','1','1','1','1','1','1','1','1','1'}

};

  private static int playerRow = 0, playerCol = 0;

  private static int moves = 0;

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    printMaze();
    System.out.println("Welcome to the maze! Your goal is to make it from your starting position, the top left corner of the maze, to the end position, the bottom right corner of the maze. 1s represent spaces that you can move to, 0s represent walls that you can not move to, and Ps represent places that you have already been. To begin, enter an input: \n1-- 'U' for up\n2-- 'D' for down\n3-- 'L' for left\n4-- 'R' for right\n5-- 'Q' for quit\nHave fun playing the maze game and best of luck to you :)");

    while (true) {

      System.out.print("\nEnter a move (U/D/L/R/Q): ");

      String move = input.nextLine();

      if (move.charAt(0) == 'Q' || move.charAt(0) == 'q') {

        System.out.println("Thanks for playing and have a wonderful day!");
        System.exit(0);

      }

      if (move.length() == 1) {

        if (move.charAt(0) == 'U' || move.charAt(0) == 'u') {

          if (playerRow > 0 && maze[playerRow - 1][playerCol] != '0') {

            playerRow--;
            moves++;

          } else {

            System.out.println("Invalid move.");

          }

        } else if (move.charAt(0) == 'D' || move.charAt(0) == 'd') {

          if (playerRow < maze.length - 1 && maze[playerRow + 1][playerCol] != '0') {

            playerRow++;
            moves++;

          } else {

            System.out.println("Invalid move. Please enter a valid move from the list below: \n1-- 'U' for up\n2-- 'D' for down\n3-- 'L' for left\n4-- 'R' for right\n5-- 'Q' for quit");

          }

        } else if (move.charAt(0) == 'L' || move.charAt(0) == 'l') {

          if (playerCol > 0 && maze[playerRow][playerCol - 1] != '0') {

            playerCol--;
            moves++;

          } else {

            System.out.println("Invalid move.");

          }

        } else if (move.charAt(0) == 'R' || move.charAt(0) == 'r') {

          if (playerCol < maze[0].length - 1 && maze[playerRow][playerCol + 1] != '0') {

            playerCol++;
            moves++;

          } else {

            System.out.println("Invalid move.");

          }

        } else {

          System.out.println("Invalid move.");

        }

      } else {

        System.out.println("Invalid move.");

      }

      printMaze();

      if (playerRow == maze.length - 1 && playerCol == maze[0].length - 1) {

        System.out.println("\nCongratulations, you have won the game in " + moves + " moves!");

        break;

      }

    }

  }

  private static void printMaze() {

    for (int i = 0; i < maze.length; i++) {

      for (int j = 0; j < maze[0].length; j++) {

        if (i == playerRow && j == playerCol) {

          System.out.print("P ");

        } else {

          System.out.print(maze[i][j] + " ");

        }

      }

      System.out.println();

    }
  }
}

最佳答案

就您而言,您可以在将玩家移动到其他部分之前立即在玩家所在的位置写入P。由于您的条件是 maze[playerRow][playerCol + 1] != '0' ,如果有 1P 并不重要,它仍然是可访问的字段。

        maze[playerRow][playerCol] = 'P'
        playerCol++;
        moves++;

关于java - 如何标记玩家在迷宫游戏(二维数组)中去过的每个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72527727/

相关文章:

尝试创建新对象并将其添加到 Arraylist 时出现 Java JPA LazyInitializationException

java - 向字符串添加转义字符的简单方法

java - Java 中文件下载的文件夹浏览对话框

c - 将函数中的 token (strtok)存储在主数组中

c - 动态数组中的垃圾值

python - Numpy 1-dim 数组与 2-dim 数组,其中一个维度的长度为 1

c++ - 如何在程序中改变多维数组的大小?

java - Android HashSet 无法转换为 LinkedHashSet

javascript - 在数组中查找连续的匹配位置

Java - 如何解决这个二维数组沙漏?