我必须使用多线程制作一个简单的基于文本的游戏。我选择猜测我的动物游戏。服务器将挑选一个随机的动物并给出线索,而客户必须猜测三个线索中的动物是什么。
但是,如果猜测动物正确,程序将转到下一个线索。我不明白我哪里出了问题?
另一个问题是,当客户对新游戏说“ y”时,它只会重复同一只动物。它不会改变。
我知道这只是我需要修复的协议类。请帮忙!我对这个程序感到沮丧。
这是我的协议类的副本:
public class KKProtocol {
private static final int WAITING = 0;
private static final int ASKNAME = 1;
private static final int SENTCLUE = 2;
private static final int SENTCLUE2 = 3;
private static final int SENTCLUE3 = 4;
private static final int ANOTHER = 5;
private static final int NUMANIMALS = 4;
private int state = WAITING;
private int currentAnimal = (int) (Math.random() * 6); // number of first joke
private String[] clues = {"I like to play", "I like to scratch", "I eat salad", "I annoy you in the morning"};
private String[] clues2 = {"Love walks", "House pet", "garden pet", "I fly everywhere"};
private String[] clues3 = {"Woof", "Meow", "I live in a hutch", "Tweet Tweet"};
private String[] answers = {"Dog",
"Cat",
"Rabbit",
"Bird",};
private String[] name = {};
public String processInput(String theInput) {
String theOutput = null;
// System.out.println("Welcome to my animal guessing game");
if (state == WAITING) {
theOutput = clues[currentAnimal];
state = SENTCLUE;
} else if (state == SENTCLUE) {
if (theInput.equals(answers[currentAnimal])) {
theOutput = "Correct...Your Score is 1....Want to play again? (y/n)";
state = ANOTHER;
} else {
theOutput = clues2[currentAnimal];
state = SENTCLUE2;
}
} else if (state == SENTCLUE2) {
if (theInput.equals(answers[currentAnimal])) {
theOutput = "Correct...Your Score is 2....Want to play again? (y/n)";
state = ANOTHER;
} else {
theOutput = clues3[currentAnimal];
state = SENTCLUE3;
}
} else if (state == SENTCLUE3) {
if (theInput.equals(answers[currentAnimal])) {
theOutput = "Correct...Your Score is 3....Want to play again? (y/n)";
state = ANOTHER;
} else {
theOutput = ("it's" + answers[currentAnimal] + " you fool! Want to play again? (y/n)");
state = ANOTHER;
}
} else if (state == ANOTHER) {
if (theInput.equalsIgnoreCase("y")) {
if (currentAnimal == (NUMANIMALS - 1)) {
currentAnimal = 0;
}
theOutput = clues[currentAnimal];
// else
currentAnimal++;
state = SENTCLUE;
} else {
theOutput = "Bye.";
state = WAITING;
}
}
return theOutput;
}
}
如果您需要查看其他课程,请询问。
最佳答案
调试程序并在第一个if子句之前设置一个断点。变量看起来如何?我想您在代码中的某个地方犯了一个小错误,该错误会在观察程序实际运行时显示出来。
您还可以在此处粘贴一些客户端代码,以便其他人可以了解正在发生的事情。从评论中我了解到,没有人知道如何使用Protocol类。
关于java - 客户端/服务器通信-猜我的动物游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13950243/