Guess the game where the computer gets smarter

I am trying to make a fortune-telling game in which the computer constantly becomes smarter after each guess. Here's a launch example: (The computer guesses which animal you think)

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

and etc.

Here is my code:

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 suppose there is an easier way to accomplish this (maybe binary trees), but I just can't figure it out. I do not want complete solutions, I just want them to be sent in the right direction.

+4
source share
4 answers

First of all, try creating smaller methods to make things easier to read.

: , , . .

:

Q1:

: ,

Q1:

: ,

Q1 , , .

+1

, , Animal, ArrayList . ArrayList . , . .

, , , :

class Animal {

    ArrayList<Answer> answers;

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

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

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

}

Enum, "Yes/True", "No/False" "Unkown".

.

+1

, , . , .

, . , J48. , , . .

weka, , .

0

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

Source: https://habr.com/ru/post/1628145/


All Articles