240 likes | 380 Views
20 Questions Assignment Intro. James Wei Professor Peck March 19, 2014. Slides by James Wei. Outline. A look at the game of 20Q Classes of 20Q/Model-View design Implementation Reading a file Building a tree Playing the game Adding to the tree Saving a file. A look at 20Q.
E N D
20 QuestionsAssignment Intro James Wei Professor Peck March 19, 2014 Slides by James Wei
Outline • A look at the game of 20Q • Classes of 20Q/Model-View design • Implementation • Reading a file • Building a tree • Playing the game • Adding to the tree • Saving a file
A look at 20Q • We want to create a simplified version of 20Q • Here are the features we implement: • Can play 20Q • Can create game trees from files • Can improve tree when computer loses • Can save improved game trees to files • Sample playthrough
Classes of 20Q • The classes of 20Q: • GameMain – runs 20Q • IAnimalModel – defines the methods and behaviors available to the model • AnimalGameModel – implements IAnimalModel • AnimalGameViewer – handles display logic • AnimalNode – represents a node on the tree • WebGameInfo – don’t worry about this one
Model-View Design • Code is split between model and view • You write the model • Know how to interact with the view • Nothing new here…
Implementation -- Overview • A few parts to this assignment: • Reading a file: initialize() • Building a tree: initialize() • Playing the game: newGame(), processYesNo() • Adding to the tree: addNewQuestion(), addNewKnowledge() • Saving a file: write() • Also consider while you’re coding—what instance variables do we need in the model?
Implementation -- Overview • View methods you will need to use: • void update(String) – displays text in main pane • void showMessage(String) – displays text in message pane • void showDialog(String) – displays a popup with the given text • void getNewInfoLeaf() – tells view to ask for a new tree leaf • void getDifferentiator() – tells view to ask for a new question node
Implementation – File I/O • Reading a file • Need to start by building the 20Q tree from a file • Implement this method: void initialize(Scanner) • initialize must do the following: • Use the scanner to read the file line by line • Use each line to build one node of the tree (in preorder) • Strip the “#Q:” from each question before creating its internal node • Call myView.setEnabled(true) • Call newGame() at the very end to start a game
Implementation – File I/O • Your life will be much easier if you write a recursive helper method • Some pseudo-code to get you started: private AnimalNodereadHelper(Scanner s) { String line = s.nextLine(); if (line is leaf) { // create new leaf } // recursively create left (yes) subtree // recursively create right (no) subtree // create internal node from subtrees }
Implementation – File I/O • Writing a file • Need to be able to save trees to file • Implement this method: void write(FileWriter) • write must do the following: • Write one line in the file for each node using the FileWriter write() method • Write a preorder traversal • Prepend “#Q:” for each question (internal node) • Append “\n” to the end of each line to signify new line
Implementation -- Overview • A few parts to this assignment: • Reading a file: initialize() • Building a tree: initialize() • Playing the game: newGame(), processYesNo() • Adding to the tree: processYesNo(), addNewQuestion(), addNewKnowledge() • Saving a file: write()
Implementation – Playing 20Q • Playing the game • Begins when the player clicks “New Game”, or when the player loads a file, both of which call the model method newGame() • First implement this method: void newGame() • newGame must do the following: • Set all instance variables to their initial state • Ask the first question
Implementation – Playing 20Q • Playing the game • As you ask questions, the user responds with YES or NO by clicking a button in the view—those buttons call processYesNo(boolean) • Next implement this method: void processYesNo(boolean) • processYesNo must do the following: • Update the current position on the game tree according to the user’s response • Ask the next question – remember this varies depending on if we are at an internal or a leaf! • Display a WIN or LOSE message if game has ended • Few more things which we’ll discuss in the next part…
Implementation -- Overview • A few parts to this assignment: • Reading a file: initialize() • Building a tree: initialize() • Playing the game: newGame(), processYesNo() • Adding to the tree: processYesNo(), addNewQuestion(), addNewKnowledge() • Saving a file: write()
Implementation – Improving • Improving the tree • If the computer loses, it’s time to add to our tree! • Go back and add to this method: void processYesNo(boolean) • processYesNo must also do the following: • Ask for the answer (what were you thinking of?) • Call getNewInfoLeaf() – this will tell the view to popup a dialog box where the user can type a response
Implementation – Improving • Improving the tree • getNewInfoLeafwill call another model method • Implement this method: void addNewQuestion(String) • addNewQuestion must do the following: • Display a list of all of the user’s answers up to that point, e.g. “You answered YES to <QUESTION>, You answered NO to <QUESTION>, etc.” • Create a new leaf node for the correct answer • Ask for a differentiator question • Call getDifferentiator()
Implementation – Improving • Improving the tree • addNewQuestion will call the final method • Implement this method: void addNewKnowledge(String) • addNewKnowledge must do the following: • Create a new internal node for the differentiator • Set the differentiator’s YES answer to the existing, incorrect answer that the computer guessed, and the differentiator’s NO answer to the new, correct answer • Add the internal node to the game tree • Start a new game
Implementation – Improving • Example – here’s the tree before playing: For reference ->
Implementation – Improving • Example – here’s the tree after playing: For reference ->
Wrapping Up • So you slept through this whole thing and now you just want to know what to do, eh? • My recommended course of action: • Implement file I/O, aka initialize() and write() • Test by opening a file and saving it • Implement newGame() and processYesNo(), leaving out improving the tree for now • Play the game – try winning and losing • Implement addNewQuestion() and addNewKnowledge() • Test it – open a file, play several times (win/lose), improve the tree, play with new tree, save file
Good luck! I didn’t have time to find a witty tree or 20Q related cartoon…