-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonster.java
More file actions
176 lines (163 loc) · 4.23 KB
/
Monster.java
File metadata and controls
176 lines (163 loc) · 4.23 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
package assign2;
import java.util.*;
/** The monster class, whos main goal is to chase creatures
*/
public class Monster{
/**position of the monster*/
private int[] position = new int[2];
/** the coordinates of the last seen creature*/
private int[] previousTarget = null;
/** edge of the world*/
private static int[] worldEdge = new int[2];
/**sight range of the monster*/
private static int sightRange = 0;
/**Counter of the track time */
private int trackTime = 0;
/**Constructor for monster, defines starting position.
* @param x x coordinate
* @param y y coordinate
*/
public Monster(int x,int y){
position[0] = x; //x position
position[1] = y; //y position
}
/**returns current position.
* @rerturn int[] representing positon.
*/
public int[] getPosition(){
return position;
}
/**sets the max x and y values for movement.
* @param maxX max x coordinate of the map.
* @param maxY max y coordinate of the map.
*/
public void setEdge(int maxX, int maxY){
worldEdge[0] = maxX;
worldEdge[1] = maxY;
}
/**sets the sight range for all monsters.
* @param n the sight range.
*/
public void setSightRange(int n){
sightRange = n;
}
/**finds the closest creature or returns null
* @param foodPos the position of the all food.
* @return coordinate of closest food.
*/
public int[] findFood(int[][] foodPos){
int[] currentOption = null, target = null;
int xDiff = 0, yDiff = 0, choice = 0, optionXDiff = 0, optionYDiff = 0;
for(int i = 0; i < foodPos.length; i++){
currentOption = foodPos[i];
if(Math.abs(optionXDiff = currentOption[0] - position[0]) <= sightRange
&& Math.abs(optionYDiff = currentOption[1] - position[1]) <=
sightRange){
if(target == null){
trackTime = 2;
target = foodPos[i];
previousTarget = target;
xDiff = optionXDiff;
yDiff = optionYDiff;
}
else if((optionXDiff + optionYDiff) < (xDiff + yDiff)){
xDiff = optionXDiff;
yDiff = optionYDiff;
target = foodPos[i];
previousTarget = target;
}
}
}
return target;
}
/**Goes towards food or tracks food, or moves randomly depending
* on presence of food.
* @param foodPos position of all food.
* @return if it has eaten something.
*/
public boolean move(int[][] foodPos){
int[] target = findFood(foodPos);
Random choiceGen = new Random();
if(target == null && trackTime != 0){
moveAfter(previousTarget, choiceGen);
trackTime--;
return false;
}
else if(target != null){
return moveAfter(target, choiceGen);
}
randomMove(choiceGen);
return false;
}
/**Moves towards the target
* @param target the coordinates of the target.
* @param choiceGen the random generator used to make choices.
* @return if it has eaten something.
*/
public boolean moveAfter(int[] target, Random choiceGen){
int xDiff = target[0] - position[0], yDiff = target[1] - position[1];
int choice = 0;
if(Math.abs(xDiff) > 0 && Math.abs(yDiff) > 0){
choice = choiceGen.nextInt(2);
if(choice == 1){
position[1] += yDiff/(Math.abs(yDiff));
}
else{
position[0] += xDiff/(Math.abs(xDiff));
}
}
else if(Math.abs(xDiff) > 0){
position[0] += xDiff/(Math.abs(xDiff));
if(position[0] == target[0]){
trackTime = 0;
return true;
}
}
else if(Math.abs(yDiff) > 0){
position[1] += yDiff/(Math.abs(yDiff));
if(position[1] == target[1]){
trackTime = 0;
return true;
}
}
else{
randomMove(choiceGen);
trackTime = 0;
}
return false;
}
/**Moves randomly based off a random number generator.
* @param choiceGen random generator used to make choices.
*/
public void randomMove(Random choiceGen){
int choice = choiceGen.nextInt(4);
switch(choice){
case 0:
if(position[0] < worldEdge[0]){
position[0] += 1;
break;
}
case 1:
if(position[0] > 0){
position[0] -= 1;
}
else{
position[0] += 1;
}
break;
case 2:
if(position[1] < worldEdge[1]){
position[1] += 1;
break;
}
case 3:
if(position[1] > 0){
position[1] -= 1;
}
else{
position[1] += 1;
}
break;
}
}
}