-
Notifications
You must be signed in to change notification settings - Fork 0
/
Homework04.java
236 lines (233 loc) · 6.16 KB
/
Homework04.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
/*
* Created by Noah Shaw
*/
import java.util.Scanner;
import java.util.Random;
public class Homework04 {
public static final int BoardSize = 10;
public static void main(String[] args) {
// TODO Auto-generated method stub
//Prompt the user
System.out.println("Welcome to the Mine Walker! Get to the ice cream and avoid the mines!");
Scanner keyboard = new Scanner (System.in);
//Initialize the movement variables
int movementX = 0;
int movementY = 0;
int x = 0;
int y = 0;
//Initialize the mine variables
int minesX = 0;
int minesY = 0;
//Define the location of the ice cream
Random rand = new Random();
int icecreamX = rand.nextInt(BoardSize);
int icecreamY = rand.nextInt(BoardSize);
//Construct the board
String[][] space = new String[BoardSize][BoardSize];
//Define the amount of mines
int amountOfMines = (BoardSize*BoardSize)/10;
//Place the mines on the board
for(int i = 0; i < amountOfMines; i++)
{
minesX = rand.nextInt(BoardSize);
minesY = rand.nextInt(BoardSize);
if(space[minesX][minesY] != null)
{
while(space[minesX][minesY] != null)
{
minesX = rand.nextInt(BoardSize);
minesY = rand.nextInt(BoardSize);
}
}
space[minesX][minesY] = "-5";
}
boolean gameOver = false;
//Game loop
while(!gameOver)
{
//Prompt the user to input their x direction movement
//Draw the new board
for(int i = 0; i < space.length; i++)
{
for(int j = 0; j < space.length; j++)
{
if(j == x && i == y)
{
System.out.print("X");
}
else if(j == icecreamX && i == icecreamY)
{
System.out.print("^");
}
else
{
System.out.print("_");
}
}
System.out.println("");
}
//Prompt the user to input their x direction movement
System.out.println("Enter either a -1, 0, or 1 to move in the x direction or 9 to quit.");
movementX = keyboard.nextInt();
if(movementX == 9)
{
System.out.println("Thanks for playing!");
System.exit(0);
}
else if(movementX == -1)
{
x = x - 1;
}
else if(movementX == 1)
{
x = x + 1;
}
else if(movementX == 0)
{
x = x + 0;
}
else if(movementX > 9)
{
System.out.println("Invalid input. Not moving in the x direction.");
x = x + 0;
}
else if(movementX > 1)
{
System.out.println("Invalid input. Not moving in the x direction.");
x = x + 0;
}
else if(movementX < -1)
{
System.out.println("Invalid input. Not moving in the x direction.");
x = x + 0;
}
//Prompt the user to input their y direction movement
System.out.println("Enter either a -1, 0, or 1 to move in the y direction or 9 to quit.");
movementY = keyboard.nextInt();
if(movementY == 9)
{
System.out.println("Thank you for playing!");
System.exit(0);
}
else if(movementY == -1)
{
y = y - 1;
}
else if(movementY == 1)
{
y = y + 1;
}
else if(movementY == 0)
{
y = y + 0;
}
else if(movementY > 9)
{
System.out.println("Invalid input. Not moving in the y direction.");
y = y + 0;
}
else if(movementY > 1)
{
System.out.println("Invalid input. Not moving in the y direction.");
y = y + 0;
}
else if(movementY < -1)
{
System.out.println("Invalid input. Not moving in the y direction.");
y = y + 0;
}
//Check to see if the user's position is where a mine is
if(space[x][y] == "-5")
{
System.out.println("Boom! You hit a mine!");
//Determine if the player wants to play again
System.out.println("Would you like to play again? Yes or no?");
String playAgain = keyboard.nextLine(); //Capture extra space
playAgain = keyboard.nextLine();
//Play again = true, reset game
if(playAgain.equalsIgnoreCase("yes"))
{
System.out.println("Good luck!");
space = new String[BoardSize][BoardSize];
movementX = 0;
movementY = 0;
x = 0;
y = 0;
icecreamX = rand.nextInt(BoardSize);
icecreamY = rand.nextInt(BoardSize);
for(int i = 0; i < amountOfMines; i++)
{
minesX = rand.nextInt(BoardSize);
minesY = rand.nextInt(BoardSize);
if(space[minesX][minesY] != null)
{
while(space[minesX][minesY] != null)
{
minesX = rand.nextInt(BoardSize);
minesY = rand.nextInt(BoardSize);
}
}
space[minesX][minesY] = "-5";
}
}
//Else end the game
else if(playAgain.equalsIgnoreCase("no"))
{
System.out.println("Thanks for playing!");
System.exit(0);
}
else
{
System.out.println("Invalid response. You will not play again.");
System.exit(0);
}
}
//Check to see if the user's position is where the ice cream is
else if(x == icecreamX && y == icecreamY)
{
System.out.println("You got the icecream!");
//Determine if the player wants to play again
System.out.println("Would you like to play again? Yes or no?");
String playAgain = keyboard.nextLine(); //Capture extra space
playAgain = keyboard.nextLine();
//Play again = true, reset game
if(playAgain.equalsIgnoreCase("yes"))
{
System.out.println("Good luck!");
space = new String[BoardSize][BoardSize];
movementX = 0;
movementY = 0;
x = 0;
y = 0;
icecreamX = rand.nextInt(BoardSize);
icecreamY = rand.nextInt(BoardSize);
for(int i = 0; i < amountOfMines; i++)
{
minesX = rand.nextInt(BoardSize);
minesY = rand.nextInt(BoardSize);
if(space[minesX][minesY] != null)
{
while(space[minesX][minesY] != null)
{
minesX = rand.nextInt(BoardSize);
minesY = rand.nextInt(BoardSize);
}
}
space[minesX][minesY] = "-5";
}
}
//Else end the game
else if(playAgain.equalsIgnoreCase("no"))
{
System.out.println("Thanks for playing!");
System.exit(0);
}
else
{
System.out.println("Invalid response. You will not play again.");
System.exit(0);
}
}
}
}
}