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

时间:2012-12-19 10:27:16

标签: java multithreading

我必须使用多线程制作一个简单的基于文本的游戏。我选择猜测我的动物游戏。服务器将挑选一个随机动物并发出线索,客户必须猜测动物在三条线索中的含义。

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

另一个问题是,当客户说 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;
    }
}

如果您需要查看其他课程,请询问。

1 个答案:

答案 0 :(得分:0)

调试程序并在第一个if子句之前设置断点。变量怎么样?我想你在代码中的某处发生了一个微不足道的错误,它会在观察程序实际执行的操作时显示出来。

您还可以在此粘贴一些客户端代码,以便有人能够了解正在发生的事情。从评论我明白,没有人知道你如何使用Protocol类。