Jacob
Jacob

Reputation: 624

Guessing game where the computer get smarter

I'm trying to make a guessing game where the computer continuously becomes smarter after every guess. Here is an example run: (The computer is guessing what animal you're thinking of)

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

etc.

Here is my code so far:

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();
    }
}
}

I'm assuming that there is an easier way to accomplish this (maybe binary trees), but I just can't figure it out. I don't want complete solutions, I just want to be pointed in the right direction.

Upvotes: 4

Views: 1298

Answers (4)

0xDEADBEEF
0xDEADBEEF

Reputation: 590

sample solution for your problem

import java.util.Scanner;

class DoesItHaveLegs
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int answer=0;
        int question=0;
        int anotherQuestion=0;
        int actualAnimals=0;
        int roundAbout=0;
        String quitter="YES";
        String userInput=null;
        Animal animal = new Animal();
        do
        {
            userInput="";
            for(int a=0;a<animal.getQuestionsLength();++a)
            {
                userInput="";
                if(a>0)
                {

                    animal.displayQuestion(a);
                    userInput=scan.nextLine();
                    if(userInput.equalsIgnoreCase("Yes"))
                    {
                        answer=a;
                        a=animal.getQuestionsLength();
                    }
                }
                else
                {
                    animal.displayQuestion(a);
                    userInput=scan.nextLine();
                    if(userInput.equalsIgnoreCase("No"))
                    {
                        a=animal.getQuestionsLength();
                    }
                }

            }

            if(userInput.equalsIgnoreCase("Yes"))
            {
                String enteredAnswer=null;

                        if(answer>0)
                        {
                            animal.displayAnswer(answer-1);
                            enteredAnswer=scan.nextLine();
                        }
                        else
                        {
                            animal.displayAnswer(answer);
                            enteredAnswer=scan.nextLine();
                        }
                        if(enteredAnswer.equalsIgnoreCase("Yes"))
                        {
                            System.out.println("Yay I won");
                            System.out.println("Do you want to play again?");
                            quitter=scan.nextLine();
                        }
                        else
                        {
                            animal.giveUpMessage();
                            String enteredAnimal = scan.nextLine();
                            animal.addActualAnimals(enteredAnimal);
                            animal.addAnswers("Is it a "+enteredAnimal);
                            actualAnimals=animal.getActualAnimalsLength()-1;
                            System.out.println("Type a question for which the answer is Yes for "+animal.getActualAnimals(actualAnimals-1)+" but No for "+animal.getActualAnimals(actualAnimals));
                            String enteredQuestion = scan.nextLine();
                            animal.addQuestions(enteredQuestion);
                            System.out.println("Do you want to play again?");
                            quitter=scan.nextLine();

                        }



            }
            else if(userInput.equalsIgnoreCase("No"))
            {
                String enteredAnswer=null;
                for(int i=0;i<=animal.getAnswersLength();++i)
                {
                    if(i==animal.getAnswersLength())
                    {
                        animal.displayAnswer(i-1);
                        enteredAnswer=scan.nextLine();
                        if(enteredAnswer.equalsIgnoreCase("Yes"))
                        {
                            System.out.println("Yay I won");
                            System.out.println("Do you want to play again?");
                            quitter=scan.nextLine();
                        }
                        else
                        {
                            animal.giveUpMessage();
                            String enteredAnimal = scan.nextLine();
                            animal.addActualAnimals(enteredAnimal);
                            animal.addAnswers("Is it a "+enteredAnimal);
                            actualAnimals=animal.getActualAnimalsLength()-1;
                            System.out.println("Type a question for which the answer is Yes for "+animal.getActualAnimals(actualAnimals-1)+" but No for "+animal.getActualAnimals(actualAnimals));
                            String enteredQuestion = scan.nextLine();
                            animal.addQuestions(enteredQuestion);
                            i=animal.getAnswersLength();
                            System.out.println("Do you want to play again?");
                            quitter=scan.nextLine();
                        }
                    }
                }
            }



        }
        while(!(quitter.equalsIgnoreCase("NO")));

    }
}



//another class
import java.util.*;
class Animal
{
    private ArrayList<String> answers;
    private ArrayList<String> questions;

    private ArrayList<String> actualAnimals;
    public Animal()
    {
        answers = new ArrayList<String>();
        questions = new ArrayList<String>();

        actualAnimals=new ArrayList<String>();
        actualAnimals.add("dog");

        questions.add("Does the animal you are thinking of have legs?");
        answers.add("Is it a dog?");
    }
    public String getActualAnimals(int actualAnimal)
    {
        return actualAnimals.get(actualAnimal);
    }
    public String getQuestions(int questionNumber)
    {
        return questions.get(questionNumber);

    }
    public String getAnswers(int answerNumber)
    {
        return answers.get(answerNumber);
    }   
    public void addQuestions(String question)
    {
        questions.add(question);
    }
    public void addAnswers(String answer)
    {
        answers.add(answer);
    }
    public void addActualAnimals(String animal)
    {
        actualAnimals.add(animal);
    }
    public void displayQuestion(int questionNumber)
    {
        System.out.println(questions.get(questionNumber));
    }
    public void displayAnswer(int answer)
    {
        System.out.println(answers.get(answer));
    }

    public void giveUpMessage()
    {
        System.out.println("I give up What was your animal");
    }
    public int getAnswersLength()
    {
        return answers.size();
    }
    public int getQuestionsLength()
    {
        return questions.size();
    }

    public int getActualAnimalsLength()
    {
        return actualAnimals.size();
    }
}

Upvotes: 0

Jannis Jorre
Jannis Jorre

Reputation: 142

The best approach would probable be to have a Animal-class which has an ArrayList of answers. You will also need an ArrayList of questions, with the same sorting-order. Now for every runthrough you could copy the list of all animals and sort out the ones for which the questions don't fit. For that you simply loop through all questions and check your copied list of animals for fits.

Your animal class is probably not gonna look more complicated than this:

class Animal {

    ArrayList<Answer> answers;

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

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

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

}

Answer is supposed to be an Enum here, which consists of the possibilities "Yes/True", "No/False" and "Unkown".

With this you can calculate the relative likelinesses for each animal and even skipped unnecessary questions.

Upvotes: 1

Dries De Rydt
Dries De Rydt

Reputation: 808

Well this seems to me like a classification problem. You want to discover what animal it is based on a set of attributes.

Since you want to phrase them as questions, I suggest a rule based or tree based system. You can have a look at Decision tree systems like J48. Every time the user has answered a question, re-train your model. Then you can ask the questions and follow your tree to the leaf nodes.

You can look at weka, it has a bunch of these methods ready and implemented.

Good day

Upvotes: 0

Wesley De Keirsmaeker
Wesley De Keirsmaeker

Reputation: 1025

First of all, try to create smaller methods, so everything is easier to read.

Second notice, you add questions and animals, but don't include anything that matches these together. Your computer just guesses a random animal and never excludes any.

Example:

Q1: Yes

Animals: Horse, Dog

Q1: No

Animals: Horse, Dog

If Q1 relates to Dog, you should remove this from the possible answers if it isn't guessed correctly after.

Upvotes: 1

Related Questions