-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCState.java
More file actions
233 lines (196 loc) · 5.12 KB
/
Copy pathRCState.java
File metadata and controls
233 lines (196 loc) · 5.12 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import edu.cwru.sepia.environment.model.state.Unit.UnitView;
public class RCState {
private int gCost;
private List<String> peasants;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public RCState getParent() {
return parent;
}
public void setParent(RCState parent) {
this.parent = parent;
}
public void setPeasants(List<String> peasants) {
this.peasants = peasants;
}
private String action;
private RCState parent;
public int getgCost() {
return gCost;
}
public void setgCost(int gCost) {
this.gCost = gCost;
}
public int getfCost() {
return fCost;
}
public void setfCost(int fCost) {
this.fCost = fCost;
}
public int gethCost() {
return hCost;
}
public void sethCost(int hCost) {
this.hCost = hCost;
}
public List<RCState> getNeighbors() {
return neighbors;
}
public void setNeighbors(List<RCState> neighbors) {
this.neighbors = neighbors;
}
public List<String> getState() {
return state;
}
public void setState(List<String> state) {
this.state = state;
}
private int fCost;
private int hCost;
private List<RCState> neighbors = new LinkedList<RCState>();
private List<String> state = new LinkedList<String>();
public RCState(List<String> state, int gCost, int hCost, RCState parent){
for(String s: state){
this.state.add(s);
}
this.gCost = gCost;
this.hCost = hCost;
fCost = gCost + hCost;
this.parent = parent;
}
public boolean isGoal(int wood, int gold){
for(String s: state)
if(s.equalsIgnoreCase("Wood(" + wood + ",t1)"))
for(String s2: state)
if(s2.equalsIgnoreCase("Gold(" + gold + ",t1)"))
return true;
return false;
}
public RCState clone(){
RCState c = new RCState(state,getgCost() + getfCost(),0,this);
return c;
}
public boolean Peasant(String peasant){
return state.contains("Peasant(" + peasant + ")");
}
public boolean Townhall(String townhall){
return state.contains("Townhall(" + townhall + ")");
}
public boolean Idle(String peasant){
return state.contains("Idle(" + peasant + ")");
}
public boolean EmptyHanded(String unit){
return state.contains("Holding(nil," + unit + ")");
}
public boolean HoldingWood(String unit){
return state.contains("Holding(w," + unit + ")");
}
public boolean HoldingGold(String unit){
return state.contains("Holding(g," + unit + ")");
}
public boolean Near(String object, String unit){
return state.contains("Near(" + object + "," + unit + ")");
}
public int getWood(String forest){
for(String s: state){
if(s.contains("Wood") && s.contains(forest))
return Integer.parseInt(s.substring(s.indexOf('(') +1,s.indexOf(',')));
}
return 0;
}
public int getGold(String mine){
for(String s: state){
if(s.contains("Gold") && s.contains(mine))
return Integer.parseInt(s.substring(s.indexOf('(') + 1,s.indexOf(',')));
}
return 0;
}
public List<String> getPeasants(){
List<String> peasants = new LinkedList<String>();
for(String s: state){
if(s.contains("Peasant("))
peasants.add(s.substring(s.indexOf('(') +1,s.indexOf(')')));
}
return peasants;
}
public List<String> getTownhalls(){
List<String> townhalls = new LinkedList<String>();
for(String s:state)
if(s.contains("Townhall("))
townhalls.add(s.substring(s.indexOf('(') +1,s.indexOf(')')));
return townhalls;
}
public boolean HarvestWood(String unit){
if(Peasant(unit) && Near("f",unit) && EmptyHanded(unit) && Idle(unit))
{
state.remove("Holding(nil," + unit);
state.add("Holding(g," + unit + ")");
return true;
}
return false;
}
public boolean HarvestGold(String unit){
if(Peasant(unit) && Near("g",unit) && EmptyHanded(unit) && Idle(unit))
{
state.remove("Holding(nil," + unit);
state.add("Holding(g," + unit + ")");
return true;
}
return false;
}
public boolean DepositGold(String townhall,String unit){
if(Peasant(unit) && Townhall(townhall) && Near(townhall,unit) && HoldingGold(unit) && Idle(unit) ){
state.remove("Holding(g," + unit + ")");
int currentGold = getGold(townhall);
state.remove("Gold(" + currentGold + "," + townhall + ")");
currentGold+= 100;
state.add("Gold(" + currentGold + "," + townhall + ")");
return true;
}
else
return false;
}
public boolean DepositWood(String townhall,String unit){
if(Peasant(unit) && Townhall(townhall) && Near(townhall,unit) && HoldingWood(unit) && Idle(unit) ){
state.remove("Holding(w," + unit + ")");
int currentWood = getWood(townhall);
state.remove("Wood(" + currentWood + "," + townhall + ")");
currentWood+= 100;
state.add("Wood(" + currentWood + "," + townhall + ")");
return true;
}
else
return false;
}
public boolean isNear(String peasant, String unit) {
for(String s: state){
if(s.equalsIgnoreCase("Near(" + unit + "," + peasant + ")"))
return true;
}
return false;
}
public boolean GoNear(String unit, String peasant){
if(Peasant(peasant)){
int index = -1;
for(String s: state){
if(s.equalsIgnoreCase("Near(" + unit + "," + peasant + ")"))
return false;
if(s.contains("Near("))
index = state.indexOf(s);
}
if(index != -1)
state.remove(index);
state.add("Near(" + unit + "," + peasant + ")");
return true;
}
else
return false;
}
}