-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.java
200 lines (175 loc) · 5.7 KB
/
Model.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
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Model implements ControllerToModel{
private static ArrayList<String> SECRET_WORDS;
private static final int MAX_BAD_GUESSES = 7;
private int totalBadGuesses;
// An array keeping track of letters of word that have been guessed.
// Letters not guessed are represented with '_'
private char correctLetters[];
// An array keeping track of the incorrectly guessed letters
private char incorrectLetters[];
private static int gameswon;
private static int gamesplayed;
public Model(String secretWordFile) throws IOException
{
totalBadGuesses = 0;
gameswon = 0;
gamesplayed = 0;
SECRET_WORDS = new ArrayList<String>();
Scanner fileReader = null;
try{
fileReader = new Scanner(new File(secretWordFile));
while(fileReader.hasNextLine()){
String line = fileReader.nextLine(); //the current line
//add the current line into the Arraylist SECRET_WORDS
SECRET_WORDS.add(line);
}
}catch(IOException e){
System.out.println("Encountered an Input/Output Error.");
throw e;
}finally{
if(fileReader!=null)
fileReader.close();
}
}
public int getGamesWon(){
return gameswon;
}
public int getGamesPlayed(){
return gamesplayed;
}
//purpose: find if guess previously guessed
//inputs: guess - char the player guessed
//assumptions: correctLetters and incorrectLetters instantiated
//post: true returned if this char previously guessed, false ow
public boolean previousGuess(char guess){
for (char letter: correctLetters) {
if(letter == guess){
return true;
}
}
for (char letter2: incorrectLetters) {
if(letter2 == guess){
return true;
}
}
return false;
}
//purpose: get the total number of bad guesses so far
//inputs: None
//assumptions: None
//post: totalBadGuesses returned
public int getTotalBadGuesses(){
return totalBadGuesses;
}
//purpose: find if max number of bad guesses guessed already
//inputs: None
//assumptions: None
//post: true returned if max num of bad guesses reached, false ow
public boolean maxBadGuessReached(){
if(totalBadGuesses==MAX_BAD_GUESSES)
return true;
else
return false;
}
//purpose: get a random word for the user to guess in the game
//inputs: None
//assumptions: None
//post: random word returned from list of secret_words
//Also, sets/resets correctLetters and incorrectLetters
public String getRandomWord(){
Random randgen = new Random();
String word = SECRET_WORDS.get(randgen.nextInt(SECRET_WORDS.size()));
setCorrectLetters(word);
setIncorrectLetters(word);
totalBadGuesses=0;
gamesplayed++;
return word;
}
//purpose: set/reset correct letters for this word
//inputs: word - word for user to guess
//assumptions: word is instantiated
//post: correctLetters set and filled
private void setCorrectLetters(String word){
//fill and/or reset correctLetters[]
correctLetters = new char[word.length()];
for (int i = 0; i < correctLetters.length; i++) {
correctLetters[i] = '_';
}
}
//purpose: set/reset incorrect letters for this word
//inputs: word - word for user to guess
//assumptions: word is instantiated
//post: incorrectLetters set and filled
private void setIncorrectLetters(String word){
incorrectLetters = new char[MAX_BAD_GUESSES];
for (int i = 0; i < incorrectLetters.length; i++) {
incorrectLetters[i] = ' ';
}
}
//purpose: add the correctly guessed letter
//inputs: word - word user is trying to guess
//guess - the guess the user made
//i - the index in the word that the guess was found
//assumptions: correctLetters already set
//post: if guess in word, put in correctLetters
public void foundGuessInWord(String word,char guess,int i){
if(0<=i && i< word.length() && word.charAt(i)==guess)
correctLetters[i] = guess;
}
//purpose: add an incorrect guess and check if all incorrect guesses used
//inputs: letterGuessed - the char the user guessed
//assumptions: incorrectLetters instantiated
//post: true returned if all bad guesses used, false ow
public boolean addBadGuess(char letterGuessed){
if(incorrectLetters[totalBadGuesses]==' '){
incorrectLetters[totalBadGuesses] = letterGuessed;
totalBadGuesses++;
}
if(incorrectLetters[incorrectLetters.length-1] == ' ' && totalBadGuesses<MAX_BAD_GUESSES)
return false; //not all bad guesses used up
else
return true; //true that all bad guesses used up
}
//purpose: check if word guessed yet
//inputs: None
//assumptions: correctLetters instantiated
//post: true if entire word guessed, false otherwise
public boolean wordGuessed() {
boolean found = true;
for (int i = 0; i < correctLetters.length; i++) {
if (correctLetters[i] == '_')
found = false;
}
if(found){
gameswon++;
}
return found;
}
//purpose: get the incorrect guesses as a string
//inputs: None
//assumptions: incorrectLetters instantiated
//post: string of incorrectLetters returned
public String incorrectGuessesToString(){
String incorrectGuesses = "";
for (int i = 0; i < incorrectLetters.length; i++) {
incorrectGuesses += incorrectLetters[i] + " ";
}
return incorrectGuesses;
}
//purpose: get the correct guesses in the word as a string
//inputs: None
//assumptions: correctLetters instantiated
//post: string of correctLetters returned
public String correctLettersToString() {
String corrects = "";
for (int i = 0; i < correctLetters.length; i++) {
corrects += correctLetters[i] + " ";
}
return corrects;
}
}