-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMazeProgram.java
338 lines (305 loc) · 12.3 KB
/
MyMazeProgram.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
///////////////////////////////////////////////////////////////////////////////
// ALL STUDENTS COMPLETE THESE SECTIONS
// Title: MyMazeProgram
// Files: MyMazeProgram.java
// Semester: Fall 2015
//
// Author: Nawal Dua
// Email: [email protected]
// CS Login: nawal
// Lecturer's Name: Deb Deppeler
// Lab Section: 311
///////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
import java.util.Random;
/**
* This Class (CS 302), is one in we are learning Java.
* In this program, users will be able to convert temperatures from F to C.
* Users will also be able to play a maze game in which they have to get a robot
* to the exit. A harder vesion is available, in which there will be an obstacle
* and positions of the robot, obstacle and exit are randomized.
*
* <p>Bugs: None that are currently known.
*
* @author Nawal Dua
*/
public class MyMazeProgram {
// put method header here
public static void main(String[] args) {
// Scanner connected to keyboard for reading user input
Scanner scnr = new Scanner(System.in);
// Random number generated seeded according to Config file
Random rng = new Random(Config.SEED);
// DISPLAY WELCOME MESSAGE AND HELP
System.out.print("Welcome to MyMazeProgram!" +"\n"+"\n");
// Declare variables and describe their use
int input = 0;
double temp = 0;
int robotRow = 0;
int robotCol = 0;
int exitRow = 4;
int exitCol = 4;
int moveCounter = 0;
int mazeSize = 0;
// MAIN COMMAND LOOP
// while the user has not selected the Exit value
while (true){
// DISPLAY MENU
System.out.println("1. Temperature Converter"+"\n"+
"2. Simple Robot Maze"+"\n"+"3. Random Robot Maze with Obstacle"+
"\n"+"4. Exit");
// DISPLAY COMMAND PROMPT
System.out.print("Enter Choice: ");
// IF the user intput is the correct type:
if (scnr.hasNextInt()) {
// READ (SCAN) user input
input=scnr.nextInt();
// Execute command and display results to user
// Temp Converter Code
if (input==1){ // Code only runs, if user enters 1
System.out.print ("Enter Fahrenheit Temperature: ");
scnr.nextLine(); // Clears any input after the 1
if (scnr.hasNextDouble()){ // if scanner has a double
temp=scnr.nextDouble(); // it sets "temp" as the double
System.out.println(temp+"F="+(temp - 32.0)/(1.8)+"C" +
"\n");
scnr.nextLine(); // clears buffer
}
else // if scanner didn't detect a double
System.out.println("Sorry, can't convert "
+scnr.nextLine()+" to C."+"\n");
}
// Robot Maze code
else if (input==2){
System.out.print ("Help Robot (R) get to Exit (E)"+"\n");
scnr.nextLine(); // clears buffer
// starts loop, for when robot is not at exit
while (robotRow!=exitRow||robotCol!=exitCol){
for (int row=0;row<5; row++){
for (int col=0;col<5; col++){
if (row==robotRow&&col==robotCol){
System.out.print("R ");
}
else if (row==exitRow&&col==exitCol){
System.out.print("E ");
}
else System.out.print("o ");
}
System.out.print("\n");
}
// prints the controls
System.out.print("1. up"+"\n"+"2. down"+"\n"+"3. left"+
"\n"+"4. right"+"\n");
System.out.println("Move? ");
if (scnr.hasNextInt()) {
input=scnr.nextInt();
if (input==1){
scnr.nextLine();
// does not let robot move out of grid
if (robotRow==0){
moveCounter++;
}
else
robotRow--;
moveCounter++;
}
else if (input==2){
scnr.nextLine();
// does not let robot move out of grid
if (robotRow==4){
moveCounter++;
}
else
robotRow++;
moveCounter++;
}
else if (input==3){
scnr.nextLine();
// does not let robot move out of grid
if (robotCol==0){
moveCounter++;
}
else
robotCol--;
moveCounter++;
}
else if (input==4){
scnr.nextLine();
// does not let robot move out of grid
if (robotCol==4){
moveCounter++;
}
else
robotCol++;
moveCounter++;
}
else System.out.print ("Sorry, I don't understand that"
+"\n"+"\n");
}
else System.out.print("Sorry, I don't understand that"+"\n"
+"\n");
}
// prints victory message and number of moves
System.out.print("Congratulations! Robot is free!"+"\n"
+"It took "+moveCounter+" moves."+"\n"+"\n");
// resets robot position and move counter back to original
robotRow = 0;
robotCol = 0;
moveCounter = 0;
}
else if (input==3){
System.out.print ("How big is the maze? (3-10) ");
scnr.nextLine();
if (scnr.hasNextInt()) {
mazeSize=scnr.nextInt();
scnr.nextLine();
// loop for maze size values that are out of bounds
while (mazeSize<3||mazeSize>10){
System.out.print("Must be between 3 and 10,"
+ " inclusive."+"\n"+"How big is the maze?"
+ " (3-10) ");
// reads new scnr int so that loop is not infinite
if (scnr.hasNextInt()){
mazeSize=scnr.nextInt();
scnr.nextLine();
}
// clears buffer, to prevent infinite loop
else scnr.nextLine();
}
// sets conditions for when maze size is within bounds
if (mazeSize>=3&&mazeSize<=10){
// initializes variables
exitRow = mazeSize-1;
robotCol = rng.nextInt(mazeSize);
exitCol = rng.nextInt(mazeSize);
int xCol = rng.nextInt(mazeSize);
int xRow = rng.nextInt(mazeSize-2)+1;
// starts loop for when robot is not at exit
while (robotRow!=exitRow||robotCol!=exitCol){
System.out.println("Help Robot (R) get to Exit (E)");
for (int row=0;row<mazeSize; row++){
for (int col=0;col<mazeSize; col++){
if (row==robotRow&&col==robotCol){
System.out.print("R ");
}
else if (row==exitRow&&col==exitCol){
System.out.print("E ");
}
else if (row==xRow&&col==xCol){
System.out.print("X ");
}
else System.out.print("o ");
}
System.out.print("\n");
}
// prints controls to the user
System.out.print("1. up"+"\n"+"2. down"+"\n"+"3. left"
+"\n"+"4. right"+"\n");
System.out.println("Move? ");
if (scnr.hasNextInt()) {
input=scnr.nextInt();
if (input==1){
scnr.nextLine();
// does not let robot move out of grid
if (robotRow==0){
moveCounter++;
}
// does not let robot go over obstacle
else if (robotRow==xRow+1&&robotCol==xCol){
moveCounter++;
}
else
robotRow--;
moveCounter++;
}
else if (input==2){
scnr.nextLine();
// does not let robot move out of grid
if (robotRow==mazeSize-1){
moveCounter++;
}
// does not let robot go over obstacle
else if (robotRow==xRow-1&&robotCol==xCol){
moveCounter++;
}
else
robotRow++;
moveCounter++;
}
else if (input==3){
scnr.nextLine();
// does not let robot move out of grid
if (robotCol==0){
moveCounter++;
}
// does not let robot go over obstacle
else if (robotCol==xCol+1&&robotRow==xRow){
moveCounter++;
}
else
robotCol--;
moveCounter++;
}
else if (input==4){
scnr.nextLine();
// does not let robot move out of grid
if (robotRow==mazeSize-1){
moveCounter++;
}
// does not let robot go over obstacle
else if (robotCol==xCol-1&&robotRow==xRow){
moveCounter++;
}
else
robotCol++;
moveCounter++;
}
else System.out.print ("Sorry, I don't understand"
+ " that"+"\n"+"\n");
}
else System.out.print("Sorry, I don't understand that"
+"\n"+"\n");
scnr.nextLine();
}
// prints victory message and number of moves
System.out.print("Congratulations! Robot is free!"+"\n"
+"It took "+moveCounter+" moves."+"\n"+"\n");
// resets robot position and move counter back to original
robotRow = 0;
robotCol = 0;
moveCounter = 0;
}
else System.out.print("Sorry, I don't understand that"
+"\n"+"\n");
}
else {System.out.print("Sorry, I don't understand that"+"\n"
+"\n");
scnr.nextLine();}
}
// EXIT command
else if (input==4){
System.out.print ("Live Long And Prosper!");
scnr.nextLine();
scnr.close();
System.exit(0);
}
// IGNORE UNRECOGNIZED command
else if (input<1){
System.out.print ("Sorry, I don't understand that"+"\n"+
"\n");
}
else if (input>4){
System.out.print ("Sorry, I don't understand that"+"\n"+
"\n");
}
// ELSE the input is incorrect type
// READ (SCAN) user input as a String
// Display bad type message to user
}
else {System.out.print ("Sorry, I don't understand that"+"\n"+"\n");
scnr.nextLine();}
// Close the scanner to avoid potential resource leak
}
} // end of main method
}
// end of MyMazeProgram class