-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticGrid.java
More file actions
39 lines (32 loc) · 1010 Bytes
/
GeneticGrid.java
File metadata and controls
39 lines (32 loc) · 1010 Bytes
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
import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;
import info.gridworld.grid.BoundedGrid;
import java.util.ArrayList;
public class GeneticGrid<E> extends BoundedGrid<E>
{
public GeneticGrid(int rows, int cols)
{
super(rows, cols);
}
public int getDistanceToFood(Location loc)
{
ArrayList<Location> foodLocs = new ArrayList<Location>();
for(Location l : getOccupiedLocations())
{
if(get(l) instanceof FoodSource) foodLocs.add(l);
}
if (foodLocs.size() < 1) return Integer.MAX_VALUE;
Location firstFood = foodLocs.get(0);
int minDist = getDistanceBetween(loc, firstFood);
for(Location f : foodLocs)
{
if(getDistanceBetween(loc, f) < minDist) minDist = getDistanceBetween(loc, f);
}
return minDist;
}
// since we're on a grid, this is going to be manhattan distance
public int getDistanceBetween(Location loc1, Location loc2)
{
return Math.abs(loc1.getCol() - loc2.getCol()) + Math.abs(loc1.getRow() - loc2.getRow());
}
}