以下に示すコードは、私が作成している迷路ゲームの主な方法です。このメインの方法では、ゲームが終了するまで続く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