首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >猜谜游戏,让计算机变得更聪明

猜谜游戏,让计算机变得更聪明
EN

Stack Overflow用户
提问于 2016-02-10 22:55:06
回答 4查看 989关注 0票数 4

我正在尝试制作一个猜谜游戏,在这个游戏中,计算机在每次猜测后都会变得越来越聪明。这是一个运行的例子:(计算机正在猜测你在想什么动物)

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

等。

到目前为止,我的代码如下:

代码语言:javascript
复制
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();
    }
}
}

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

EN

回答 4

Stack Overflow用户

发布于 2016-02-10 23:14:26

首先,试着创建更小的方法,这样一切都更容易阅读。

第二个注意事项,您添加了问题和动物,但不要包含任何与这些匹配的内容。你的计算机只是猜测一个随机的动物,从来不会排除任何动物。

示例:

Q1:是的。

动物:马,狗

Q1:否

动物:马,狗

如果Q1与狗有关,如果之后没有正确猜测,则应将其从可能的答案中删除。

票数 1
EN

Stack Overflow用户

发布于 2016-02-11 02:17:41

最好的方法可能是有一个动物类,它有一个答案的ArrayList。您还需要一个问题的ArrayList,具有相同的排序顺序。现在,对于每次遍历,您可以复制所有动物的列表,并找出问题不适合的动物。为此,你只需循环所有的问题,并检查你复制的动物列表是否合适。

你的动物类可能看起来不会比这个更复杂:

代码语言:javascript
复制
class Animal {

    ArrayList<Answer> answers;

    public Animal() {
        answers = new ArrayList<Answer>();
    }

    public Answer checkQuestion(int questionId) { ... }

    public void setAnswerToQuestion(int questionId, Answer answer) { ... }

}

Answer在这里应该是一个枚举,它由可能性"Yes/True","No/False“和"Unkown”组成。

有了这个,你可以计算每种动物的相对相似度,甚至可以跳过不必要的问题。

票数 1
EN

Stack Overflow用户

发布于 2016-02-10 23:16:38

嗯,在我看来,这似乎是一个分类问题。您希望根据一组属性来发现它是什么动物。

由于您希望将它们表述为问题,因此我建议使用基于规则或基于树的系统。你可以看看像J48这样的决策树系统。每次用户回答了一个问题,重新训练你的模型。然后,您可以提出问题,并跟随您的树到达叶节点。

你可以看看weka,它有一堆这样的方法已经准备好并实现了。

日安

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35318466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档