Repository navigation
Expand file tree
/
Copy pathGameEngine.java
More file actions
1238 lines (1105 loc) · 29.6 KB
/
Copy pathGameEngine.java
File metadata and controls
1238 lines (1105 loc) · 29.6 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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* CS 141: Intro to Programming and Problem Solving
* Professor: Edwin Rodríguez
*
* Programming Assignment #N
*
* <description-of-assignment>
* This is a team project in which we implement a 2D game containing
* various objects: rooms, power ups, player, and ninjas.
* The goal of this game is to have the user interact with different
* entities in the program and to complete the objective of finding
* the briefcase.
*
* Team Blank
*
* Justen Minamitani
* Saroj Poudel
* Steve Marrero
* Aaron Lim
* Koshi Huynh
* Jon Camarillo
*/
package edu.cpp.cs.cs141.final_prog_assignment;
import java.util.Random;
import java.util.Random;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* This class determines all the actions that takes place within the game
* from player action to random ninja movments and random items spawns.
*/
public class GameEngine implements Serializable{
private Random random = new Random();
/**
* This field represents the player the user controls. Only 1 player
* may be active in the board at all times.
*/
private Character player;
/**
* This field represents the board of the game
*/
private Board board;
/**
* This field creates the 6 ninjas that are in play
*/
private Character[] ninjas = new Character[6];
private Item radar = null;
private Item invincible = null;
private Item ammo = null;
/**
* This field represents the number of ninjas that are left on the board
*/
private int numNinja;
/**
* This field represents the difficulty of the game
*/
private String difficulty;
/**
* This field represents if the player picked up the radar or not
*/
private boolean hasRadar;
/**
* This method will create the board, player, items, and ninjas.
* It will set the player and rooms in the initial position while
* setting the items and ninjas in random spaces.
* The default setting of the board does not have the radar active
* making the briefcase hidden.
*/
public void createBoard() {
player = new Character(false,0,8);
board = new Board();
board.set(player, 8, 0);
createPowerUp();
createNinjas();
hasRadar = false;
}
/**
* This method will create new ninjas {@link #ninjas} and set the on the board at
* random coordinates which the {@Square} is empty
*/
public void createNinjas() {
int x,y;
int count = 0;
while( count <= 5 ) {
do {
x = getRandom();
y = getRandom();
}while(x >= 5 && y <= 3);
if(!board.at(x, y).getNinja()
&& board.at(x, y).getEmpty()
&& !board.at(x, y).getRoom()
&& !board.at(x, y).getItem()){
ninjas[count] = new Character(x,y);
board.setNinja(ninjas[count],x,y);
ninjas[count].setX(x);
ninjas[count].setY(y);
count ++;
}
}
}
/**
* This method will create {@link Item} radar, invincible, and ammo
* and set them on the board at random locations in the board
*/
private void createPowerUp() {
int x, y;
/**
* Items weren't populated when the player chose to start another new game.
* Items are initialized as null to act as a reset.
*/
radar = null;
invincible = null;
ammo = null;
/**
* will loop until one of each item is created
*/
while(radar == null) {
x = getRandom();
y = getRandom();
if(!board.at(x, y).getItem() && board.at(x, y).getEmpty() && !board.at(x, y).getRoom() && !board.at(x, y).getPlayer()) { //check if there is no item, not a room, and is empty
radar = new Item("radar");
board.set(radar, x, y);
}
}
while(invincible == null) {
x = getRandom();
y = getRandom();
if(!board.at(x, y).getNinja() && board.at(x, y).getEmpty() && !board.at(x, y).getRoom() && !board.at(x, y).getPlayer()) {//check if there is no item, not a room, and is empty
invincible = new Item("invincible");
board.set(invincible, x, y);
}
}
while(ammo == null) {
x = getRandom();
y = getRandom();
if(!board.at(x, y).getNinja() && board.at(x, y).getEmpty() && !board.at(x, y).getRoom() && !board.at(x, y).getPlayer()) {//check if there is no item, not a room, and is empty
ammo = new Item("ammo");
board.set(ammo, x, y);
}
}
}
/**
* This method will get a random number from 0 to 8
* @return random integer from 0 to 8
*/
public int getRandom(){
return random.nextInt(9);
}
/**
* This method will display the String representation of the item at x and y
* @param x integer representing the x coordinate
* @param y integer representing the y coordinate
* @return String representation of the object at the square
*/
public String displayBoard(int x, int y){
return board.displayBoard(x, y);
}
/**
* This method will allow the user to see 2 spaces ahead of them.
* @param direction String input from user which indicates which direction to see wasd
* @return true if spaces ahead isn't a room or wall, otherwise false
*/
public boolean look(String direction) {
Square[] s = new Square[3];
boolean clear = true;
switch(direction.toLowerCase()) {
case "w":
for(int i = 1;i < 3; i++) {
if(player.getX()-i >= 0) {
s[i] = board.at(player.getX()-i, player.getY());
}
}
break;
case "a":
for(int i = 1;i < 3; i++) {
if(player.getY()-i >= 0) {
s[i] = board.at(player.getX(), player.getY()-i);
}
}
break;
case "s":
for(int i = 1;i < 3; i++) {
if(player.getX()+i <= 8) {
s[i] = board.at(player.getX()+i, player.getY());
}
}
break;
case "d":
for(int i = 1;i < 3; i++) {
if(player.getY()+i <= 8) {
s[i] = board.at(player.getX(), player.getY()+i);
}
}
break;
default:
System.out.print("Error: GameEngine-look-switch");
}
for(int i = 1; i < 3; i++) {
if(s[i] != null) {
if(s[i].getRoom()) {
break;
}
else if(s[i].getNinja()) {
s[i].reveal();
clear = false;
}
else {
s[i].reveal();
}
}
}
return clear;
}
/**
* This method moves the player 1 space in the direction indicated by the user.
* It cannot move past its boundaries, and cannot walk through walls.
* Walls are only accessible from the north side, and the player cannot walk over
* ninja while invincible. If player walks over a power up, they will use the items
* effect if valid.
* @param direction String input from the user wasd
* @return true if move is valid: if location isn't a room or wall
* otherwise return false
*/
public boolean move(String direction) {
boolean moved = false;
switch(direction){
case"w":
if((player.getX() == 0) ||
(player.getX() == 1+1 ||player.getX() == 4+1 ||player.getX() == 7+1) &&
(player.getY() == 1 || player.getY() == 4 ||player.getY() == 7)){
System.out.println("WALL!");
moved = false;
}/*
else if(board.at(player.getX()-1, player.getY()).getNinja()) {
System.out.println("Ninja!");
moved = false;
}*/
else {
board.set(player, player.getX()-1, player.getY());
board.move(player.getX()+1, player.getY());
moved = true;
}
break;
case"a":
if((player.getY() == 0) ||
(player.getX() == 1 || player.getX() == 4 || player.getX() == 7) &&
(player.getY() == 1 || player.getY() == 4 || player.getY() == 7) ||
(player.getY() == 1+1 ||player.getY() == 4+1 || player.getY() == 7+1) &&
(player.getX() == 1 || player.getX() == 4 || player.getX() == 7)){
System.out.println("WALL!");
moved = false;
}/*
else if(board.at(player.getX(), player.getY()-1).getNinja()) {
System.out.println("Ninja!");
moved = false;
}*/
else{
board.set(player, player.getX(), player.getY()-1);
board.move(player.getX(), player.getY()+1);
moved = true;
}
break;
case"s":
if((player.getX() == 8) ||
(player.getX() == 1 ||player.getX() == 4 || player.getX() == 7) &&
(player.getY() == 1 ||player.getY() == 4 ||player.getY() == 7) ||
(player.getX() == 1 ||player.getX() == 4 ||player.getX() == 7) &&
(player.getY() == 1 ||player.getY() == 4 || player.getY() == 7)){
System.out.println("WALL!");
moved = false;
}
/*else if(board.at(player.getX()+1, player.getY()).getNinja()) {
System.out.println("Ninja!");
moved = false;
}*/
else{
board.set(player, player.getX()+1, player.getY());
board.move(player.getX()-1, player.getY());
moved = true;
}
break;
case"d":
if((player.getY() == 8) ||
(player.getX() == 1 || player.getX() == 4 || player.getX() == 7) &&
(player.getY() == 1 || player.getY() == 4 || player.getY() == 7) ||
(player.getY() == 1-1 || player.getY() == 4-1 || player.getY() == 7-1) &&
(player.getX() == 1 || player.getX() == 4 || player.getX() == 7)){
System.out.println("WALL!");
moved = false;
}/*
else if(board.at(player.getX(), player.getY()+1).getNinja()) {
System.out.println("Ninja!");
moved = false;
}*/
else{
board.set(player, player.getX(), player.getY()+1);
board.move(player.getX(), player.getY()-1);
moved = true;
}
break;
default:
moved = true;
break;
}
return moved;
}
/**
* This method checks if the player location is the same as the briefcase location
* @return boolean is true if they're the same, false if they're not
*/
public boolean checkPlayerIsBriefcase(){
if(player.getX() == board.getBriefcaseX() && player.getY() == board.getBriefcaseY())
return true;
else
return false;
}
/**
* This method sets the game state to debug mode, revealing the entire board
*/
public void debugMode() {
board.debugMode(getHasRadar());
}
/**
* This method returns the amount of ammo the player has
* @return 1 if full ammo, 0 if no ammo
*/
public int getAmmoCount() {
return player.getAmmo();
}
/**
* This method gets a direction indicated by the user wasd and shoots in that direction.
* The bullet will travel until a ninja is hit or it hits a wall or a room.
* Ammo will be depleted upon firing and will indicate that you cannot fire if you attempt
* with no ammo.
* @param direction String input from user that indicates which direction wasd
* @return String telling user what they hit
*/
public String shoot(String direction) {
int value = 0;
if(player.getAmmo() == 0)
return "You are out of ammunition! Idiot! \n";
if(direction.equals("s") || direction.equals("d"))
value = 1;
else
value = -1;
int x = player.getX();
int y = player.getY();
switch(direction) {
case "w":
case "s":
for(int i = x + value; i >= 0 && i < 9; i+=value) {
if(board.at(i, y).getNinja()) {
board.at(i,y).killNinja();
player.fire();
return "You killed a Ninja!\n";
}
else if(board.at(i, y).getRoom()) {
break;
}
}
break;
case "a":
case "d":
for(int j = y + value; j >= 0 && j < 9; j+=value) {
if(board.at(x, j).getNinja()) {
board.at(x,j).killNinja();
player.fire();
return "You killed a Ninja! \n";
}
else if(board.at(x,j).getRoom()) {
break;
}
}
break;
default:
System.out.println("Error: GameEninge-shoot-switch");
}
player.fire();
return "No Ninjas were killed!\n";
}
public boolean checkSpy(){
int x = player.getX();
int y = player.getY();
int a = 0;
int b = 0;
boolean value = false;
for( int count = 0; count <= 5; count ++) {
if(ninjas[count].getAlive() == false) //if ninja is dead - check next ninja?
continue;
a = ninjas[count].getX();
b = ninjas[count].getY();
if (board.at(x, y).getRoom())// if ninja goes into a room
return value;
if( y == b) { //if in same vertical line
if(x+1 == a) { //check for horizontal match in position
if(!player.isInvincible()) {
reset(x,y);
return !value;
}
else
return value;
}
else if (x-1 == a) {
if(!player.isInvincible()) {//if not invincible
reset(x,y);
return !value;
}
else
return value;
}
}
if ( x == a ) {
if( y + 1 == b) {
if(!player.isInvincible()) {
reset(x,y);
return !value;
}
else
return value;
}
else if (y - 1 == b) {
if(!player.isInvincible()) {
reset(x,y);
return !value;
}
else
return value;
}
}
if(x == a && y == b) {
if(!player.isInvincible()) {
reset(x,y);
return(!value);
}
else
return value;
}
}
return value;
}
/**
* This method represents when the player gets killed by the ninja but still has atleast 1 or more lives.
* The player will be placed back into the initial starting position, and the location where the player
* was killed will be set to empty.
* @param x integer value for x coordinate
* @param y integer value for y coordinate
*/
public void reset(int x,int y) {
player.decLives();
board.set(player,8, 0);
board.setEmpty(x, y);
}
/**
* This method represents the random movement of the ninja. The random movements
* depends on rng from 0 to 3. This method will make sure the ninja will not
* move out of bounds, or into rooms. If the ninja walks over item, {@link Square}
* will temporarily hold the value and upon moving, the item is returned.
*/
public void ninjaMovement() {
for ( int i = 0; i <= 5; i++) {
if(ninjas[i].getAlive() == false)
continue;
int a = ninjas[i].getX();
int b = ninjas[i].getY();
int direction;
int x = 0;
int y = 0;
do {
direction = random.nextInt(4);
switch(direction) {
case 0:
x= a-1;
y= b;
break;
case 1:
x= a+1;
y = b;
break;
case 2:
x= a;
y = b-1;
break;
case 3:
x= a;
y = b+1;
break;
default:
System.out.println("Error @ ninjamovement default case");
}
}while(( x < 0 || x > 8) || (y < 0 || y > 8)|| board.at(x, y).getNinja()|| board.at(x,y).getRoom()); //keep within boundary
boolean valid = false;
while(!valid) { //check so ninjas do not go into room
if(!board.at(x, y).getRoom()) {
moveNinja(ninjas[i],direction,x,y);
valid = true;
}
else
valid = false;
}
}
}
/**
* This method will set the difficulty of the randomized ninja movements. If the player
* is in the Ninjas line of sight, it will follow the player until the player is out of
* its line of sight.
*/
public void useLineOfSightMovement()
{
for ( int i = 0; i <=5; i++)
{
if(ninjas[i].getAlive() == false)
continue;
int a = ninjas[i].getX();
int b = ninjas[i].getY();
int direction;
int x = 0;
int y = 0;
boolean isLoop = false;
if (senseSpyInLineOfSight(a,b) == true)
{
isLoop = true;
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
direction = getNinjaDirection(a, b, player.getX(), player.getY());
while (isLoop == true){
switch(direction) {
case 0: // up
x= a-1;
y= b;
count0++;
break;
case 1: // down
x= a+1;
y = b;
count1++;
break;
case 2: // left
x= a;
y = b-1;
count2++;
break;
case 3: // right
x= a;
y = b+1;
count3++;
break;
case 4: // added
x = a;
y = b;
break;
default:
System.out.println("Error @ uselineofsightmovement default case");
}
// if ninja can't move there...
if ( (x < 0 || x > 8 || y < 0 || y > 8)
|| board.at(x, y).getNinja()==true
|| board.at(x, y).getRoom() == true
|| board.at(x, y).getPlayer() == true)
{
isLoop = true; // ...the do loop activates...
direction = random.nextInt(4); //...and a random direction is assigned.
if (count0 >= 1 && count1 >= 1 && count2 >= 1 && count3 >= 1) { // if ninja can't move at all, case 4 is activated; added count4
isLoop = false;
direction = 4;
x = a;
y = b;
}
}
else{
isLoop = false;
moveNinja(ninjas[i],direction,x,y);
}
}
}
else{
isLoop = true;
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
do
{
direction = random.nextInt(4);
switch(direction) {
case 0:
x= a-1;
y= b;
count0++;
break;
case 1:
x= a+1;
y = b;
count1++;
break;
case 2:
x= a;
y = b-1;
count2++;
break;
case 3:
x= a;
y = b+1;
count3++;
break;
case 4:
x = a;
y = b;
break;
default:
System.out.println("Error @ useradialmovement else - default case");
break;
}
if (count0 >= 1 && count1 >= 1 && count2 >= 1 && count3 >= 1) { // if ninja can't move, case 4 is activated.
isLoop = false;
direction = 4;
x = a;
y = b;
}
else
{
isLoop = false;
}
} while(( x < 0 || x > 8) || (y < 0 || y > 8)
|| (board.at(x, y).getNinja() == true && direction != 4)
|| board.at(x,y).getRoom() == true
|| board.at(x, y).getPlayer() == true
|| isLoop == true); //keep within boundary; added
moveNinja(ninjas[i],direction,x,y);
}
}
}
/**
* This method sets the ninja movement to the hardest difficulty. It will follow the player
* if the player is within a radius of the ninja.
*/
public void useRadialMovement() {
for ( int i = 0; i <=5; i++)
{
if(ninjas[i].getAlive() == false)
continue;
int a = ninjas[i].getX();
int b = ninjas[i].getY();
int direction;
int x = 0;
int y = 0;
boolean isLoop = false;
if (senseSpyInRad(a,b) == true) { // senseSpy() checks if spy is within a 3 unit radius of a ninja; return true if spy is nearby, false if not
direction = getNinjaDirection(a, b, player.getX(), player.getY()); // specifies a direction for a ninja
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
//loop that takes in the specified direction; if the ninja can't move to that location, direction is random
do{
switch(direction) {
case 0: // up
x= a-1;
y= b;
count0++;
break;
case 1: // down
x= a+1;
y = b;
count1++;
break;
case 2: // left
x= a;
y = b-1;
count2++;
break;
case 3: // right
x= a;
y = b+1;
count3++;
break;
case 4: // stay (used only if a ninja can't move)
x = a;
y = b;
break;
default:
System.out.println("Error @ useradialmovement default case");
break;
}
// if ninja can't move somewhere...
if (( x < 0 || x > 8)
|| (y < 0 || y > 8)
|| (board.at(x, y).getNinja()==true && direction != 4)
|| board.at(x, y).getRoom() == true
|| board.at(x, y).getPlayer() == true){ //added
isLoop = true; // ...the do loop activates...
direction = random.nextInt(4); //...and a random direction is assigned.
if (count0 >= 1 && count1 >= 1 && count2 >= 1 && count3 >= 1) { // if ninja can't move, case 4 is activated.
isLoop = false;
direction = 4;
x = a;
y = b;
}
}
else
{
isLoop = false;
moveNinja(ninjas[i],direction,x,y);
}// if for some other reason a ninja can't move, the ninja will keep the same position
}while (isLoop == true);
}
// if the ninja is not within 3 units radius of spy, then direction of ninja movement is random.
else{
isLoop = false;
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
do {
direction = random.nextInt(4);
switch(direction) {
case 0:
x= a-1;
y= b;
count0++;
break;
case 1:
x= a+1;
y = b;
count1++;
break;
case 2:
x= a;
y = b-1;
count2++;
break;
case 3:
x= a;
y = b+1;
count3++;
break;
case 4:
x = a;
y = b;
default:
System.out.println("Error @ useradialmovement else - default case");
break;
}
if (count0 >= 1 && count1 >= 1 && count2 >= 1 && count3 >= 1) { // if ninja can't move, case 4 is activated.
isLoop = false;
direction = 4;
x = a;
y = b;
}
} while(( x < 0 || x > 8) || (y < 0 || y > 8)
|| (board.at(x, y).getNinja() == true && direction != 4)
|| board.at(x,y).getRoom() == true
|| board.at(x, y).getPlayer() == true); //keep within boundary; added
moveNinja(ninjas[i],direction,x,y);
}
}
}
/**
* This method will move the ninja in the x and y coordinate of the board.
* @param ninjas Character object of the ninja that has moved
* @param direction integer value of the random number generated
* @param x integer value for the x coordinate
* @param y integer value for the y coordinate
*/
public void moveNinja( Character ninjas, int direction, int x, int y) {
switch(direction) {
case 0:
board.removeNinja(x+1, y);
board.setNinja(ninjas,x,y);
break;
case 1:
board.removeNinja(x-1, y);
board.setNinja(ninjas,x,y);
break;
case 2:
board.removeNinja(x, y+1);
board.setNinja(ninjas,x,y);
break;
case 3:
board.removeNinja(x, y-1);
board.setNinja(ninjas,x,y);
break;
case 4: // added
board.removeNinja(x, y);
board.setNinja(ninjas,x,y);
default:
break;
}
}
public int getNinjaDirection(int row, int column, int i, int j) // row column = ninja, i j = spy
{
int ninjaDirection = 0;
int s;
if (row < i && column < j) { // ninja in quadrant 3: spy = origin
s = random.nextInt(2);
if (s == 0)
ninjaDirection = 1;
else ninjaDirection = 3;
}
else if (row < i && column > j){
s = random.nextInt(2);
if (s == 0)
ninjaDirection = 1;
else ninjaDirection = 2;
}
else if (row > i && column < j){
s = random.nextInt(2);
if (s == 0)
ninjaDirection = 0;
else ninjaDirection = 3;
}
else if (row > i && column > j){
s = random.nextInt(2);
if (s == 0)
ninjaDirection = 0;
else ninjaDirection = 2;
}
else if (row == i && column < j)
ninjaDirection = 3;
else if (row == i && column > j)
ninjaDirection = 2;
else if (row < i && column == j)
ninjaDirection = 1;
else if (row > i && column == j)
ninjaDirection = 0;
else if (row == i && column == j)
{
ninjaDirection = 4;
}
else System.out.println("Error @ getninjadirection");
return ninjaDirection;
}
/**
* This method will check to see if the player is within the ninjas line of sight.
* @param xNinja x coordinate of the ninja
* @param yNinja y coordinate of the ninja
* @return true if within xNina or yNinja
*/
public boolean senseSpyInLineOfSight(int xNinja, int yNinja)
{
boolean isSpyInSight = false;
if (xNinja == player.getX() || yNinja == player.getY()){ // if ninja and spy are in same row or column
if (xNinja < player.getX()) // same column and ninja above spy
{
for ( int i = xNinja + 1; i < player.getX(); i++)
{
if (board.at(i, player.getY()).getNinja() || board.at(i, player.getY()).getRoom())
{
isSpyInSight = false;
break;
}
else isSpyInSight = true;
}
}
else if (xNinja > player.getX()) // same column and ninja below spy
{
for ( int i = xNinja - 1; i > player.getX(); i--)
{
if (board.at(i, player.getY()).getNinja() || board.at(i, player.getY()).getRoom())
{
isSpyInSight = false;
break;
}
else isSpyInSight = true;
}
}
else if (yNinja > player.getY()) // same row and ninja to the right of spy
{
for ( int i = yNinja -1 ; i > player.getY(); i--)
{
if (board.at(player.getX(), i).getNinja() || board.at(player.getX(), i).getRoom())
{
isSpyInSight = false;
break;
}
else isSpyInSight = true;
}
}
else if (yNinja < player.getY()) // same row and ninja to the left of spy
{
for ( int i = yNinja + 1; i < player.getY(); i++)
{
if (board.at(player.getX(), i).getNinja() || board.at(player.getX(), i).getRoom())
{
isSpyInSight = false;
break;
}
else isSpyInSight = true;
}
}
else
isSpyInSight = false;