{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMastermind.java
More file actions
268 lines (221 loc) · 7.61 KB
/
Copy pathMastermind.java
File metadata and controls
268 lines (221 loc) · 7.61 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//import stuffs for things
import java.util.Random;
import java.io.Console;
import java.io.IOException;
import java.util.Arrays;
class Mastermind {
//pre: The argument is an array of strings
//post: This is the main function
public static void main (String[] args) {
//set the input function
if (System.console() == null) {
System.err.println("No console.");
System.exit(1);
}
//variable decliration
Mastermind game = new Mastermind();
boolean DEBUGGING = true;
//create the hidden code
game.createHiddenCode();
//FOR DEBUGGING
if(DEBUGGING) {
game.printCode();
}
//print the rules for the game
game.printRules();
//main game loop
while(!game.returnWinner() && game.hasTriesLeft()) {
//reset the pegs
game.setBlackCount();
game.setWhiteCount();
//get guess from user
game.getUserGuess(game.promptForGuess());
//get black and white pegs
char[] tempArray = Arrays.copyOf(game.returnCode(), game.returnCode().length);
game.getBlackCount(tempArray);
game.getWhiteCount(tempArray);
//print the pegs
game.printPegs();
//check to see if the user has won
game.checkWinner();
game.incrementTries();
}
//see if the user has won
if(game.returnWinner()) {
game.ifWin();
} else {
game.ifLose();
}
}
//define variables
private char[] userGuess = {'x','x','x','x'};
private char[] code = {'X','X','X','X'};
private int blackCount, whiteCount, triesLeft = 10;
private boolean winner = false;
//pre: None
//post: Prints the computer generated code
void printCode() {
//set the input function
for(int i=0; i<code.length; i++) {
System.console().printf("%s", code[i]);
}
System.console().printf("%s", "\n");
}
//pre: None
//post: Returns the computer generated code
char[] returnCode() {
return code;
}
//pre: None
//post: Returns if the winner won or not
boolean returnWinner() {
return winner;
}
//pre: None
//post: Prints what the user inputted. (Used for debugging)
void printUserInput() {
//set the input function
for(int i=0; i<userGuess.length; i++) {
System.console().printf("%s", userGuess[i]);
}
System.console().printf("%s", "\n");
}
//pre: Guess is a character array
//post: Sets the user guess to the argument
void getUserGuess(char[] guess) {
userGuess = guess;
}
//pre: None
//post: Increments the players turn
void incrementTries() {
triesLeft--;
}
//pre: None
//post: Resets the black pegs
void setBlackCount() {
blackCount = 0;
}
//pre: None
//post: Reset the white pegs
void setWhiteCount() {
whiteCount = 0;
}
//pre: newComputerGuess is a character array
//post: Finds the black pegs
void getBlackCount(char[] newComputerGuess) {
for(int i=0; i<newComputerGuess.length; i++) {
if(newComputerGuess[i] == userGuess[i]) {
blackCount++;
newComputerGuess[i] = 'z';
userGuess[i] = 'Z';
}
}
}
//pre: newComputerGuess is a character array
//post: Finds the white pegs
void getWhiteCount(char[] newComputerGuess) {
for(int i=0; i<userGuess.length; i++) {
for(int j=0; j<newComputerGuess.length; j++) {
if(newComputerGuess[i] == userGuess[j]) {
whiteCount++;
newComputerGuess[i] = 'z';
userGuess[j] = 'Z';
}
}
}
}
//pre: None
//post: Prints the pegs for the user
void printPegs() {
System.console().printf("You got %d black pegs and %d white pegs.\n", blackCount, whiteCount);
}
//pre: None
//post: Creates the hidden code
void createHiddenCode() {
//variables
Random rand = new Random();
final int RADIX = 10;
int tempVar = 5;
for(int i=0; i<4; i++) {
tempVar = rand.nextInt(4);
code[i] = Character.forDigit(tempVar,RADIX);
}
}
//pre: None
//post: Defines what the allowed characters are for the user to guess
String allowedCharacters() {
return "0123";
}
//pre: None
//post: Checks to see if the user has won
void checkWinner() {
winner = (blackCount == 4);
}
//pre: none
//post: Returns how many tries the user has left
boolean hasTriesLeft() {
return(triesLeft > 0);
}
//pre: None
//post: Ending message if the user did not guess the code
void ifLose() {
System.out.println("You suck. You took too many tries and did not guess the code");
}
//pre: None
//post: Ending message if the user guessed the code
void ifWin() {
System.out.println("Congrats on WINNING!!! You have won $0");
}
//pre: None
//post: Prints the rules for the player
void printRules() {
System.out.println("* The objective of the game is to guess a four digit password");
System.out.println("* You can guess the numbers 0-3");
System.out.println("* A black peg means one of your numbers is in the right place");
System.out.println("* A white peg menas that one of your numbers is the correct number, but is not in the right place");
}
//pre: None
//post: Gets the user's guess
char[] promptForGuess() {
//define variables
char[] answer = {'X','X','X','X'};
//get input from user
String tempVar = System.console().readLine("Please enter your four numbers without spaces: ");
//check if input is good
boolean goodInput = validateGuess(tempVar);
//set the array and return the user input if it is good
if(goodInput) {
for(int i=0; i<4; i++) {
answer[i] = tempVar.charAt(i);
}
return answer;
}
return promptForGuess();
}
//pre: aString is a string
//post: Checks to see if the input matches the criteria
boolean validateGuess(String aString) {
//define variables
String trueCharacters = allowedCharacters();
boolean goodInput = true;
//checks if the imput was the correct length
if(aString.length() != 4) {
System.out.println("Please enter four (4) characters");
goodInput = false;
} else {
//checks to see if the string contains the correct characters
for(int i=0; i<4; i++) {
if(aString.charAt(i)!=trueCharacters.charAt(0) &&
aString.charAt(i)!=trueCharacters.charAt(1) &&
aString.charAt(i)!=trueCharacters.charAt(2) &&
aString.charAt(i)!=trueCharacters.charAt(3) ) {
goodInput = false;
}
}
if(!goodInput) {
System.out.println("Please enter the characters from '" + trueCharacters + "'");
}
}
return goodInput;
}
}
You can’t perform that action at this time.
