-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.java
632 lines (468 loc) · 19.1 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
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
import java.util.ArrayList;
import java.util.Random;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
class Binocular {
public int searchLotus(int xPoint , int yPoint, Grid lakeNozama) {
if (xPoint != 0 && lakeNozama.grid[xPoint - 1][yPoint][0] instanceof Lotus && ((Lotus)lakeNozama.grid[xPoint - 1][yPoint][0]).hasPetals()) {
return 0;
} else if (xPoint != 10 && lakeNozama.grid[xPoint+1][yPoint][0] instanceof Lotus && ((Lotus)lakeNozama.grid[xPoint+1][yPoint][0]).hasPetals()) {
return 1;
} else if (yPoint != 0 && lakeNozama.grid[xPoint][yPoint-1][0] instanceof Lotus && ((Lotus)lakeNozama.grid[xPoint][yPoint-1][0]).hasPetals()){
return 2;
} else if (yPoint != 10 && lakeNozama.grid[xPoint][yPoint +1][0] instanceof Lotus && ((Lotus)lakeNozama.grid[xPoint][yPoint+1][0]).hasPetals()) {
return 3;
} else {
return 4;
}
}
}
class Fin {
}
class Node {
private Object object;
private int xPoint;
private int yPoint;
private ArrayList<String> warriorRoute = new ArrayList<String>();
public Node(int x, int y ,Object object){
xPoint = x;
yPoint = y;
this.object = object;
}
public void setPointX(int x){
xPoint = x;
String route = "(" + Integer.toString(this.xPoint) + "," + Integer.toString(this.yPoint) + ")";
warriorRoute.add(route);
}
public void setPointY(int y){
yPoint = y;
String route = "(" + Integer.toString(this.xPoint) + "," + Integer.toString(this.yPoint) + ")";
warriorRoute.add(route);
}
public String getTheRoute() {
String wRoute = "";
for (int j = 0; j < warriorRoute.size(); j++) {
wRoute= wRoute + (warriorRoute.get(j) + " ");
}
return wRoute;
}
public int getPointX(){
return xPoint;
}
public int getPointY(){
return yPoint;
}
}
class Lotus {
private boolean hasPetal;
private static int petals = 100;
Node node;
private ArrayList<Petal> petalList = new ArrayList<Petal>(100);;
public Lotus() {
for(int i =0 ; i< 100 ; i++){
Petal petal = new Petal();
petalList.add(petal);
}
hasPetal = true;
}
public void setCoordinates(int xCoordinate, int yCoordinate) {
node = new Node ( xCoordinate, yCoordinate, this);
}
public synchronized void loosePetal(Warrior w){
if (petals!=0){
petalList.remove(0);
w.setImmortal(true);
petals --;
System.out.println(w.getInhabitantName() + "warrior is immortal ");
}
else{
hasPetal = false;
System.out.println("There are no petals in this lotus");
}
}
public boolean hasPetals(){
return hasPetal;
}
}
class Petal {
}
abstract class Inhabitant extends Thread {
private String name;
public Node node;
public abstract void setCoordinates(int xCoordinate, int yCoordinate);
public void setInhabitantName(String name) {
this.name = name;
}
public String getInhabitantName() {
return name;
}
}
class Fish extends Inhabitant {
Grid lakeNozama;
public Fish(String name, Grid lakeNozama) {
setInhabitantName(name);
this.lakeNozama = lakeNozama;
}
public void setCoordinates(int xPoint, int yPoint) {
node = new Node(xPoint, yPoint, this);
}
}
class RubberEater extends Fish{
ArrayList<Fin> fins;
public RubberEater(String name, Grid lake) {
super(name, lake);
fins = new ArrayList<Fin>();
}
public void update(Warrior warrior){
if ( warrior.node.getPointX()== this.node.getPointX() && warrior.node.getPointY() == this.node.getPointY()) {
eatFins(warrior);
}
}
public synchronized void eatFins(Warrior warrior) {
if (warrior.node.getPointX() == this.node.getPointX() && warrior.node.getPointY() == this.node.getPointY()) {
Game.warriorCount--; //warriors who can swim is decreased by one
warrior.noFins();
System.out.println( getInhabitantName()+ " ate the fins of " + warrior.getInhabitantName());
}
}
}
class KillerFish extends Fish{
public KillerFish(String name, Grid lake) {
super(name, lake);
}
public void update(Warrior warrior) {
if (this.node.getPointX() == warrior.node.getPointX() && this.node.getPointY() == warrior.node.getPointY()) {
killWarrior(warrior);
}
}
public synchronized void killWarrior(Warrior warrior) {
if (warrior.getImmortal()==false) {
warrior.setCanSwim(false);
System.out.println(this.getInhabitantName() + " killer fish, killed warrior " + warrior.getInhabitantName());
warrior = null;
Game.warriorCount--;
} else {
System.out.println( warrior.getInhabitantName() + " immortal warrior cannot be killed");
}
}
}
interface WarriorInterface {
public void eat();
public void sleep();
}
class Warrior extends Inhabitant implements WarriorInterface {
private boolean canSwim;
private boolean isImmortal ;
private static int numberOfWarriors = 0;
ArrayList<Fin> fins = new ArrayList<Fin>(2);
ArrayList<Inhabitant> warriorObservers;
Random random;
Grid lakeNozama;
public Warrior(String name, Grid lakeNozama,ArrayList<Inhabitant> warriorObservers) {
for (int i=0; i<2 ; i++){
Fin fin = new Fin();
fins.add(fin);
}
this.warriorObservers = warriorObservers;
setInhabitantName(name);
random = new Random();
isImmortal = false;
canSwim = true;
this.lakeNozama = lakeNozama;
numberOfWarriors += 1;
}
public void setCoordinates(int xCoordinate, int yCoordinate) {
node = new Node( xCoordinate, yCoordinate, this);
}
public void swim(int gridDirection ,Grid lake ) {
if (getCanSwim()==false) {
System.out.println(getInhabitantName() + " can't swim in the lake!");
this.stop();
}else{
if (gridDirection == 0 && node.getPointX() != 0) {
lake.changeCoordinate(node.getPointX(), node.getPointY(), node.getPointX()-1, node.getPointY(), 4, this );
node.setPointX(node.getPointX() - 1);
} else if (gridDirection == 1 && node.getPointX() != 10) {
lake.changeCoordinate( node.getPointX(), node.getPointY(), node.getPointX()+1, node.getPointY(),4, this);
node.setPointX(node.getPointX() + 1);
} else if (gridDirection == 2 && node.getPointY() != 0) {
lake.changeCoordinate( node.getPointX(), node.getPointY(), node.getPointX(), node.getPointY()-1,4, this);
node.setPointY(node.getPointY() - 1);
} else if (gridDirection == 3 && node.getPointY() != 10) {
lake.changeCoordinate(node.getPointX(), node.getPointY(), node.getPointX(), node.getPointY()+1, 4, this);
node.setPointY(node.getPointY() + 1);
}
if (lake.grid[node.getPointX()][node.getPointY()][0] != null) {
if ( !getImmortal() && lake.grid[node.getPointX()][node.getPointY()][0] instanceof Lotus ) {
Lotus lotus = (Lotus) lake.grid[node.getPointX()][node.getPointY()][0];
pickLotus(lotus);
}
}
notifyFish();
}
}
@Override
public void run() {
while (!TreasureChest.hasWon()) {
if (Game.warriorCount == 0) {
System.out.println("Game over !");
break;
}
swim(random.nextInt(4),lakeNozama);
}
this.stop();
}
public void notifyFish() {
for (int j = 0; j < 6; j++) {
Inhabitant in = warriorObservers.get(j);
if (in instanceof RubberEater) {
RubberEater re = (RubberEater) in;
re.update(this);
} else if (in instanceof KillerFish) {
KillerFish kf = (KillerFish) in;
kf.update(this);
}
}
}
public void noFins() {
setCanSwim(false);
}
public void eat(){
//
}
public void sleep(){
//
}
public void setImmortal(boolean immortal) {
isImmortal = immortal;
}
public boolean getImmortal() {
return isImmortal;
}
public void setCanSwim(boolean canSwim) {
this.canSwim = canSwim;
}
public boolean getCanSwim() {
return canSwim;
}
public static int getWarriorCount() {
return numberOfWarriors;
}
public void update(){
this.stop();
}
public void pickLotus(Lotus lotus) {
if(lotus.hasPetals()!=false){
lotus.loosePetal(this);
}
}
}
class SuperWarrior extends Warrior {
Binocular binocular;
public SuperWarrior(String name, Grid lake, ArrayList<Inhabitant> warriorObservers) {
super(name, lake, warriorObservers);
binocular = new Binocular();
}
public void swimSuper(Grid lake) {
int direction = 4;
if(this.getImmortal()!= false){
binocular.searchLotus(node.getPointX(), node.getPointY(), lake);
}
if(direction ==4){
swim(random.nextInt(4), lake);
}else if (direction >=0 && direction <4) {
swim(direction, lake);
}
}
@Override
public void run() {
while (!TreasureChest.hasWon()) {
if (Game.warriorCount == 0) {
System.out.println("Game over !");
break;
}
swimSuper(lakeNozama);
}
this.stop();
}
public void eat(){
//
}
public void sleep(){
//
}
}
class TreasureChest extends Thread {
private static boolean hasWon = false;
ArrayList<Inhabitant> swimmers;
public TreasureChest(ArrayList<Inhabitant> warriors) {
swimmers = warriors;
}
public static synchronized void setHasWon(boolean won) {
hasWon = won;
}
public synchronized void notifyWarriors() {
for (int j = 0; j < Game.warriorCount; j++) {
Warrior war = (Warrior) swimmers.get(j);
war.update();
}
}
public static synchronized boolean hasWon() {
return hasWon;
}
}
class Grid {
private boolean nodeIsAvailable;
TreasureChest treasure;
Object[][][] grid;
Random random;
public Grid(int gridWidth, int gridLength, TreasureChest treasure) {
nodeIsAvailable = true;
random = new Random();
this.treasure = treasure;
grid = new Object[gridWidth][gridLength][2];
}
public synchronized int checkCoordinate(int xPoint, int yPoint, int indexNo) {
if (grid[xPoint][yPoint][indexNo] instanceof Lotus) {
return 1;
} else if (grid[xPoint][yPoint][indexNo] instanceof RubberEater) {
return 2;
} else if (grid[xPoint][yPoint][indexNo] instanceof KillerFish) {
return 3;
} else if (grid[xPoint][yPoint][indexNo] instanceof Warrior ) {
return 4;
} else if (grid[xPoint][yPoint][indexNo] instanceof Fish){
return 5;
}else if (grid[xPoint][yPoint][indexNo] == null){
return 6;
} else {
return 0 ;
}
}
public synchronized void removeObject(int xPoint, int yPoint) {
grid[xPoint][yPoint][0] = null;
}
public synchronized void setObject( int xPoint, int yPoint,Object object , int indexNo) {
grid[xPoint][yPoint][indexNo] = object;
}
public synchronized boolean checkWinner(int xPoint, int yPoint) {
return (xPoint == 5 & yPoint == 5);
}
public synchronized void checkAvailable(Warrior warrior) { // guarded block
while (nodeIsAvailable==false) {
try {
warrior.wait();
} catch (InterruptedException ex) {
}
}
}
public synchronized void changeCoordinate( int firstX, int firstY, int secondX, int secondY, int inhabitantType,Warrior warrior) {
if (checkCoordinate(secondX, secondY, 1) == 6 && TreasureChest.hasWon()== false) {
grid[secondX][secondY][1] = grid[firstX][firstY][1];
grid[firstX][firstY][1] = null;
System.out.println(warrior.getInhabitantName() + " swam to (" + secondX + "," + secondY + ")");
}
if (checkWinner(secondX, secondY)) {
long endTime = System.currentTimeMillis();
TreasureChest.setHasWon(true);
try {
File file = new File("winner.txt");
FileWriter fileWriter = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.println("Winner : " + warrior.getInhabitantName());
System.out.println("Winner : " + warrior.getInhabitantName());
printWriter.append("Winner route : " + warrior.node.getTheRoute()+"\n");
System.out.println("Winner route : " + warrior.node.getTheRoute());
printWriter.append("Time : " + ((endTime - Game.getStartTime()) ) + " miliseconds");
System.out.println("Time : " + ((endTime - Game.getStartTime()) ) + " miliseconds");
fileWriter.close();
treasure.notifyWarriors();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Game {
private static long timeBegan;
public static int warriorCount = 0;
public static void main(String[] args) {
Random random = new Random();
ArrayList<Lotus> lotus = new ArrayList<Lotus>();
ArrayList<Inhabitant> fish = new ArrayList<Inhabitant>();
ArrayList<Inhabitant> warriors = new ArrayList<Inhabitant>();
TreasureChest treasure = new TreasureChest(warriors);
Grid lake = new Grid( 11, 11, treasure);
lake.setObject(5, 5, treasure, 0);
System.out.println("Treasure is at (5,5)");
lotus.add(new Lotus());
lotus.add(new Lotus());
lotus.add(new Lotus());
lotus.add(new Lotus());
lotus.add(new Lotus());
fish.add(new Fish("CatWoman", lake));
fish.add(new Fish("RobbinHood", lake));
fish.add(new RubberEater("Oswarld", lake));
fish.add(new RubberEater("FishMooney", lake));
fish.add(new KillerFish("Voldermort", lake));
fish.add(new KillerFish("Barbara", lake));
warriors.add(new Warrior("WonderWoman", lake, fish));
warriors.add(new Warrior("Flash", lake, fish));
warriors.add(new Warrior("Batman", lake, fish));
warriors.add(new SuperWarrior("SuperGirl", lake,fish));
int[] gridCorners = {0, 10};
while (warriorCount <= 3) {
int xPoint, yPoint = 0;
int gridSide = random.nextInt(2);
if (gridSide != 0) {
xPoint = gridCorners[random.nextInt(2)];
yPoint = random.nextInt(11);
} else {
xPoint = random.nextInt(11);
yPoint = gridCorners[random.nextInt(2)];
}
if (lake.checkCoordinate(xPoint, yPoint, 0) == 6) {
lake.setObject( xPoint, yPoint,warriors.get(warriorCount), 1);
warriors.get(warriorCount).setCoordinates(xPoint, yPoint);
System.out.println(warriors.get(warriorCount).getClass().getSimpleName()
+ " " + warriors.get(warriorCount).getInhabitantName()
+ " at (" + xPoint + "," + yPoint + ")");
warriorCount++;
}
}
int numberOfLotus = 0;
while (numberOfLotus < 5) {
int xPoint = random.nextInt(11);
int yPoint = random.nextInt(11);
if (lake.checkCoordinate(xPoint, yPoint, 0) == 6) {
lake.setObject( xPoint, yPoint,lotus.get(numberOfLotus), 0);
lotus.get(numberOfLotus).setCoordinates(xPoint, yPoint);
System.out.println("A Tree at (" + xPoint + "," + yPoint + ")");
numberOfLotus++;
}
}
int numberOfFish = 0;
while (numberOfFish < 6) {
int xPoint = random.nextInt(11);
int yPoint = random.nextInt(11);
if (lake.checkCoordinate(xPoint, yPoint, 0) == 6) {
lake.setObject(xPoint, yPoint, fish.get(numberOfFish),0);
fish.get(numberOfFish).setCoordinates(xPoint, yPoint);
System.out.println( fish.get(numberOfFish).getClass().getSimpleName()
+ " " + fish.get(numberOfFish).getInhabitantName()
+ " at (" + xPoint + "," + yPoint + ")");
numberOfFish++;
}
}
System.out.println("Number Of Warriors = " + warriorCount);
timeBegan = System.currentTimeMillis();
for (int j = 0; j < warriorCount; j++) {
warriors.get(j).start();
}
}
public static long getStartTime() {
return timeBegan;
}
}