gpt4 book ai didi

java - 客户端/服务器通信 - 猜我的动物游戏

转载 作者:行者123 更新时间:2023-12-01 04:56:58 25 4
gpt4 key购买 nike

我必须使用多线程制作一个简单的基于文本的游戏。我选择玩猜动物游戏。服务器将随机选择一种动物并给出线索,客户端必须在三个线索内猜测该动物是什么。

但是,如果动物猜对了,程序就会转到下一条线索。我不明白我哪里错了?

另一个问题是,当客户对新游戏说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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com