-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeleport.java
More file actions
104 lines (88 loc) · 4.64 KB
/
Teleport.java
File metadata and controls
104 lines (88 loc) · 4.64 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
import java.util.ArrayList;
public class Teleport {
/*
Teleport function. Every time a hero need to teleport, a new instance of Teleport will be created.
* */
private ArrayList<Heros> heroList;
private int actingHeroIndex;
private Map map;
private boolean movable;
public Teleport(ArrayList<Heros> heroList, int actingHeroIndex, Map map){
/* Every time when a hero wants to teleport, a new instance will be created with current map which has the record
* of the position of all the heroes and monsters, name list of alive heroes and the acting hero index */
this.heroList = heroList;
this.actingHeroIndex = actingHeroIndex;
this.map = map;
this.movable = true;
}
public int requestTargetHeroIndex(){
/*Prompt an inquiry about which hero to teleport. Then go through the list of alive heroes to print out the heroes
* that are not in the same lane of the currently acting hero.*/
System.out.println("Select a index of the hero whose adjacent cells you want to teleport to ");
ArrayList<Integer> availableHeroIndex = new ArrayList<>();
for (int i = 0;i< heroList.size();i++){
if (i == actingHeroIndex){
continue;
}
if (heroList.get(i).getCurrentLane() == heroList.get(actingHeroIndex).getCurrentLane()){
/* if the hero is in the same lane then skip it.*/
continue;
}
availableHeroIndex.add(i);
System.out.print(" " + i + " ");
System.out.print(heroList.get(i).getName());
}
System.out.println(" ");
if (availableHeroIndex.isEmpty()){
movable = false;
/*No place to teleport*/
System.out.println("There is no hero can be targeted to teleport to.");
throw new NegativeArraySizeException();
}
return PromptScan.scanWithinArray(availableHeroIndex);
}
public void requestTeleport(){
/*Prompt an inquiry about which hero to teleport. Then go through the list of alive heroes to check if the heroes'
* adjacent cells are good for teleporting to. Then only heroes with available adjacent cells will be printed out
* and only inputs of the indices printed out are valid. */
System.out.println("Select the index of the desired adjacent location of targeted hero");
int targetHeroIndex = requestTargetHeroIndex();
int targetedHeroCol = heroList.get(targetHeroIndex).getHeroCol();
int targetedHeroRow = heroList.get(targetHeroIndex).getHeroRow();
ArrayList<Integer> availableLocationIndex = new ArrayList<>();
if (targetedHeroRow > 0 && !map.getStatus()[targetedHeroCol][targetedHeroRow - 1].isHeroOccupied()){
System.out.println("Type 1 to teleport to the location behind targeted hero.");
availableLocationIndex.add(1);
}
if (targetedHeroCol > 0 && !map.getStatus()[targetedHeroCol - 1][targetedHeroRow].isHeroOccupied() &&
!(map.getStatus()[targetedHeroCol - 1][targetedHeroRow] instanceof Inaccessible)){
System.out.println("Type 2 to teleport on the left of targeted hero.");
availableLocationIndex.add(2);
}
if (targetedHeroCol < 7 && !map.getStatus()[targetedHeroCol + 1][targetedHeroRow].isHeroOccupied() &&
!(map.getStatus()[targetedHeroCol + 1][targetedHeroRow] instanceof Inaccessible)){
System.out.println("Type 3 to teleport to the location to the right of targeted hero.");
availableLocationIndex.add(3);
}
if (availableLocationIndex.isEmpty()){
throw new NegativeArraySizeException();
}
int targetLocationIndex = PromptScan.scanWithinArray(availableLocationIndex);
int targetLocationCol = targetedHeroCol;
int targetLocationRow = targetedHeroRow;
switch (targetLocationIndex) {
case 1 -> targetLocationRow = targetedHeroRow - 1;
case 2 -> targetLocationCol = targetedHeroCol - 1;
case 3 -> targetLocationCol = targetedHeroCol + 1;
}
int[] newPosition = new int[2];
int[] oldPosition = new int[2];
oldPosition[0] = heroList.get(actingHeroIndex).getHeroCol();
oldPosition[1] = heroList.get(actingHeroIndex).getHeroRow();
newPosition[0] = targetLocationCol;
newPosition[1] = targetLocationRow;
map.HeroMove(actingHeroIndex,heroList, newPosition, oldPosition);
heroList.get(actingHeroIndex).setHeroCol(newPosition[0]);
heroList.get(actingHeroIndex).setHeroRow(newPosition[1]);
}
}