Download - 20 Questions Assignment Intro
![Page 1: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/1.jpg)
20 QuestionsAssignment Intro
James WeiProfessor PeckMarch 19, 2014
Slides by James Wei
![Page 2: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/2.jpg)
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
![Page 3: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/3.jpg)
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
![Page 4: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/4.jpg)
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
![Page 5: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/5.jpg)
Model-View Design• Code is split between model and view• You write the model• Know how to interact with the view• Nothing new here…
![Page 6: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/6.jpg)
Model-View Design
![Page 7: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/7.jpg)
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?
![Page 8: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/8.jpg)
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
![Page 9: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/9.jpg)
Implementation -- Overview
![Page 10: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/10.jpg)
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
![Page 11: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/11.jpg)
Implementation – File I/O
![Page 12: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/12.jpg)
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 AnimalNode readHelper(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}
![Page 13: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/13.jpg)
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
![Page 14: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/14.jpg)
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()
![Page 15: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/15.jpg)
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
![Page 16: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/16.jpg)
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…
![Page 17: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/17.jpg)
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()
![Page 18: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/18.jpg)
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
![Page 19: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/19.jpg)
Implementation – Improving• Improving the tree• getNewInfoLeaf will 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()
![Page 20: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/20.jpg)
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
![Page 21: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/21.jpg)
Implementation – Improving• Example – here’s the tree before playing:
For reference ->
![Page 22: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/22.jpg)
Implementation – Improving• Example – here’s the tree after playing:
For reference ->
![Page 23: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/23.jpg)
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
![Page 24: 20 Questions Assignment Intro](https://reader036.vdocuments.mx/reader036/viewer/2022062411/568165d2550346895dd8e163/html5/thumbnails/24.jpg)
Good luck!
I didn’t have time to find a witty tree or 20Q related cartoon…