-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClyde.java
More file actions
176 lines (144 loc) · 4.94 KB
/
Clyde.java
File metadata and controls
176 lines (144 loc) · 4.94 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 projects.Pacman;
import com.sun.javafx.scene.traversal.Direction;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by 20ZhangJ on 6/15/2018.
*/
public class Clyde extends Enemy {
private Location Pacman = new Location(0, 0);
private int totalMoves = 0;
private boolean isScattered = false;
boolean last = false;
Location previous;
/**
* Constructor
*/
public Clyde() {
previous = this.getLocation();
this.setDirection(360);
this.setColor(Color.ORANGE);
}
/**
* Acts out by moving in the right place. CLyde moves into scatter mode normally, but when
* Pacman distance <= 8, he will chase until pacman too far
*/
public void act() {
SmallFood food = new SmallFood();
if (last)
food.putSelfInGrid(getGrid(), previous);
if(totalMoves < 40){}
else if(totalMoves == 40){
if(this.getGrid().get(new Location(7,9)) == null || getGrid().get(new Location(7,9)) instanceof SmallFood)
{
moveTo(new Location(7, 9));
}
}
else if (isCloseEnough())
moveTo(selectMoveLocation(getMoveLocations()));
else
scatterMode(getMoveLocations());
totalMoves++;
}
public boolean canMove() {
Grid gr = this.getGrid();
if(gr == null) {
return false;
} else {
Location loc = this.getLocation();
Location next = loc.getAdjacentLocation(this.getDirection());
if(!gr.isValid(next)) {
return false;
} else {
Actor neighbor = (Actor)gr.get(next);
/*if (neighbor instanceof Food) {
return true;
}*/
return neighbor == null || !(neighbor instanceof Wall);
}
}
}
public void processActors(ArrayList<Actor> actors) {
for (Actor a : actors) {
if (!(a instanceof Wall))
a.removeSelfFromGrid();
}
}
public Location findPacMan() {
Location loc = new Location(0, 0);
ArrayList<Object> actors = getActorList();
for (Object a : actors) {
if (a instanceof Player) {
loc = ((Player) a).getLocation();
break;
}
}
return loc;
}
public Location selectMoveLocation(ArrayList<Location> locs) {
if(totalMoves%4 != 0) {
Location target = findPacMan();
Location chosen = new Location(0, 0);
double distance = 10000;
double temp;
for (Location location : locs) {
temp = Math.sqrt((location.getCol() - target.getCol()) * (location.getCol() - target.getCol()) + (location.getRow() - target.getRow()) * (location.getRow() - target.getRow()));
if (temp < distance) {
chosen = location;
distance = temp;
}
}
//Checks if it was a food;
if(getGrid().get(chosen) instanceof Food) last = true;
else last = false;
previous = this.getLocation();
return chosen;
}
else return this.getLocation();
}
public void scatterMode(ArrayList<Location> locs) {
if (!isScattered) {
if (getLocation() == new Location(17, 1)) {
isScattered = true;
}
else if (totalMoves % 4 != 0) {
Location target = new Location(17, 1);
Location chosen = new Location(0, 0);
double distance = 10000;
double temp = 0;
for (Location location : locs) {
temp = Math.sqrt((location.getCol() - target.getCol()) * (location.getCol() - target.getCol()) + (location.getRow() - target.getRow()) * (location.getRow() - target.getRow()));
if (temp < distance) {
chosen = location;
distance = temp;
}
}
moveTo(chosen);
}
else moveTo(this.getLocation());
}
else {
Location loc = getLocation();
if(canMove())
moveTo(loc.getAdjacentLocation(getDirection()));
else {
setDirection(getDirection() + Location.LEFT);
}
}
}
public boolean isCloseEnough() {
Location current = getLocation();
Location pacMan = findPacMan();
double xCoor = Math.pow(current.getRow() - pacMan.getRow(), 2);
double yCoor = Math.pow(current.getCol() - pacMan.getCol(), 2);
if (Math.sqrt(xCoor + yCoor) <= 8) {
isScattered = false;
return true;
}
else
return false;
}
}