Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

135
LINES

< > BotCompany Repo | #1002517 // Java text game / QuestionTree

Java source code [tags: archive]

1  
//Omar Khan
2  
//CSE 143 Section AA
3  
4  
import java.util.*;
5  
import java.io.*;
6  
7  
//This program builds a tree for a game similar to 20 questions using QuestionNodes
8  
//and displays it using the passed UserInterface object. It also keeps track of the total
9  
//games played and won for an instance of play.
10  
11  
public class QuestionTree{
12  
   private UserInterface ui;
13  
   private QuestionNode root;
14  
   private int played;
15  
   private int won;
16  
17  
   //Passes in the UserInterface and starts the tree with a single QuestionNode that contains the answer "Computer".
18  
   //Throws an IllegalArgumentException if UserInterface is null
19  
   public QuestionTree(UserInterface ui){
20  
      //Checks if passed in ui is null and throws an exception accordingly
21  
      if(ui == null){
22  
         throw new IllegalArgumentException();
23  
      }
24  
      this.ui = ui;
25  
      this.played = 0;
26  
      this.won = 0;
27  
      root = new QuestionNode("computer");
28  
   }
29  
   
30  
   //This method is used to play a game, it builds the question tree as the game progresses.
31  
   public void play(){
32  
      played++;
33  
      root = play(root);
34  
   }
35  
   
36  
   //Helper method to play the game, returns a 
37  
   //QuestionNode and requires a QuestionNode be passed in.
38  
   private QuestionNode play(QuestionNode otherRoot){
39  
      if(otherRoot.isAnswer()){
40  
         ui.print("Would your object happen to be " + otherRoot.data + "?");
41  
         if(ui.nextBoolean()){
42  
            won++;
43  
            ui.println("I win!");
44  
            return otherRoot;
45  
         }else{
46  
            ui.print("I lose. What is your object?");
47  
            String answer = ui.nextLine();
48  
            ui.print("Type a yes/no question to distinguish your item from " + otherRoot.data + ":");
49  
            String question = ui.nextLine();
50  
            ui.print("And what is the answer for your object?");
51  
            
52  
            otherRoot = add(otherRoot, question, answer, ui.nextBoolean());
53  
            return otherRoot;
54  
         }
55  
      }else{
56  
         ui.print(otherRoot.data);
57  
         if(ui.nextBoolean()){
58  
            otherRoot.yes = play(otherRoot.yes);
59  
         }else{
60  
            otherRoot.no = play(otherRoot.no);
61  
         }
62  
      }
63  
      return otherRoot;
64  
   }
65  
   
66  
   //Helper method to add a new question and answer, used to expand the tree.
67  
   private QuestionNode add(QuestionNode otherRoot,String question, String answer, boolean y){
68  
      QuestionNode currQuestion = new QuestionNode(question);
69  
      if(y){
70  
         currQuestion.yes = new QuestionNode(answer);
71  
         currQuestion.no = otherRoot;
72  
      }else{
73  
         currQuestion.yes = otherRoot;
74  
         currQuestion.no = new QuestionNode(answer);
75  
      }
76  
77  
      return currQuestion;
78  
   }
79  
   
80  
   //This method saves the contents of the tree in an appropriate 
81  
   //format to the passed in PrintStream object.
82  
   //Throws an IllegalArgumentException if passed PrintStream is null
83  
   public void save(PrintStream output){
84  
      if(output == null){
85  
         throw new IllegalArgumentException();
86  
      }
87  
      save(output, root);
88  
   }
89  
   
90  
   //Helper method for save which takes in a PrintStream object and a QuestionNode.
91  
   private void save(PrintStream output, QuestionNode otherRoot){
92  
      if(otherRoot.isAnswer()){
93  
         output.println("A:" + otherRoot.data);
94  
      }else{
95  
         output.println("Q:" + otherRoot.data);
96  
         save(output, otherRoot.yes);
97  
         save(output, otherRoot.no);
98  
      }
99  
   }
100  
   
101  
   //This method builds a new tree using the contents of a passed in Scanner object.
102  
   //Throws an IllegalArgumentException if passed Scanner is null.
103  
   public void load(Scanner input){
104  
      if(input == null){
105  
         throw new IllegalArgumentException();
106  
      }
107  
      root = load(input, root);
108  
   }
109  
   
110  
   //Helper method that takes in a Scanner object 
111  
   //and a QustionNode, also returns a QuestionNode.
112  
   private QuestionNode load(Scanner input, QuestionNode otherRoot){
113  
      if(!input.hasNextLine()){
114  
         return otherRoot;
115  
      }else{
116  
         String data = input.nextLine();
117  
         otherRoot = new QuestionNode(data.substring(2));
118  
         if(data.startsWith("Q:")){
119  
            otherRoot.yes = load(input, otherRoot.yes);
120  
            otherRoot.no = load(input, otherRoot.no);
121  
         }
122  
      }
123  
      return otherRoot;
124  
   }
125  
   
126  
   //Returns the total games won.
127  
   public int gamesWon(){
128  
      return won;
129  
   }
130  
   
131  
   //Returns total games played.
132  
   public int totalGames(){
133  
      return played;
134  
   }
135  
}

Author comment

Began life as a copy of #1002516

download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002517
Snippet name: Java text game / QuestionTree
Eternal ID of this version: #1002517/1
Text MD5: a5251402e12325521f1f6ee485f891cc
Author: stefan
Category:
Type: Java source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-29 18:34:36
Source code size: 4542 bytes / 135 lines
Pitched / IR pitched: No / No
Views / Downloads: 3030 / 192
Referenced in: [show references]