Background Information

Guessing game is a game where the objective of the computer is to guess something the user is thinking of, in a certain category (e.g. games, movies, music, animals).  The computer can ask the user any yes or no questions to help it guess.

Task to Complete

Develop a program for a Guessing Game.  This program must:

  • be interactive with the user
  • learn from the user after it gets “stumped”
  • be able to create, save, update, and load trees from files

September 28th, 2016 – Node and Tree Classes

  • Create the class Node to hold the question “nodes” of the game, that will have:
    • two other nodes, yesNode and noNode, representing its two “children”
    • accessor and mutator methods for yesNode and noNode
    • a String text which will store its content/question
    • an askQuestion() method which will:
      • print out the question: “Is it ” + text + “?”
      • use Scanner to take in a response
      • if the response is “Y”, call the askQuestion() method of yesNode
      • if the response is “N”, call the askQuestion() method of noNode
    • a constructor, taking in a String as a parameter, that:
      • sets text to what it took in
      • sets yesNode and noNode to null
  • Create the class Tree to represent the tree used to store the questions, which has:
    • Node head, referring to the first node of the tree (note: Tree does not have to store any other nodes, because it operates sort of like a linked list where each element has the address of the next)
    • the saveTreeFile() and readTreefile() methods (implemented later) that will be responsible for loading and saving the data from/to a file

September 30th, 2016 – Reading and Writing Files

  • Implementing the file-writing function by:
    • creating a recursive saveToFile() method in Node that:
      • takes in a PrintWriter object fileOut as a parameter
      • prints text to fileOut
      • if yesNode is null, printing “null”, otherwise calling the saveToFile() method of yesNode
      • if noNode is null, printing “null”, otherwise calling the saveToFile() method of noNode
    • implementing the saveTreeFile() method in Tree that:
      • creates the PrintWriter object from the given File
      • calls the saveToFile() method of head
      • closes the PrintWriter object
  • Implementing the file-reading function by:
    • creating a recursive readFromFile() method in Node that:
      • takes in a Scanner object fileIn as a parameter
      • if the next line isn’t “null”, then setting yesNode to whatever the next line is, and calling the readFromFile() method on yesNode
      • then, if the next line isn’t “null”, then setting noNode to whatever the next line is, and calling the readFromFile() method on noNode
    • implementing the readTreeFile() method in Tree that:
      • creates the Scanner object from the given File
      • creates head from the first line of the file
      • calls the readFromFile() method of head

September 30th, 2016 – Program Learning & Final Touches

  • Adding the isGuess() method to Node to tell whether it is the last guess in the tree (in other words, if its children are null)
  • Updating askQuestion() such that if yesNode.askQuestion() or noNode.askQustion() is about to be called, and if yesNode or noNode are last guesses, then to call the lastGuess() method instead of calling askQuestion()
  • Adding the lastGuess() method that:
    • makes the “last guess”
    • if the user answers “Y”, then to print out “Got it!”
    • if the user answers “N”, then to:
      • print out “I got stumped.  What was the answer?”, and take in the correct answer from the user
      • print out “What makes
        different from [last guess]?”, and take in the difference from the user
      • calls the insertNode() method and passes in the correct answer and the difference as parameters
  • Adding the insertNode() method that:
    • takes in the correct answer, the “difference”, and a boolean isYes as parameters
    • if isYes is true, to store the original yesNode in temp and set yesNode as a new Node with the “difference”; if false, do the same with noNode
    • set the yesNode of the “difference” node to a new Node with the correct answer
    • set the noNode of the “difference” node to temp (the original Node)
  • Creating the constructor of Tree to play the game that:
    • uses readTreeFile() to load the information from a given file
    • prints the instructions
    • calls the askQuestion() method of head, which begins the game
    • uses saveTreeFile() to save the new information
    • asks “Do you want to play again?”; if “Y”, then loops back to the start
  • Creating the class Runner to run the program that:
    • creates a new Tree object
  • Playing the game a billion times to add more information to the tree!