1

I have to make a simple text based game using multi threading. I have chosen to do a guess my animal game. A server will pick a random animal and give out clues and the client has to guess what the animal is within three clues.

However, if the animal is guessed correct, the program just goes to the next clue. I dont understand where i have gone wrong?

The other problem is when the client says y to a new game, it just repeats the same animal. It won't change.

I know it is just the protocol class i need to fix. Please help! I have cried with frustration over this program.

Here is a copy of my protocol class:

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 you need to see the other classes please just ask.

4

1 回答 1

0

调试程序并在第一个 if 子句之前设置一个断点。变量看起来如何?我想你在代码的某个地方犯了一个微不足道的错误,当你观察你的程序实际做了什么时,它就会暴露出来。

您还可以在此处粘贴一些客户端代码,以便有人可以了解正在发生的事情。从评论中我了解到,没有人完全了解您如何使用 Protocol 类。

于 2012-12-19T20:16:23.633 回答