1 / 24

20 Questions Assignment Intro

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.

sef
Download Presentation

20 Questions Assignment Intro

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 20 QuestionsAssignment Intro James Wei Professor Peck March 19, 2014 Slides by James Wei

  2. 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

  3. 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

  4. 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

  5. Model-View Design • Code is split between model and view • You write the model • Know how to interact with the view • Nothing new here…

  6. Model-View Design

  7. 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?

  8. 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

  9. Implementation -- Overview

  10. 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

  11. Implementation – File I/O

  12. 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 }

  13. 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

  14. 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()

  15. 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

  16. 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…

  17. 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()

  18. 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

  19. 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()

  20. 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

  21. Implementation – Improving • Example – here’s the tree before playing: For reference ->

  22. Implementation – Improving • Example – here’s the tree after playing: For reference ->

  23. 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

  24. Good luck! I didn’t have time to find a witty tree or 20Q related cartoon… 

More Related