如何在二维数组实现的迷宫游戏中标记玩家已访问位置
迷宫路径标记功能实现方案
不需要额外新建完全相同的二维数组副本,直接在原有迷宫数组上做访问标记即可,只要选用和现有标识不冲突的字符区分已访问路径,逻辑简单且没有额外内存开销。
核心实现逻辑
你现有代码已经定义了三类基础地图标识:
0:不可通行的墙体1:可通行的未探索通路P:玩家当前所在位置
只需要新增一个不冲突的字符(示例中使用.)代表已访问过的路径,按以下规则调整代码即可:
- 每次判定移动合法后,先将玩家当前所在的旧坐标位置赋值为已访问标记,再更新玩家的行列坐标到新位置
- 原有打印逻辑不需要大改:优先判断当前坐标是否为玩家实时位置,是则输出
P,其余位置直接输出数组中存储的墙体、通路、已访问路径标识即可 - 可选规则调整:如果不允许玩家走回头路,就将移动合法性判断从「目标位置不等于
0」改为「目标位置等于1(仅未探索通路可走)」;如果允许走回头路,保留原有判断逻辑即可。
修改后完整代码
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; // 已访问路径标记字符,可自行修改为其他喜欢的符号 private static final char VISITED = '.'; 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, . represent paths you have visited, and P represents your current position. 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') { // 离开当前位置前标记为已访问 maze[playerRow][playerCol] = VISITED; 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') { maze[playerRow][playerCol] = VISITED; 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') { maze[playerRow][playerCol] = VISITED; 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') { maze[playerRow][playerCol] = VISITED; 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; } } input.close(); } 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(); } } }
如果你想要更换路径标记的样式,只需要修改常量
VISITED的值即可,比如换成*或者其他字符;如果要禁止走回头路,把四个方向判断里的!= '0'替换为== '1'即可实现。
内容的提问来源于stack exchange,提问作者Deezer Nutz
相关产品推荐
相关产品推荐

