-
Notifications
You must be signed in to change notification settings - Fork 1
/
PawnCritter_gonz.java
90 lines (79 loc) · 2.45 KB
/
PawnCritter_gonz.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
//Brandon Gonzalez
//Pawn Critter
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Critter;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
public class PawnCritter_gonz extends Critter {
public ArrayList< Actor > getActors() {
ArrayList< Actor > actors = new ArrayList< Actor >();
int[] dirs = {Location.HALF_LEFT, Location.HALF_RIGHT};
for(Location loc: getLocationsInDirections(dirs)) {
Actor myActor = getGrid().get(loc);
if(myActor != null)
actors.add(myActor);
}
return actors;
}
public Location getMoveLocation() {
Location loc = new Location(0,0);
int[] dir = {Location.AHEAD};
if(getActors().size() == 0) {
// Checks if there is an actor in front of it
Actor myActor = getGrid().get(getLocationsInDirections(dir).get(0));
// Actor ahead or Not a valid move ie off grid
if(myActor != null) {
loc = getLocation();
//Reverses direction
setDirection(180);
} else //if there is nothing in front, it moves up
loc = getLocationsInDirections(dir).get(0);
} else {
Random myRandom = new Random();
//Randomly choses an actor in the diagonals to consume
int index = myRandom.nextInt(getActors().size());
//Moves to the chosen actors location
Actor myActor = getActors().get(index);
if(myActor instanceof Rock)
loc = getLocationsInDirections(dir).get(0);
else {
loc = myActor.getLocation();
processActor(myActor);
}
}
return loc;
}
public void makeMove(Location loc) {
super.makeMove(loc);
}
public void act() {
if (getGrid() == null)
return;
Location loc = getMoveLocation();
makeMove(loc);
}
public void processActor(Actor actor)
{
if (!(actor instanceof Rock))
actor.removeSelfFromGrid();
}
public ArrayList<Location> getLocationsInDirections(int[] directions) {
ArrayList<Location> locs = new ArrayList<Location>();
Grid gr = getGrid();
Location loc = getLocation();
for (int d : directions)
{
Location neighborLoc = loc.getAdjacentLocation(getDirection() + d);
if (gr.isValid(neighborLoc))
locs.add(neighborLoc);
}
return locs;
}
}