-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrace.java
More file actions
391 lines (370 loc) · 9.98 KB
/
race.java
File metadata and controls
391 lines (370 loc) · 9.98 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Karishma Sinha 2018339
import java.util.*;
import java.io.*;
import java.lang.*;
//Inheritance
//polymorphism
//exception handling
//define own exceptions
public class race
{
public static abstract class tile
{
protected String bf;
//protected int toccur;
protected int x;
//protected int bite=0;
public abstract void message();
public int retx()
{
return x;
}
public String retbf()
{
return bf;
}
}
public static class Snake extends tile
{
public Snake(int _x)
{
//this.toccur=to;
this.x=_x;
this.bf="Backward";
}
@Override
public void message()
{
System.out.println(">>> Hiss...! I am a Snake, you can go back "+x+" tiles!");
}
}
public static class Vulture extends tile
{
public Vulture(int _x)
{
//this.toccur=to;
this.x=_x;
this.bf="Backward";
}
@Override
public void message()
{
System.out.println(">>> Yapping...! I am a Vulture, you can go back "+x+" tiles!");
}
}
public static class Cricket extends tile
{
public Cricket( int _x)
{
//this.toccur=to;
this.x=_x;
this.bf="Backward";
}
@Override
public void message()
{
System.out.println(">>> Chirp...! I am a Cricket, you can go back "+x+" tiles!");
}
}
public static class Trampoline extends tile
{
public Trampoline(int _x)
{
//this.toccur=to;
this.x=_x;
this.bf="Forward";
}
@Override
public void message()
{
//System.out.println(">>> ");
System.out.println(">>> PingPong! I am a Trampoline, you can advance "+x+" tiles!");
}
}
public static class White extends tile
{
public White()
{
//this.toccur=to;
this.x=0;
this.bf="None";
}
@Override
public void message()
{
System.out.print("I am a White tile!");
}
}
static class SnakeBiteException extends RuntimeException
{
public SnakeBiteException(String message)
{
super(message);
}
}
static class VultureBiteException extends RuntimeException
{
public VultureBiteException(String message)
{
super(message);
}
}
static class CricketBiteException extends RuntimeException
{
public CricketBiteException(String message)
{
super(message);
}
}
static class TrampolineException extends RuntimeException
{
public TrampolineException(String message)
{
super(message);
}
}
static class GameWinnerException extends RuntimeException
{
public GameWinnerException(String message)
{
super(message);
}
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
boolean gameOngoing=true;
System.out.println(">>> Enter total number of tiles on the racetrack (length)");
int totalTiles=input.nextInt();
System.out.println(">>> Setting up the race track...");
Random rand = new Random();
int ranS = rand.nextInt(totalTiles/4);
int ranC = rand.nextInt(totalTiles/4);
int ranV = rand.nextInt(totalTiles/4);
System.out.println(">>> Danger: There are "+ranS+", "+ranC+", "+ranV+" numbers of Snakes, Cricket, and Vultures respectively on your track!");
int ranT = rand.nextInt(totalTiles/4);
System.out.println(">>> Good News: There are "+ranT+" number of Trampolines on your track!");
int ranW=totalTiles-(ranS+ranC+ranV+ranT);
int xS=rand.nextInt(totalTiles/10)+1;
int xC=rand.nextInt(totalTiles/10)+1;
int xV=rand.nextInt(totalTiles/10)+1;
int xT=rand.nextInt(totalTiles/10)+1;
System.out.println(">>> Good News: Each Trampoline can help you advance by "+xT+" number of Tiles");
tile[] track= new tile[totalTiles];
int[] checkWhite= new int[totalTiles];
//white=1
//snake=2
//vulture=4
//cricket=3
//trampoline=5
for(int i=0;i<totalTiles;i++)
{
checkWhite[i]=1;
}
int assign=0;
while(assign<ranS)
{
int y=rand.nextInt(totalTiles);
if(checkWhite[y]==1)
{
track[y]=new Snake(xS);
assign++;
checkWhite[y]=2;
}
}
assign=0;
while(assign<ranC)
{
int y=rand.nextInt(totalTiles);
if(checkWhite[y]==1)
{
track[y]=new Cricket(xC);
assign++;
checkWhite[y]=3;
}
}
assign=0;
while(assign<ranV)
{
int y=rand.nextInt(totalTiles);
if(checkWhite[y]==1)
{
track[y]=new Vulture(xV);
assign++;
checkWhite[y]=4;
}
}
assign=0;
while(assign<ranT)
{
int y=rand.nextInt(totalTiles);
if(checkWhite[y]==1)
{
track[y]=new Trampoline(xT);
//track[y]=
assign++;
checkWhite[y]=5;
}
}
for(int i=0;i<totalTiles;i++)
{
if(checkWhite[i]==1)
{
track[i]=new White();
}
}
System.out.println(">>> Enter Player Name");
String name=input.next();
System.out.println(">>> Starting the game with "+name+" at Tile-1");
System.out.println(">>> Control transferred to Computer for rolling the Dice for "+name);
System.out.println(">>> Hit enter to start the game");
//find out enter fn
System.out.println(">>> Game Started ===================================>");
int croll=0;
int ctile=1;
int roll;
int tsb=0;
int tcb=0;
int tvb=0;
int tt=0;
while(gameOngoing)
{
roll=rand.nextInt(6)+1;
croll++;
if(ctile==1)
{
if(roll!=6)
{
System.out.println(">>>[Roll-"+croll+"]: "+name+" rolled "+roll+" at Tile-"+ctile+", OOPs you need 6 to start");
//croll++;
}
else
{
System.out.println(">>>[Roll-"+croll+"]: "+name+" rolled "+roll+" at Tile-"+ctile+". You are out of the cage! You get a free roll");
//croll++;
roll=rand.nextInt(6)+1;
croll++;
int newctile=ctile+roll;
System.out.println(">>>[Roll-"+croll+"]: "+name+" rolled "+roll+" at Tile-"+ctile+", landed on Tile "+newctile);
ctile+=roll;
}
}
else
{
int newctile=ctile+roll;
System.out.println(">>>[Roll-"+croll+"]: "+name+" rolled "+roll+" at Tile-"+ctile+", landed on Tile "+newctile);
ctile+=roll;
}
if(ctile!=1)
{
if(ctile>=totalTiles)
{
System.out.println(">>> "+name+" moved to Tile-"+totalTiles);
System.out.println(">>> "+name+" wins the race in "+croll+" rolls!");
System.out.println(">>> Total Snake Bites= "+tsb);
System.out.println(">>> Total Vulture Bites= "+tvb);
System.out.println(">>> Total Cricket Bites= "+tcb);
System.out.println(">>> Total Trampolines= "+tt);
gameOngoing=false;
//System.out.println()
}
else
{
System.out.println(">>> Trying to shake Tile-"+ctile);
//System.out.println(">>> ");
//System.out.println(track[ctile-1].retx());
if(track[ctile-1] instanceof Snake)
{
try
{
throw new SnakeBiteException(">>> Hiss...! I am a Snake, you can go back "+xS+" tiles!");
}
catch(RuntimeException e)
{
System.out.println(e.getMessage());
}
}
else if(track[ctile-1] instanceof Vulture)
{
try
{
throw new VultureBiteException(">>> Yapping...! I am a Vulture, you can go back "+xV+" tiles!");
}
catch(RuntimeException e)
{
System.out.println(e.getMessage());
}
}
else if(track[ctile-1] instanceof Cricket)
{
try
{
throw new CricketBiteException(">>> Chirp...! I am a Cricket, you can go back "+xC+" tiles!");
}
catch(RuntimeException e)
{
System.out.println(e.getMessage());
}
}
else if(track[ctile-1] instanceof Trampoline)
{
try
{
throw new TrampolineException(">>> PingPong! I am a Trampoline, you can advance "+xT+" tiles!");
}
catch(RuntimeException e)
{
System.out.println(e.getMessage());
}
}
track[ctile-1].message();
if(checkWhite[ctile-1]==2)
tsb++;
else if(checkWhite[ctile-1]==3)
tcb++;
else if(checkWhite[ctile-1]==4)
tvb++;
else if(checkWhite[ctile-1]==5)
tt++;
if(track[ctile-1].retbf().equals("Backward"))
{
if(track[ctile-1].retx()>=ctile)
{
ctile=1;
}
else
{
ctile-=track[ctile-1].retx();
}
//ctile-=track[ctile-1].retx();
}
else
{
ctile+=track[ctile-1].retx();
}
if(ctile>=totalTiles)
{
System.out.println(">>> "+name+" moved to Tile-"+totalTiles);
System.out.println(">>> "+name+" wins the race in "+croll+" rolls!");
System.out.println(">>> Total Snake Bites= "+tsb);
System.out.println(">>> Total Vulture Bites= "+tvb);
System.out.println(">>> Total Cricket Bites= "+tcb);
System.out.println(">>> Total Trampolines= "+tt);
gameOngoing=false;
//System.out.println()
try
{
throw new GameWinnerException(">>> "+name+" wins the race in "+croll+" rolls!");
}
catch(RuntimeException e)
{
System.out.println(e.getMessage());
}
}
else
System.out.println(">>> "+name+" moved to Tile-"+ctile);
}
}
}
}
}