java - 计算机变得更聪明的猜谜游戏

标签 java machine-learning

我正在尝试制作一个猜谜游戏,让计算机在每次猜测后不断变得更聪明。这是一个示例运行: (计算机在猜测你在想什么动物)

计算机:你想到的动物有腿吗? 玩家:是的 电脑:它是一只狗吗? 玩家:是的 电脑:我赢了!你想再玩一次吗? 玩家:是的 电脑:你想到的动物有腿吗 玩家:是的 电脑:它是一只狗吗? 玩家:没有 电脑:我放弃了。你的动物是什么? 球员:马 计算机:输入一个问题,狗的答案是肯定的,马的答案不是 玩家:它住在房子里吗? 电脑:你想再玩一次吗? 玩家:是的 电脑:你想到的动物有腿吗? 玩家:是的 计算机:它住在房子里吗? 玩家:没有 计算机:它是一匹马吗? 玩家:没有 电脑:我放弃

等等

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

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: Yes

Animals: Horse, Dog

Q1: No

Animals: Horse, Dog

如果 Q1 与 Dog 相关,如果之后没有猜对,您应该从可能的答案中删除它。

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

相关文章:

java - Gson json解析不起作用

python - 修改 soft-max 函数以对列表中的最小值给出最高概率的最佳方法是什么?

python - 如何向量化 SVM 中的损失

java - 使用for循环时的除法问题

java - 在 Android 中检查文件是否存在不起作用

java - 如何在 Java 的字符串中打印\n

machine-learning - 神经网络激活函数的选择

python - NLP - 在预处理过程中何时将文本小写

python - 对于小数据集中的非常大的值,梯度下降不会收敛

java - 网络托管上的多处理