forked from Sparows/AP-Assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.java
363 lines (297 loc) · 12.2 KB
/
game.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package assignment4;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
class CreatedException extends Exception {
public CreatedException(String s) {
super(s);
}
}
class CreatedException2 extends Exception {
public CreatedException2(String s) {
super(s);
}
}
class bucket {
private int i = 0;
private ArrayList<String> buck = new ArrayList<String>();
public void addItems(String s) {
buck.add(s);
}
public void getItems() {
for (i = 0; i < buck.size(); i++) {
System.out.println(buck.get(i) + "\n");
}
}
}
class randomString {
private String auc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String alc = "abcdefghijklmnopqrstuvwxyz";
private String allCharacters = alc + auc;
public String getrString() {
StringBuffer randomString = new StringBuffer();
int count = 0;
while (count < 4) {
int randomIndex = (int) (Math.random() * allCharacters.length());
randomString.append(allCharacters.charAt(randomIndex));
count++;
}
return randomString.toString();
}
}
class random {
int min;
int max;
int rand;
random(int Max, int Min) {
min = Min;
max = Max;
}
public int get_random() {
rand = (int) Math.floor(Math.random() * (max - min + 1) + min);
return rand;
}
}
class player {
private int jumps = 5;
private bucket b = new bucket();
private int tile_num;
public void printbucket() {
b.getItems();
}
public void update(String s) {// updates entire player stat;
if (jumps > 0) {
jumps--;
if (s.length() > 0) {
b.addItems(s);
}
}
}
public void settile(int i) {
tile_num = i;
}
public void play() {
// tile_num = rand.get_random();
System.out.println(tile_num + 1); // rand num is diff from tile num
}
public int get_tilenum() {
return tile_num;
}
}
class softToys implements Cloneable {
private String name;
public void setname(String s) {
name = s;
}
public String getname() {
return name;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class TileCarpet implements Cloneable {
private ArrayList<softToys> carpet = new ArrayList<softToys>();// composition and array index is tile number
// private softToys st = new softToys();
public void addtoys(String s) {
softToys st = new softToys();
st.setname(s);
carpet.add(st);
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public softToys getToy(int i) throws CloneNotSupportedException {// i is index for array list or tile num
// softToys sft;
return (softToys) carpet.get(i).clone();
// modifications needed for object cloning
}
// public Object clone() throws CloneNotSupportedException {
// return super.clone();
// }
}
class genCalc<T> {
T ob1;
T ob2;
// T result;
genCalc(T a, T b) {// basically it generates 2 random numbers and then prints them out
ob1 = a;
ob2 = b;
}
public int div() {
try {
return ((int) ob1 / (int) ob2);
} catch (ArithmeticException e) {
System.out.println("System Fault !! Zero Division Error \nYou may press -1 to win the prize!!");
}
return -1;
}
public String cat() {
String s = (String) ob1 + (String) ob2;
return s;
}
// public T geta() {
// return (ob1);
// }
// public T getb() {
// return (ob2);
// }
}
public class game {
public static void main(String[] args) throws CloneNotSupportedException {
TileCarpet tc = new TileCarpet();
player p = new player();
random r2 = new random(4000, -4000);
randomString rs = new randomString();
Scanner scan = new Scanner(System.in);
random r1 = new random(25, 0);
// hardcode
tc.addtoys("Teddie");
tc.addtoys("Winnie the Pooh");
tc.addtoys("Elephant");
tc.addtoys("Tiger");
tc.addtoys("Mickey Mouse");
tc.addtoys("Donald Duck");
tc.addtoys("Cat");
tc.addtoys("Black Cat");
tc.addtoys("Bear");
tc.addtoys("White Bear");
tc.addtoys("Rhinoceros");
tc.addtoys("Eagle");
tc.addtoys("Pigeon");
tc.addtoys("Deer");
tc.addtoys("Peakcock");
tc.addtoys("Dog");
tc.addtoys("Dolphin");
tc.addtoys("Whale");
tc.addtoys("Cow");
tc.addtoys("Tweety");
// System.out.println(tc.carpet.size());
for (int i = 0; i < 5; i++) {
int flg2 = 1;
while (flg2 == 1) {
System.out.println("\nHit Enter for your " + (i + 1) + "th hop:");
int flg1 = 1;
String sup = "-1";
while (flg1 == 1) {
try {
sup = scan.nextLine();
flg1 = 0;
} catch (InputMismatchException e) {
System.err.println("INPUT ERROR");
}
}
if (sup.equals("")) {
p.settile(r1.get_random());
// p.play();
if (p.get_tilenum() > 19) {
System.out
.println("You are too energetic and zoomed past all the tiles. Muddy Puddle Splash!\n");
} else {
if (p.get_tilenum() % 2 == 0) {// even here means odd in reality
int f = 1;
// System.out.println("check1 ");
while (f == 1) {
System.out.println("You landed on Tile " + (p.get_tilenum() + 1));
System.out.println("Question answer round. Integer or strings?\n");
String quest = scan.next();
if (quest.equals("string")) {
// flow to tilecarpet
String s1 = rs.getrString();
String s2 = rs.getrString();
System.out.println(s1 + " " + s2 + "\n");
genCalc<String> genStr = new genCalc(s1, s2);
String result = genStr.cat();
String input = scan.next();
if (input.equals(result)) {
// gives reward;
softToys sft;
try {
sft = tc.getToy(p.get_tilenum());
} catch (CloneNotSupportedException e) {
System.out.println("Unable to Create Clone Object !!");
}
sft = tc.getToy(p.get_tilenum());
p.update(sft.getname());
// System.out.println("You landed on Tile " + (p.get_tilenum() + 1) + "\n");
// softToys sft = tc.getToy(p.get_tilenum());
System.out.println("You won a " + sft.getname() + " Soft toy");
} else {
// bhago u lost
System.out.println("Incorrect answer");
System.out.println("You did not win any soft toy");
}
f = 0;
} else if (quest.equals("integer")) {// random int generation wala
// flow to tilecarpet
int a = r2.get_random();
int b = r2.get_random();
System.out.println(
"Calculate Integer Division of " + a + " Divided by " + b + " :\n");
genCalc<Integer> genInt = new genCalc(a, b);
int result = genInt.div();
int ip = 0;//
int flg4 = 1;
while (flg4 == 1) {
try {
int input = scan.nextInt();
ip = input;
flg4 = 0;
} catch (InputMismatchException e) {
scan.next();
System.err.println("\nINPUT MISMATCH ERROR!! Please enter valid input!\n");
}
}
if (ip == result) {
// gives reward;
softToys sft;
try {
sft = tc.getToy(p.get_tilenum());
} catch (CloneNotSupportedException e) {
System.out.println("Unable to Create Clone Object !!");
}
sft = tc.getToy(p.get_tilenum());
p.update(sft.getname());
System.out.println("You won a " + sft.getname() + " Soft toy");
// p.update(sft.getname());
} else {
try {
throw new CreatedException2("CreatedException2.0");
} catch (CreatedException2 e) {
System.out.println(
"\nINCORRECT ANSWER EXCEPTION !! Incorrect Answer \n\nYou didn't win any Soft toys");
}
}
f = 0;
} else {
try {
throw new CreatedException("CreatedException");
} catch (CreatedException e) {
System.out.println("\nINVALID INPUT EXCEPTION !! \n\nEnter valid Input:");
}
}
}
} else {
// free
System.out.println("You landed on Tile " + (p.get_tilenum() + 1) + "\n");
softToys sft;
try {
sft = tc.getToy(p.get_tilenum());
} catch (CloneNotSupportedException e) {
System.out.println("Unable to Create Clone Object !!");
}
sft = tc.getToy(p.get_tilenum());
System.out.println("You won a " + sft.getname() + " Soft toy");
p.update(sft.getname());
// tc.getToy(p.get_tilenum());// clonable wala kaam and uske baad update play
}
}
flg2 = 0;
}
}
}
System.out.println("Game Over");
System.out.println("Soft toys won by you are:\n");
p.printbucket();
}
}