-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2018145_lab6.java
More file actions
715 lines (645 loc) · 20.1 KB
/
2018145_lab6.java
File metadata and controls
715 lines (645 loc) · 20.1 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
package off;
import java.util.*;
import java.io.*;
/*abstract result{
public String positive(String name,int t);
public String negative(String name,int t,ArrayList<tile> tiles,int i);
public void pingpong(String name,int t,ArrayList<tile> tiles);
public void white(String name,int t);
}*/
class checking implements Serializable{
private final String filename;
private int t25;
private int t50;
private int t75;
private int no;
private int alltile;
private int roll;
private int tile;
private int[] arr=new int[alltile];
private String name;
private int count1;
private int count2;
private int count3;
private int count4;
checking(int alltile,int roll,int tile,String name,int count1,int count2,int count3,int count4,int arr[],String filename){
this.roll=roll;
this.tile=tile;
this.name=name;
this.count1=count1;
this.count2=count2;
this.count3=count3;
this.count4=count4;
this.arr=arr;
this.alltile=alltile;
this.filename=filename;
}
public String getfile() {
return filename;
}
public void setno(int no) {
this.no=no;
}
public void setroll(int roll) {
this.roll=roll;
}
public void settile(int tile) {
this.tile=tile;
}
public void setname(String name) {
this.name=name;
}
public void setcount1(int count1) {
this.count1=count1;
}
public void setcount2(int count2) {
this.count2=count2;
}
public void setcount3(int count3) {
this.count3=count3;
}
public void setcount4(int count4) {
this.count4=count4;
}
public void setalltile(int alltile) {
this.alltile=alltile;
}
public void setarr(int arr[]) {
this.arr=arr;
}
public int getroll() {
return roll;
}
public int gettile() {
return tile;
}
public String getname() {
return name;
}
public int getcount2() {
return count2;
}
public int getcount3() {
return count3;
}
public int getcount4() {
return count4;
}
public int getcount1() {
return count1;
}
public int getalltile() {
return alltile;
}
public int[] getarr() {
return arr;
}
public int getno() {
return no;
}
/////////////////////////////////////////////
public void setc1(int alltile) {
t25=(int)alltile/4;
}
public void setc2(int alltile) {
t50=(int)alltile/2;
}
public void setc3(int alltile) {
t75=(int)(3*alltile/4);
}
public int getc1() {
return t25;
}
public int getc2() {
return t50;
}
public int getc3() {
return t75;
}
}
class snakebiteexception extends Exception{
public snakebiteexception(String message) {
super(message);
}
}
class cricketbiteexception extends Exception{
public cricketbiteexception(String message) {
super(message);
}
}
class vulturebiteexception extends Exception{
public vulturebiteexception(String message) {
super(message);
}
}
class trampolineexception extends Exception{
public trampolineexception(String message) {
super(message);
}
}
class gamewinnerexception extends Exception{
public gamewinnerexception(String message) {
super(message);
}
}
class tile implements Serializable{
private int steps;
private int no;
private final int number;
tile(int number,int no){
this.no=no;
this.number=number;
}
public void positive(String name,int t) {
t=1;
System.out.println(name+" MOVED TO TILE 1 AS IT CAN'T GO BACK FURTHER");
}
public void positive(String name,int t,ArrayList<tile> arr,int i) {
//t=t-arr.get(i).getsteps();
System.out.println(name+" MOVED TO TILE-"+t);
}
public void setno(int no) {
Random rand=new Random();
no=rand.nextInt(no)+1;
this.no=no;
}
public void setsteps(int steps) {
Random rand=new Random();
steps=rand.nextInt(10)+1;
this.steps=steps;
}
public int getnumber() {
return number;
}
public int getno() {
return no;
}
public int getsteps() {
return steps;
}
}
class snake extends tile{
snake(int number,int steps){
super(number,steps);
}
}
class vultures extends tile{
vultures(int number,int steps){
super(number,steps);
}
}
class cricket extends tile{
private int steps;
cricket(int number,int steps){
super(number,steps);
}
}
class trampoline extends tile{
trampoline(int number,int steps){
super(number,steps);
}
}
public class lab6 {
static tile tl;
public static void main(String[] args) {
ArrayList<checking> arr1=new ArrayList<checking>();
Scanner s=new Scanner(System.in);
Random rand = new Random();
int q1=0;
int arr[]= {};
String filename=null;
String name=null;
int check=0;
int t25=0;
int t50=0;
int t75=0;
int v1=1;
int v2=1;
int v3=1;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int tile=1;
int roll=0;
int cout=0;
Boolean t1=true;
Boolean t2=true;
Boolean result=true;
int snakebite=0;
int cricketbite=0;
int vulturebite=0;
int trampolines=0;
int race_track;
ArrayList<tile> tiles=new ArrayList<tile>();
ArrayList<tile> tile_str=new ArrayList<tile>();
while(t1!=false) {
Boolean done=false;
result=true;
System.out.println("1) FOR STARTING NEW GAME");
System.out.println("2) FOR LOADING LAST GAME");
int decision=s.nextInt();
while(!done) {
if(decision==1) {
decision=0;
System.out.println("ENTER TOTAL NO. OF TILES ON THE RACE TRACK(LENGTH)");
q1=s.nextInt();
v1=v2=v3=1;
arr=new int[q1+1];
t25=(int)q1/4;
t50=(int)q1/2;
t75=(int)(3*(q1/4));
//arr[]=new int[q1];
try {
race_track=rand.nextInt(q1)+1;
done=true;
snake snake=new snake(1,race_track);
tl=new tile(1,race_track);
tile_str.add(tl);
tiles.add(snake);
tiles.get(0).setno(race_track);
tiles.get(0).setsteps(10);
done=true;
cricket cricket= new cricket(2,race_track-tiles.get(0).getno());
tl=new tile(2,race_track-tiles.get(0).getno());
tile_str.add(tl);
tiles.add(cricket);
tiles.get(1).setno(race_track-tiles.get(0).getno());
tiles.get(1).setsteps(10);
done=true;
vultures vultures=new vultures(3,race_track-tiles.get(0).getno()-tiles.get(1).getno());
tl=new tile(3,race_track-tiles.get(0).getno()-tiles.get(1).getno());
tile_str.add(tl);
tiles.add(vultures);
tiles.get(2).setno(race_track-tiles.get(0).getno()-tiles.get(1).getno());
tiles.get(2).setsteps(10);
done=true;
trampoline trampoline=new trampoline(4,race_track-tiles.get(0).getno()-tiles.get(1).getno()-tiles.get(2).getno());
tl=new tile(4,race_track-tiles.get(0).getno()-tiles.get(1).getno()-tiles.get(2).getno());
tile_str.add(tl);
tiles.add(trampoline);
tiles.get(2).setno(race_track-tiles.get(0).getno()-tiles.get(1).getno()-tiles.get(2).getno());
tiles.get(2).setsteps(10);
done=true;
int white_tiles=q1-(tiles.get(0).getno()+tiles.get(1).getno()+tiles.get(2).getno()+tiles.get(3).getno());
done=true;
}
catch(IllegalArgumentException ill){
System.out.println("INPUT NOT VERIFY");
System.out.println("TRY AGAIN");
t1=false;
break;
}
System.out.println("SETTING UP THE RACE TRACK....");
for(int i=1;i<=q1;i++) {
int no=rand.nextInt(4)+1;
if(no==1&&count1<tiles.get(0).getno()) {
arr[i]=1;
count1+=1;
}
else if(no==2&&count2<tiles.get(1).getno()) {
arr[i]=2;
count2+=1;
}
else if(no==3&&count3<tiles.get(2).getno()) {
arr[i]=3;
count3+=1;
}
else if(no==4&&count4<tiles.get(3).getno()) {
arr[i]=4;
count4+=1;
}
else {
arr[i]=5;
}
}
System.out.println("DANGER...THERE ARE "+tiles.get(0).getno()+", "+tiles.get(1).getno()+", "+tiles.get(2).getno()+", "+"SNAKES, CRICKET AND VULTURES RESPECTIVELY ON YOU TRACK!");
System.out.println("DANGER...EACH SNAKE, CRICKET AND VULTURE CAN THROW YOU BACK BY "+tiles.get(0).getsteps()+", "+tiles.get(1).getsteps()+", "+tiles.get(2).getsteps()+", "+"NO. OF TILES RESPECTIVELY!");
System.out.println("GOOD NEWS...THERE ARE "+tiles.get(3).getno()+" NUMBER OF TRAMPOLINES ON YOUR TRACK!");
System.out.println("GOOD NEWS...EACH TRAMPOLINES CAN HELP YOU ADVANCED BY "+tiles.get(3).getsteps()+" NUMBER OF TILES");
System.out.println("ENTER THE PLAYER NAME");
name=s.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");
//System.out.println("GAME STARTED.................");
}
if(decision==2) {
v1=v2=v3=1;
System.out.println("ENTER THE PLAYER NAME");
String nm=s.next();
for(int i=0;i<arr1.size();i++) {
if(arr1.get(i).getname().contentEquals(nm))
filename=arr1.get(i).getfile();
}
checking object1 = null;
// Deserialization
try
{
// Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
object1 = (checking)in.readObject();
in.close();
file.close();
t25=object1.getc1();
t50=object1.getc2();
t75=object1.getc3();
roll=object1.getroll();
tile=object1.gettile();
arr=object1.getarr();
q1=object1.getalltile();
System.out.println("Object has been deserialized ");
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
decision=0;
}
//System.out.println("HIT ENTER TO START THE GAME");
if(result!=false) {
System.out.println("GAME STARTED.................");
t2=true;
//v1=v2=v3=1;
while(tile<q1&&t2!=false) {
int dice=rand.nextInt(6)+1;
roll+=1;
if(q1-tile==dice) {
try {
throw new gamewinnerexception("EXCEPTION");
}
catch(gamewinnerexception e) {
System.out.println("Caught");
System.out.println(e.getMessage());
}
System.out.println("[ROLL-"+roll+"]: "+name+" ROLLED "+dice+" AT TILE-"+tile);
System.out.println(name+" WINS THE RACE IN "+roll+" ROLLS");
System.out.println("TOTAL SNAKEBITES:"+snakebite);
System.out.println("TOTAL CRICKETBITES:"+cricketbite);
System.out.println("TOTAL VULTUREBITES:"+vulturebite);
System.out.println("TOTAL TRAMPOLINES:"+trampolines);
t2=result=false;
break;
}
else if(dice>q1-tile) {
System.out.println("[ROLL-"+roll+"]: "+name+" ROLLED "+dice+" AT TILE-"+tile+", ");
}
else if(dice==6&&tile==1) {
System.out.println("[ROLL-"+roll+"]: "+name+" ROLLED "+dice+" AT TILE-"+tile+", "+"YOU ARE OUT OF THE CAGE! YOU GET A FREE ROLL");
tile=tile+1;
cout=cout+1;
}
else if(dice!=6&&tile==1){
System.out.println("[ROLL-"+roll+"]: "+name+" ROLLED "+dice+" AT TILE-"+tile+", OOPS YOU NEED 6 TO START");
}
else if(tile>1&&tile<q1) {
if(cout==1) {
tile=tile-1;
cout=0;
}
if(q1-tile==dice) {
try {
throw new gamewinnerexception("EXCEPTION");
}
catch(gamewinnerexception e) {
System.out.println("Caught");
System.out.println(e.getMessage());
}
//System.out.println("[ROLL-"+roll+"]: "+name+" ROLLED "+dice+" AT TILE-"+tile);
System.out.println(name+" WINS THE RACE IN "+roll+" ROLLS");
System.out.println("TOTAL SNAKEBITES:"+snakebite);
System.out.println("TOTAL CRICKETBITES:"+cricketbite);
System.out.println("TOTAL VULTUREBITES:"+vulturebite);
System.out.println("TOTAL TRAMPOLINES:"+trampolines);
t2=result=false;
break;
}
else if(tile<q1) {
System.out.println("[ROLL-"+roll+"]: "+name+" ROLLED "+dice+" AT TILE-"+tile+", ");
}
tile+=dice;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*checking checking=new checking(q1,roll,tile,name,count1,count2,count3,count4,arr);
String filename = "file.ser";
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(checking);
out.close();
file.close();
System.out.println("Object has been serialized");
}
catch(IOException ex)
{
System.out.println("IOException is caught");
} */
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
System.out.print(" YOU LANDED ON TILE-"+tile+" ");
if(tile>t25-1&&v1==1) {
System.out.println("####CHECKPOINT FIRST####");
System.out.println("1) CONTINUE");
System.out.println("2) SAVE");
int no=s.nextInt();
v1=0;
if(no==2) {
check=1;
System.out.println("ENTER FILENAME");
String f=s.next();
filename=f;
checking checking=new checking(q1,roll,tile,name,count1,count2,count3,count4,arr,f);
arr1.add(checking);
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(checking);
out.close();
file.close();
System.out.println("Object has been serialized");
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
finally {
v1=0;
t2=false;
break;
}
}
}
else if(tile>t50-1&&v2==1) {
System.out.println("####CHECKPOINT SECOND####");
System.out.println("1) CONTINUE");
System.out.println("2) SAVE");
int no=s.nextInt();
v2=0;
if(no==2) {
check=1;
System.out.println("ENTER FILENAME");
String f=s.next();
filename=f;
checking checking=new checking(q1,roll,tile,name,count1,count2,count3,count4,arr,f);
arr1.add(checking);
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(checking);
out.close();
file.close();
System.out.println("Object has been serialized");
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
finally {
v2=0;
t2=false;
break;
}
}
//v2=0;
}
else if(tile>t75-1&&v3==1) {
System.out.println("####CHECKPOINT THIRD####");
System.out.println("1) CONTINUE");
System.out.println("2) SAVE");
int no=s.nextInt();
v3=0;
if(no==2) {
check=1;
System.out.println("ENTER FILENAME");
String f=s.next();
filename=f;
checking checking=new checking(q1,roll,tile,name,count1,count2,count3,count4,arr,f);
arr1.add(checking);
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(checking);
out.close();
file.close();
System.out.println("Object has been serialized");
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
finally {
v3=0;
t2=false;
break;
}
}
}
System.out.println(" TRYING TO SHAKE THE TILE-"+tile);
if(arr[tile]==1) {
snakebite+=1;
try {
throw new snakebiteexception("EXCEPTION");
}
catch(snakebiteexception e) {
System.out.println("Caught");
System.out.println(e.getMessage());
}
System.out.println("HISS....! I AM A SNAKE, YOU GO BACK "+tiles.get(0).getsteps()+" TILE");
if(tile-tiles.get(0).getsteps()<1) {
tile=1;
//System.out.println(name+" MOVED TO TILE 1 AS IT CAN'T GO BACK FURTHER");
tl.positive(name, tile);
}
else {
tile=tile-tiles.get(0).getsteps();
//System.out.println(name+" MOVED TO TILE-"+tile);
tl.positive(name,tile,tiles,0);
}
}
if(arr[tile]==2) {
cricketbite+=1;
try {
throw new cricketbiteexception("EXCEPTION");
}
catch(cricketbiteexception e) {
System.out.println("Caught");
System.out.println(e.getMessage());
}
System.out.println("CHIRP....! I AM A CRICKET, YOU GO BACK "+tiles.get(1).getsteps()+" TILE");
if(tile-tiles.get(1).getsteps()<1) {
tile=1;
//System.out.println(name+" MOVED TO TILE 1 AS IT CAN'T GO BACK FURTHER");
tl.positive(name, tile);
}
else {
tile=tile-tiles.get(1).getsteps();
/*System.out.println(name+" MOVED TO TILE-"+tile);*/
tl.positive(name,tile,tiles,1);
}
}
if(arr[tile]==3) {
vulturebite+=1;
try {
throw new vulturebiteexception("EXCEPTION");
}
catch(vulturebiteexception e) {
System.out.println("Caught");
System.out.println(e.getMessage());
}
System.out.println("YAPPING....! I AM A VULTURE, YOU GO BACK "+tiles.get(2).getsteps()+" TILE");
if(tile-tiles.get(2).getsteps()<1) {
tile=1;
//System.out.println(name+" MOVED TO TILE 1 AS IT CAN'T GO BACK FURTHER");
tl.positive(name, tile);
}
else {
tile=tile-tiles.get(2).getsteps();
//System.out.println(name+" MOVED TO TILE-"+tile);
tl.positive(name,tile,tiles,2);
}
}
if(arr[tile]==4) {
trampolines+=1;
try {
throw new trampolineexception("EXCEPTION");
}
catch(trampolineexception e) {
System.out.println("Caught");
System.out.println(e.getMessage());
}
System.out.println("PINGPONG....! I AM A TRAMPOLINE, YOU ADVANCED "+tiles.get(3).getsteps()+" TILE");
tile=tile+tiles.get(3).getsteps();
System.out.println(name+" MOVED TO TILE-"+tile);
}
if(arr[tile]==5) {
System.out.println("I AM A WHITE TILE ");
System.out.println(name+" MOVED TO TILE-"+tile);
}
}
}
}
}
}
}
}