ユーザーがコマンドラインで終了するまでプレイするゲームを作成しています。
ユーザーは、get や go などのさまざまなコマンドを入力できます。get コマンドを使用すると、ユーザーは何を取得するか、get 野球のバットを取得することができます。私のコードで行うことは、コマンドを分割することです。
すべて正常に動作していますが、解決できないバグが見つかりました。「get」と入力して押してから+を押すspaceと、終了しない while ループに入ります。ctrlz
ctrl+でのみ発生しますz(1 回で発生しctrl cますが、その後は 1 回ではなくなります)
private void run()
{
while (! quitCommand)
{
String input = null;
try
{
input = null;
System.out.println("Input "+ input);
System.out.println("Give a command.");
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
input = is.readLine();
handleCommand(input);
// As long as the command isn’t to quit:
// get the next input line and handle it. (With handleCommand.)
}
catch (Exception e)
{
System.out.println("Something went wrong we are sorry try again.");
e.printStackTrace();
}
}
}
/**
* @param userInput (This is the entire input string from the user.)
*
* (Tell others to) Perform the task which belongs to the given
* command.
*/
private void handleCommand(String userInput)
{
// Split the user input string.
if (userInput != null) // user input can not be empty
{
String[] delenTekst = userInput.split(" ");
// The first word is a command. The rest is extra information
String command = delenTekst[0];
String extra = "";
for (int i = 1; i < delenTekst.length; i ++)
{
if (i == 1)
{
extra = extra + delenTekst[i];
}
else
{
extra = extra +" " + delenTekst[i];
}
}
switch (command)
{
// Check if the command is to travel between rooms. If so, handle
case "go"
:
this.checkRoomTravel(extra);
break;
// If there isn't any room travel, then check all other command
case "get"
:
System.out.println("Looking for " +extra );
this.handleGetCommand(extra);
break;
case "quit"
:
quitCommand = true;
break;
default
:
System.out.println("Command is not known try help for information");
break;
}
}
else
{
userInput = "help";
}
}
私はJavaが初めてなので、本当に簡単なものになる可能性があります。
quitCommand = false;
スクリプトの一番上に、ユーザーが終了を入力したかどうかを確認するためのプライベート ブール値があります。