下面显示的代码是我正在创建的迷宫游戏的主要方法。在这个主要方法中,我有一个 while 循环,一直持续到游戏结束。在 while 循环中,我打印迷宫,然后询问用户他们想要移动角色的方向。我有一个疯狂的问题,提示用户询问密钥,不会显示。输出只是打印迷宫,然后等待用户输入。
do
{
maze.printArray();
//Asks the user which direction they wish to move THIS LINE NEVER SHOWS UP!
System.out.print ("Enter U,L,D,R to indicate move direction: ");
//Gets the string from user, and changes it to a char, then converts it to an uppercase
direction = read.next().charAt(0);
direction = Character.toUpperCase(direction);
//Moves the character and increments the invalidMoves counter if the move was not legal
if(maze.move(direction) == false)
invalidMoves++;
//Increment the move count regardless of its validity
moves++;
}
while(maze.isFinished() == false);
这是在打印之前调用的 printArray 方法。
public void printArray()
{
for (char [] x : mazeArray)
{
for (char y : x)
{
//Print the characters for the row
System.out.print (y);
}
//linebreak to go to next row
System.out.println ();
}
System.out.println ();
}
2D 数组包含一个迷宫图案,它会显示在屏幕上
XXXXXXXXXX
XO-------X
XX-XXX-XXX
XX---X---X
XXXX-X-X-X
X----X-XXX
X-XXXX--$X
XXXXXXXXXX