Eclipse控制台迷宫游戏玩家移动时数组索引越界问题求助
解决迷宫游戏的索引越界问题
嘿,我来帮你搞定这个索引越界的问题!先拆解下你代码里的核心问题,再一步步给你修复方案:
核心问题分析
- 未正确定位玩家初始位置:你直接把
vxTemp和vyTemp设为0,但如果玩家"P"位置变动(虽然这次初始是(0,0),但逻辑不严谨),后续移动会用错误坐标;而且移动前完全没检查目标坐标是否在迷宫范围内——比如向上移动时直接用vyTemp-1,如果当前y是0,就会得到-1,直接触发越界。 - 迷宫数组语法错误:第四行
{"!","".","T","."},多了一个引号,应该是{"!","!",".","T","."},不然编译都过不了。 - 移动逻辑混乱:嵌套的for循环完全多余,
mazeStructure[vxTemp][vyTemp] = mazeStructure[i][k-1];这行更是错误覆盖玩家位置;同时用==比较字符串,Java里必须用equals()才对。 - 无效的边界检查:
choice == 1 && a >= 0 && a < columnsTotal里的a是刚声明的0,这个条件毫无意义——向上移动要检查的是行坐标减1后是否≥0,不是列的范围。
修复后的完整代码
import java.util.Scanner; public class MazeGame { public static void main(String[] args) { Scanner input = new Scanner(System.in); int rowsTotal = 5; int columnsTotal = 5; int playerX = 0; // 玩家当前行坐标 int playerY = 0; // 玩家当前列坐标 String[][] mazeStructure = { {"P", ".", ".", "!", "."}, {".", ".", ".", ".", "."}, {".", ".", ".", "!", "."}, {"!", "!", ".", "T", "."}, // 修复语法错误 {"!", ".", "!", "!", "."}, }; // 先找到玩家初始位置(如果初始位置不固定,这步非常重要) for (int i = 0; i < rowsTotal; i++) { for (int j = 0; j < columnsTotal; j++) { if (mazeStructure[i][j].equals("P")) { playerX = i; playerY = j; break; } } } boolean won = false; while (!won) { // 打印迷宫 for (int a = 0; a < rowsTotal; a++) { for (int b = 0; b < columnsTotal; b++) { System.out.print(mazeStructure[a][b] + " "); } System.out.println(); } // 打印操作菜单 System.out.println("\nPlease select one of the following:"); System.out.println("Press 1 to move up."); System.out.println("Press 2 to move down."); System.out.println("Press 3 to move left."); System.out.println("Press 4 to move right."); System.out.println("Press 0 to stop playing the game."); int choice = input.nextInt(); boolean moveAllowed = true; switch (choice) { case 1: // 向上移动:行坐标减1 if (playerX - 1 >= 0) { // 先检查是否越界 if (mazeStructure[playerX - 1][playerY].equals("!")) { moveAllowed = false; } else { // 更新玩家位置 mazeStructure[playerX][playerY] = "."; playerX--; mazeStructure[playerX][playerY] = "P"; // 检查是否到达终点 if (mazeStructure[playerX][playerY].equals("T")) { won = true; System.out.println("You won!"); } } } else { moveAllowed = false; } break; case 2: // 向下移动:行坐标加1 if (playerX + 1 < rowsTotal) { if (mazeStructure[playerX + 1][playerY].equals("!")) { moveAllowed = false; } else { mazeStructure[playerX][playerY] = "."; playerX++; mazeStructure[playerX][playerY] = "P"; if (mazeStructure[playerX][playerY].equals("T")) { won = true; System.out.println("You won!"); } } } else { moveAllowed = false; } break; case 3: // 向左移动:列坐标减1 if (playerY - 1 >= 0) { if (mazeStructure[playerX][playerY - 1].equals("!")) { moveAllowed = false; } else { mazeStructure[playerX][playerY] = "."; playerY--; mazeStructure[playerX][playerY] = "P"; if (mazeStructure[playerX][playerY].equals("T")) { won = true; System.out.println("You won!"); } } } else { moveAllowed = false; } break; case 4: // 向右移动:列坐标加1 if (playerY + 1 < columnsTotal) { if (mazeStructure[playerX][playerY + 1].equals("!")) { moveAllowed = false; } else { mazeStructure[playerX][playerY] = "."; playerY++; mazeStructure[playerX][playerY] = "P"; if (mazeStructure[playerX][playerY].equals("T")) { won = true; System.out.println("You won!"); } } } else { moveAllowed = false; } break; case 0: // 退出游戏 System.out.println("Game stopped."); input.close(); return; default: System.out.println("Invalid choice, please try again."); moveAllowed = true; // 不提示移动失败 } if (!moveAllowed) { System.out.println("Move isn't allowed."); } } input.close(); } }
关键修复点说明
- 正确定位玩家:添加初始化循环找到"P"的坐标,确保后续移动用的是真实玩家位置。
- 先检查边界再操作:每次移动前先判断目标坐标是否在0-4的合法范围内,彻底避免越界。
- 简化移动逻辑:用switch-case替代混乱的if判断,每次移动只更新玩家坐标和迷宫数组,去掉多余的嵌套循环。
- 字符串比较修正:所有字符串比较用
equals()替代==,避免Java字符串引用比较的坑。 - 添加胜利判断:移动后检查是否到达终点"T",触发胜利条件。
这样修改后,你就不会再遇到索引越界的问题,移动逻辑也清晰可靠啦!
内容的提问来源于stack exchange,提问作者jackpaster




