-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafecracker.java
More file actions
246 lines (225 loc) · 6.14 KB
/
Safecracker.java
File metadata and controls
246 lines (225 loc) · 6.14 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
import java.util.Scanner;
import java.util.Random;
class Safecracker{
private int[] code = new int[3]; //global variable to hold code
private boolean running = true; //status of the program
private int maxAttempts = -1;
private boolean showHints = false;
private Scanner scanner = new Scanner(System.in);
/**
* Creating instance of this class and running it
*/
public static void main(String[] args){
Safecracker instance = new Safecracker();
instance.start();
}
/**
* Starts the program and keeps it running
*/
public void start(){
while(this.running){
this.showOptionMenu();
System.out.print("Enter an option: ");
this.handleOption(this.getOption());
}
}
/**
* Executes an action that should happen when specific option entered
* @param option
*/
private void handleOption(int option){
switch(option){
case 1:
this.letUserEnterCode();
break;
case 2:
this.generateCode();
break;
case 3:
this.letUserSetMaxAttempts();
break;
case 4:
if(this.showHints){
System.out.println("Hints were turned off!");
}else{
System.out.println("Hints were turned on!");
}
this.showHints = !this.showHints;
break;
case 5:
this.letUserGuessCode();
break;
case 6:
this.running = false;
}
}
/**
* Sets the maximum amount of attempts
* @param maxAttempts
*/
public void setMaxAttempts(int maxAttempts){
this.maxAttempts = maxAttempts;
}
/**
* Returns the maximum amount of attempts
* @return int
*/
public int getMaxAttempts(){
return this.maxAttempts;
}
/**
* Asks the user for the maximum number of attempts and makes sure valid value entered and stored
*/
private void letUserSetMaxAttempts(){
System.out.print("Input max amount of attempts or enter 0 for unlimited: ");
boolean valid = false;
int attempts;
while(!valid){
try{
attempts = Integer.parseInt(this.scanner.nextLine());
}catch(Exception e){
System.out.println("Must be numeric, try again: ");
continue;
}
if(attempts < 0){
System.out.print("Can't be less than 0, try again: ");
continue;
}
if(attempts == 0){
this.maxAttempts = -1;
System.out.println("Unlimited number of attempts was set");
}else{
this.maxAttempts = attempts;
System.out.println("Maximum attempts of " + attempts + " was set");
}
valid = true;
}
}
/**
* Asks the user to guess the current code
*/
private void letUserGuessCode(){
System.out.print("Try to guess the code: ");
boolean guessed = false;
String code;
int guesses = 0;
while(!guessed){
code = this.scanner.nextLine();
if(code.length() != 3){
System.out.print("The code should be 3-digit, try again: ");
continue;
}
if(!this.isCodeEqual(code.split(""))){
guesses++;
if(guesses == this.maxAttempts){
System.out.println("Wrong code, maximum amount of attempts exceeded");
return;
}else{
System.out.print("Wrong code, try again: ");
}
continue;
}
guessed = true;
}
System.out.println("You guessed it!");
}
/**
* Cheks if given code equal to the currently stored one
* @param code
* @return boolean
*/
private boolean isCodeEqual(String[] code){
boolean correct = true;
try{
for(int i = 0; i < code.length; i++){
if(Integer.parseInt(code[i]) != this.code[i]){
correct = false;
}else{
if(this.showHints){
System.out.println("You gussed the right number at position " + (i + 1) + " it is number " + code[i]);
}
}
}
}catch(Exception e){
return false;
}
return correct;
}
/**
* Randomly generates 3-digit code
*/
public void generateCode(){
Random random = new Random();
for(int i = 0; i < 3; i++){
this.code[i] = random.nextInt(9);
}
System.out.println("Random code was set");
}
/**
* Lets user enter the code to store. Ensures that data inputed is valid.
*/
private void letUserEnterCode(){
System.out.print("Enter the code you want to set: ");
boolean valid = false;
String code;
String[] splitCode = new String[3];
loop:
while(!valid){
code = this.scanner.nextLine();
if(code.length() != 3){
System.out.print("The code should be 3-digit, try again: ");
continue;
}
splitCode = code.split("");
for(int i = 0; i < splitCode.length; i++){
try{
this.code[i] = Integer.parseInt(splitCode[i]);
}catch(Exception e){
System.out.print("The code should be numeric, try again: ");
continue loop;
}
}
valid = true;
}
}
/**
* Displays option menu
*/
public void showOptionMenu(){
System.out.println("1 - Manually set a new 3-digit code\n2 - Randomly generate a 3-digit code\n3 - Set max number of guesses\n4 - Turn hints on/off\n5 - Guess the code\n6 - Exit");
}
/**
* Sets given code as current code
* @param code
*/
public void setCode(int[] code){
this.code = code;
}
/**
* Returns if the given string can be converted to integer
* @param String unparsedInt
* @return boolean
*/
public boolean isInt(String unparsedInt){
try{
Integer.parseInt(unparsedInt);
}catch(Exception e){
return false;
}
return true;
}
/**
* Reads input from the user
* @return int
*/
private int getOption(){
String option = this.scanner.nextLine();
int numericOption = 0;
try{
numericOption = Integer.parseInt(option);
}catch(Exception e){
System.out.println("Selected option doesn't exists");
}
return numericOption;
}
}