1 | // CSE 143 Homework 6: 20 Questions |
2 | // |
3 | // To use the jGRASP debugger with this program, set a breakpoint |
4 | // and once the execution breaks, open 'this' or 'tq' on the left, |
5 | // then look at its variable 'tree'. That's your QuestionTree. |
6 | // Drag your 'tree' over to the right to see a visualization of it. |
7 | // |
8 | // (Your QuestionTree is constructed by this file on line 30. |
9 | // The overall loop to play games is around line 68.) |
10 | |
11 | import java.io.*; |
12 | import java.util.Scanner; |
13 | |
14 | /** A basic text user interface for the 20 questions game. */ |
15 | public class QuestionMain implements UserInterface { |
16 | public static void main(String[] args) { |
17 | QuestionMain tq = new QuestionMain(); |
18 | tq.run(); |
19 | } |
20 | |
21 | |
22 | // fields |
23 | private Scanner console; |
24 | private QuestionTree tree; |
25 | |
26 | /** Constructs a text user interface and its question tree. */ |
27 | public QuestionMain() { |
28 | console = new Scanner(System.in); |
29 | tree = new QuestionTree(this); |
30 | } |
31 | |
32 | /** |
33 | * Returns the user's response as a String. |
34 | */ |
35 | public String nextLine() { |
36 | return console.nextLine(); |
37 | } |
38 | |
39 | /** Prints the given string to the console. */ |
40 | public void print(String message) { |
41 | System.out.print(message); |
42 | System.out.print(" "); |
43 | } |
44 | |
45 | /** Prints the given string to the console. */ |
46 | public void println(String message) { |
47 | System.out.println(message); |
48 | } |
49 | |
50 | /** Prints a blank line to the console. */ |
51 | public void println() { |
52 | System.out.println(); |
53 | } |
54 | |
55 | /** |
56 | * Waits for the user to answer a yes/no question on the console and returns the |
57 | * user's response as a boolean (true for anything that starts with "y" or "Y"). |
58 | */ |
59 | public boolean nextBoolean() { |
60 | String answer = console.nextLine(); |
61 | return answer.trim().toLowerCase().startsWith("y"); |
62 | } |
63 | |
64 | // private helper for overall game(s) loop |
65 | private void run() { |
66 | println("Welcome to the game of 20 Questions!"); |
67 | load(); |
68 | |
69 | // "Think of an item, and I will guess it in N tries." |
70 | println("\n" + BANNER_MESSAGE); |
71 | |
72 | do { |
73 | // play one complete game |
74 | println(); // blank line between games |
75 | tree.play(); |
76 | print(PLAY_AGAIN_MESSAGE); |
77 | } while (nextBoolean()); // prompt to play again |
78 | |
79 | // print overall stats |
80 | // Games played: N ... I have won: M |
81 | println("\n" + String.format(STATUS_MESSAGE, |
82 | tree.totalGames(), tree.gamesWon())); |
83 | |
84 | save(); |
85 | } |
86 | |
87 | // common code for asking the user whether they want to save or load |
88 | private void load() { |
89 | print(LOAD_MESSAGE); |
90 | if (nextBoolean()) { |
91 | print(SAVE_LOAD_FILENAME_MESSAGE); |
92 | String filename = nextLine(); |
93 | try { |
94 | Scanner in = new Scanner(new File(filename)); |
95 | tree.load(in); |
96 | } catch (FileNotFoundException e) { |
97 | System.out.println("Error: " + e.getMessage()); |
98 | } |
99 | } |
100 | } |
101 | |
102 | // common code for asking the user whether they want to save or load |
103 | private void save() { |
104 | print(SAVE_MESSAGE); |
105 | if (nextBoolean()) { |
106 | print(SAVE_LOAD_FILENAME_MESSAGE); |
107 | String filename = nextLine(); |
108 | try { |
109 | PrintStream out = new PrintStream(new File(filename)); |
110 | tree.save(out); |
111 | out.close(); |
112 | } catch (FileNotFoundException e) { |
113 | System.out.println("Error: " + e.getMessage()); |
114 | } |
115 | } |
116 | } |
117 | } |
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1002516 |
Snippet name: | Java text game / QuestionMain |
Eternal ID of this version: | #1002516/1 |
Text MD5: | 676f602d8e74013e667716e92bdb335a |
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:07 |
Source code size: | 3742 bytes / 117 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 790 / 157 |
Referenced in: | [show references] |