作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须使用多线程制作一个简单的基于文本的游戏。我选择玩猜动物游戏。服务器将随机选择一种动物并给出线索,客户端必须在三个线索内猜测该动物是什么。
但是,如果动物猜对了,程序就会转到下一条线索。我不明白我哪里错了?
另一个问题是,当客户对新游戏说y时,它只是重复相同的动物。它不会改变。
我知道这只是我需要修复的协议(protocol)类。请帮忙!我因这个计划而沮丧地哭泣。
这是我的协议(protocol)类的副本:
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;
}
}
如果您需要查看其他类(class),请直接询问。
最佳答案
调试程序并在第一个 if 子句之前设置断点。变量是什么样的?我想您在代码中的某个地方犯了一个小错误,在观察您的程序实际执行的操作时,该错误会暴露出来。
您还可以在此处粘贴一些客户端代码,以便其他人可以了解发生了什么。从评论中我了解到没有人完全了解如何使用 Protocol 类。
关于java - 客户端/服务器通信 - 猜我的动物游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13950243/
我正在用 python 编写一个猜词游戏。这是我的学校项目。我快完成了,我只是有一件事有问题。我不知道如何掩盖一个词。例如,如果单词是 monkey,程序应该显示 ------ 并且当用户猜一个字母时
我是一名优秀的程序员,十分优秀!