我正在尝试制作一个猜谜游戏,每次猜出后计算机都会不断变得更聪明。这是一个示例运行:
(电脑在猜你在想什么动物)
Computer: Does the animal you're thinking of have legs? Player: Yes Computer: Is it a dog? Player: Yes Computer: I win! Do you want to play again? Player: Yes Computer: Does the animal you're thinking of have legs Player: Yes Computer: Is it a dog? Player: No Computer: I give up. What was your animal? Player: Horse Computer: Type a question which the answer is yes for Dog but No for Horse Player: Does it live in a house? Computer: Do you want to play again? Player: Yes Computer: Does the animal you're thinking of have legs? Player: Yes Computer: Does it live in a house? Player: No Computer: Is it a horse? Player: No Computer: I give up
等等。

到目前为止,这是我的代码:

import java.util.*;

public class DoesItHaveLegs {
public static void main(String[] args) {
    ArrayList<String> questions = new ArrayList<String>();
    ArrayList<String> animals = new ArrayList<String>();
    Scanner input = new Scanner(System.in);

    questions.add("Does it have legs");
    animals.add("dog");

    String userAnimal;
    String userQuestion = "";

    String giveUp = "I give up. What animal was it?";

    String userAnswer = "YES";

    while(userAnswer.equals("YES")) {
        System.out.println(animals);
        System.out.println(questions);

        int q = 0;
        int a = 0;

        while (q < questions.size()) {
            System.out.println(questions.get(q));
            userAnswer = input.nextLine().toUpperCase();

            if(userAnswer.equals("YES")) {
                System.out.println("Is it a " + animals.get(a));
                userAnswer = input.nextLine().toUpperCase();
                while(a < animals.size()) {
                    if(userAnswer.equals("YES")) {
                        System.out.println("Yay! I win. Do you want to play again?");
                        userAnswer = input.nextLine().toUpperCase();
                    }
                    else if(a < animals.size()) {
                            a++;
                        }

                    else {
                        System.out.println("I give up. What animal is it?");
                        userAnimal = input.nextLine();
                        animals.add(userAnimal);
                    }
                }

            }
            else {
                if(q < questions.size()) {
                    q++;
                }
            }
        }
        System.out.println("I give up. What animal is it?");
        userAnimal = input.nextLine();
        animals.add(userAnimal);


        System.out.println("Type in a question for which the answer is yes for " + animals.get(a) + " but no for " + userAnimal);
        userQuestion = input.nextLine();

        questions.add(userQuestion);

        System.out.println("Do you want to play again?");
        userAnswer = input.nextLine().toUpperCase();
    }
}
}

我假设有一种更简单的方法来实现这一点(也许是二叉树),但我就是想不通。我不想要完整的解决方案,我只想指出正确的方向。

最佳答案

首先,尝试创建更小的方法,这样一切都更容易阅读。
第二个注意事项,您添加了问题和动物,但不要包含与这些匹配的任何内容。您的计算机只是随机猜测一种动物,而不会排除任何动物。
例子:

如果 Q1 与 Dog 相关,如果之后没有正确猜出,您应该将其从可能的答案中删除。

关于java - 让电脑变得更聪明的猜谜游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35318466/

10-11 02:22