-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrace2.0.java
More file actions
504 lines (474 loc) · 12.9 KB
/
race2.0.java
File metadata and controls
504 lines (474 loc) · 12.9 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// 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 class Player implements Serializable
{
private int ptotaltiles;
private String pname;
private tile[] ptrack;
private int pct=0;
public Player(String pn, int ptt, tile[] pt)
{
this.pname=pn;
this.ptotaltiles=ptt;
this.ptrack=new tile[this.ptotaltiles];
for(int i=0;i<ptotaltiles;i++)
{
this.ptrack[i]=pt[i];
}
}
public int getpct()
{
return this.pct;
}
public String getpname()
{
return pname;
}
public void setpct(int ct)
{
this.pct=ct;
}
}
public static void main(String[] args) throws Exception
{
Scanner input=new Scanner(System.in);
ArrayList players=new ArrayList(Player)<>;
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();
boolean found =false;
for(int i=0;i<players.size();i++)
{
//System.out.println(M.get(i).getmcode() + M.get(i).getmname());
if(players.get(i).getpname().equals(name))
{
found=true;
FileInputStream fis=new FileInputStream(f);
ObjectInputStream ois=new ObjectInputStream(fis);
Player p=(Player)ois.readObject();
}
}
if(found==false)
{
Player p=new Player(name,totalTiles,track);
players.add(p);
}
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;
boolean pass25=false;
boolean pass50=false;
boolean pass75=false;
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);
if(ctile>=.25*totalTiles&&pass25==false)
{
pass25=true;
System.out.println(">>> 1. Continue");
System.out.println(" 2. Save");
int cos=input.nextInt();
if(cos==2)
{
//players.add(name);
p.setpct(ctile);
File f=new File("gamestatus.txt");
FileOutputStream fos=new FileOutputStream(f);
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(p);
gameOngoing=false;
break;
}
}
else if(ctile>=.50*totalTiles&&pass50==false)
{
pass50=true;
System.out.println(">>> 1. Continue");
System.out.println(" 2. Save");
int cos=input.nextInt();
if(cos==2)
{
//players.add(name);
p.setpct(ctile);
File f=new File("gamestatus.txt");
FileOutputStream fos=new FileOutputStream(f);
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(p);
gameOngoing=false;
break;
}
}
else if(ctile>=.75*totalTiles&&pass75==false)
{
pass75=true;
System.out.println(">>> 1. Continue");
System.out.println(" 2. Save");
int cos=input.nextInt();
if(cos==2)
{
//players.add(name);
p.setpct(ctile);
File f=new File("gamestatus.txt");
FileOutputStream fos=new FileOutputStream(f);
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(p);
gameOngoing=false;
break;
}
}
}
}
}
}
}