-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
189 lines (151 loc) · 5.09 KB
/
Board.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import ants.Direction;
import ants.Tile;
/**
* @author carmine carella
*
* Class Board
*/
public class Board {
//STARTING SQUARE - ANTHILL
Square antHillSquare;
//CURRENT SQUARE
Square currentSquare;
//VISITED SQUARE
HashMap<String, Square> discoveredSquare;
//SETS OF SQUARES THAT CONTAIN FOOD OR NOT THE ANT MEETS IN HIS PATH
Set<Square> unsettledFoodSquare;
Set<Square> unsettledPlainSquare;
public Board() {
//anthill is the first square for an ant when it is created
antHillSquare = new Square(0,0);
//when the board is created for an ant the current square is the anthill
currentSquare = antHillSquare;
this.discoveredSquare = new HashMap<String, Square>();
this.discoveredSquare.put(antHillSquare.squareKey(), antHillSquare);
this.unsettledFoodSquare = new HashSet<Square>();
this.unsettledPlainSquare = new HashSet<Square>();
}
/****** GET DIRECTION OF NEXT SQUARE TO MOVE ******/
public Direction getDirection(Square square) {
Direction result = null;
this.unsettledPlainSquare.remove(square);
if(Utils.isNorthSquare(square, currentSquare)){
result = Direction.NORTH;
}
if(Utils.isEastSquare(square, currentSquare)){
result = Direction.EAST;
}
if(Utils.isSouthSquare(square, currentSquare)){
result = Direction.SOUTH;
}
if(Utils.isWestSquare(square, currentSquare)){
result = Direction.WEST;
}
System.out.println("Current square: " + currentSquare.squareKey());
System.out.println("SquareToMove: " + square);
System.out.println("Direction to move: " + result.toString());
if(result != null)
currentSquare = square;
return result;
}
/****** EXPLORE SORROUNDING SQUARES ******/
// Update the current square with the new amount of food
public void updateCurrentSquare(Integer food) {
currentSquare.setFood(food);
if (food <= 0){
this.unsettledFoodSquare.remove(currentSquare);
}
}
// Explore sorrounding square around the current one
public void exploreSurroundings(Tile N, Tile E, Tile S, Tile W) {
exploreSurrounding(Utils.getNorthSquare(currentSquare), N, currentSquare.getX(), currentSquare.getY()+1);
exploreSurrounding(Utils.getEastSquare(currentSquare), E, currentSquare.getX()+1, currentSquare.getY());
exploreSurrounding(Utils.getSouthSquare(currentSquare), S, currentSquare.getX(), currentSquare.getY()-1);
exploreSurrounding(Utils.getWestSquare(currentSquare), W, currentSquare.getX()-1, currentSquare.getY());
}
private void exploreSurrounding(String key, Tile t, Integer x, Integer y) {
//square already explored
if (discoveredSquare.get(key) != null) {
discoveredSquare.get(key).setFood(t.getAmountOfFood());
if (discoveredSquare.get(key).getFood() <= 0){
this.unsettledFoodSquare.remove(discoveredSquare.get(key));
}
}
//new square for the ant
else {
if (t.isTravelable()) {
Square newSquare = new Square(x, y, t.getAmountOfFood());
discoveredSquare.put(key, newSquare);
if (newSquare.getFood() > 0){
this.unsettledFoodSquare.add(newSquare);
}
else{
this.unsettledPlainSquare.add(newSquare);
}
}
}
}
/****** NEXT CELL TO MOVE, FOOD HAS PRIORITY ******/
public Square getNextSquare() {
Square closestFoodSquare = closestSquare(unsettledFoodSquare);
Square closestPlainSquare = closestSquare(unsettledPlainSquare);
if (closestFoodSquare != null)
return closestFoodSquare;
else
return closestPlainSquare;
}
private Square closestSquare(Set<Square> squareSet) {
Square best = null;
Integer distance = Integer.MAX_VALUE;
if (!squareSet.isEmpty()) {
for (Square s : squareSet) {
Integer d = Utils.approximateDistance(currentSquare, discoveredSquare.get(s.squareKey()));
if (d < distance) {
best = discoveredSquare.get(s.squareKey());
distance = d;
}
}
}
return best;
}
/****** SHARING INFORMATION ABOUT ENVIROMENT KNOWLEDGE ******/
public void updateBoard(List<Square> squares) {
for(Square s: squares) {
if(discoveredSquare.containsKey(s.squareKey())){
Integer mapFood = discoveredSquare.get(s.squareKey()).getFood();
discoveredSquare.get(s.squareKey()).setFood(Math.min(s.getFood(), mapFood));
if (discoveredSquare.get(s.squareKey()).getFood() <= 0){
this.unsettledFoodSquare.remove(s.squareKey());
}
}
else {
Square newSquare = new Square(s.getX(),s.getY(),s.getFood());
newSquare.setParent(s.getParent());
discoveredSquare.put(newSquare.squareKey(), newSquare);
if (newSquare.getFood() > 0){
this.unsettledFoodSquare.add(newSquare);
} else {
this.unsettledPlainSquare.add(newSquare);
}
}
}
}
/****** UTILITY METHODS ******/
public Square getAntHillSquare() {
return antHillSquare;
}
public Square getCurrentSquare() {
return currentSquare;
}
public Boolean isAntHill(){
if(this.currentSquare.equals(antHillSquare)){
return true;
} else {
return false;
}
}
}