-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
executable file
·228 lines (212 loc) · 7.54 KB
/
Game.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
import java.lang.IllegalArgumentException;
import javax.swing.JOptionPane;
import java.util.*;
/**
* This class controls the aspects of the game by requesting info from a User,
* updating the game, and then passing the updated game back to the User
*
* Variables will be declared as protected after testing is complete
*
* @author (Jesse Nelson)
* @version (October 4, 2012 Windows 7(x64) : Java 1.7)
*/
public class Game {
/**
* Minimum number of guesses user is allowed to have
*/
static final int MIN_NUMBER_GUESSES = 1;
/**
* Maximum number of guesses user is allowed to have
*/
static final int MAX_NUMBER_GUESSES = 26;
Dictionary d;
int numGuesses;
String secretWord;
List<String> candidates;
SortedSet<Character> guesses;
char [] clue;
protected Random rand;
/**
* Initialize instance variables and get first guess from user
*
* Instance variables will be made private once testing is complete
*
* @throws FileNotFoundException if the Dictionary file is not located
*/
public Game(HangMan interfaceType) {
try {
this.d = interfaceType.dictionarySetUp();
this.secretWord = this.d.randWord(interfaceType.wordLength());
this.numGuesses = interfaceType.numberOfGuesses();
this.guesses = new TreeSet<Character>();
this.clue = new char [this.secretWord.length()];
for(int i = 0; i < clue.length; i++) {
this.clue[i] = '_';
}
updateGame(interfaceType.showGame("Welcome to HangMan!", this), interfaceType);
} catch(FileNotFoundException e){
System.out.println("There was an error loading the default and/or selected Dictionary");
}
/* Should never happen!*/
catch(NullPointerException e ){
JOptionPane.showMessageDialog(null, "There is critical information missing from the game.\n"
+ "The game is restarting.", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public Game(){
}
/**
* Constructer to used for testing
*/
public Game(int guessesAllowed, int wordLength, String word) {
try {
this.d = new Dictionary();
if(word == null) {
this.secretWord = d.randWord(wordLength);
} else secretWord = word;
this.numGuesses = guessesAllowed;
this.guesses = new TreeSet<Character>();
this.clue = new char [this.secretWord.length()];
for(int i = 0; i < clue.length; i++) {
clue[i] = '_';
}
} catch(FileNotFoundException e) {
System.out.println("There was an error loading the default and/or selected Dictionary");
}
}
public Game(Dictionary dictionary, HangMan interfaceType) { // Used by EvilHangMan
try {
this.rand = new Random();
this.d = dictionary;
this.candidates = d.allWordsOfLength(interfaceType.wordLength());
this.numGuesses = interfaceType.numberOfGuesses();
this.secretWord = candidates.get(rand.nextInt(candidates.size()));
this.guesses = new TreeSet<Character>();
this.clue = new char [secretWord.length()];
for(int i = 0; i < clue.length; i++) {
this.clue[i] = '_';
}
/* Should never happen!*/
} catch(NullPointerException e ){
JOptionPane.showMessageDialog(null, "There is critical information missing from the game.\n"
+ "The game is restarting.", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Checks if current guess is part of the secretWord and updates the game accordingly
*
* @param Current guess
*/
public void updateGame(char c, HangMan interfaceType) {
if(checkGuess(c)) {
this.rightGuess(c, interfaceType);
}
else {
this.wrongGuess(c, interfaceType);
}
}
/**
* Checks to see if the User's guess is part of the secret word
*
* @param Current guess
*
* @return Returns true if guess is correct, else false
*/
public boolean checkGuess(char c) {
for(int i = 0; i < this.secretWord.length(); i++) {
if(this.secretWord.charAt(i) == c) return true;
}
return false;
}
/**
* Checks to see if the game is over
*
* @returns true if the secrect word has been guessed
* or the max number of guesses has been reached
* else returns false
*/
public boolean gameOver() {
return this.numGuesses == 0 || Arrays.equals(this.secretWord.toCharArray(), this.clue);
}
/**
* Update the string of wrongLetters, calls decreaseNumberOfGuesses, and checks
* to see if the Game is over.
*
* If the game is lost, GameLost() is called
*
* Shows the game to the user
*
* @param Current guess
*/
public void wrongGuess(char c, HangMan interfaceType) throws IllegalArgumentException {
this.decreaseGuessesLeft(c);
this.guesses.add(c);
if(gameOver()) {
interfaceType.gameLost(secretWord, this);
}
else {
this.updateGame(interfaceType.showGame(c + " Is incorrect", this), interfaceType);
}
}
/**
* Update the clue with the correct guess and check to see if game is over
*
* Shows the game to the user
*
* @param Current guess
*/
public void rightGuess(char c, HangMan interfaceType) {
this.guesses.add(c);
this.updateClue(c);
if(gameOver()) {
interfaceType.gameWon(this);
}
else {
this.updateGame(interfaceType.showGame("You are getting closer! " + c + " Is correct!", this), interfaceType);
}
}
/**
* Decreases the number of guesses unless the guess is a duplicate
*
* @args The current guess
*/
public void decreaseGuessesLeft(char c) {
if (this.guesses.contains(c) || c == '`') {
this.numGuesses = this.numGuesses;
} else {
this.numGuesses--;
}
}
/**
* Updates clue with newly guessed letters when they are correct
*
* @args Current guess
*/
public void updateClue(char c) {
for(int i = 0; i < this.secretWord.length(); i++) {
if(this.secretWord.charAt(i) == c) {
this.clue[i] = c;
}
}
}
/**
* Displays the number of guesses left, the guessed letters, and the clue
*/
public String toString() {
return "Guesses Left: " + this.numGuesses +
"\nGuessed Letters: " + this.guesses +
"\nClue: " + this.formatClue();
}
/**
* Formats the clue to be easily read by the user
*/
private String formatClue() {
String formattedClue = "";
for(int i = 0; i < clue.length; i++) {
formattedClue += clue[i] + " " ;
}
return formattedClue;
}
}