preview

Nt1330 Unit 3 Assignment

Better Essays

import java.util.*; import java.io.*; // Michael Anthony Wilson // CSE 143; section: AF; TA: Joshua // Assignment: QuestionTree and QuestionNode // This assignment will simulate an interactive game // between a CPU and a player. In this game the player // will answer a series of questions that the CPU has // attained from either a file or throughout previous // interaction with the user public class QuestionTree { private QuestionNode overallRoot; private UserInterface user; private int count; private int winnerCount; // This will initalize the game state // The gaming tree will either grow larger as more questions // arise from the user, or will be set to a certain number of // predetermined questions and answers …show more content…

If the computer wins // then a message will print out stating so. Otherwise the computer asks the user for // an object, a question for the object and then an answer for the object. public void play() { overallRoot = play(overallRoot); } // This method will return a QuestionNode whenever the program runs out of questions // and does not find an answer for the user, the question node will keep track of // the object that the user is thinking about and a question that the user sets for // the object private QuestionNode play(QuestionNode root) { rootIsNull(root); if(root.left == null && root.right == null) { user.print("Would your object happen to be " + root.info + "?"); count++; if(user.nextBoolean()) { user.println("\nI win!"); winnerCount++; } else { user.print("I lose. What is your object?"); QuestionNode answer = new …show more content…

user.print("Type a yes/no question to distinguish your item from " + root.info + ":"); QuestionNode question = new QuestionNode(user.nextLine()); user.print("And what is the answer for your object?"); if (user.nextBoolean()) { question.left = answer; question.right = root; root = question; } else { question.left = root; question.right = answer; root = question;

Get Access