-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
116 lines (107 loc) · 2.62 KB
/
State.java
File metadata and controls
116 lines (107 loc) · 2.62 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
import java.util.ArrayList;
public class State implements Comparable<State> {
private State prevTown;
private Town town;
private int gCost;
private ArrayList<Job> jobsLeft;
private Strategy strats;
public State(State prevTown, Town currTown, int gCost, ArrayList<Job> jobs, Strategy strats) {
this.prevTown = prevTown;
this.town = currTown;
this.gCost = gCost;
this.jobsLeft = new ArrayList<>();
jobsLeft.addAll(jobs);
this.strats = strats;
}
/**
* Funciton which determines which edge to take depending on distance
* @precondition State o != null
*/
@Override
public int compareTo(State o) {
return calculateFCost() - o.calculateFCost();
}
public int getCurrCost() {
return gCost;
}
/**
* Calculates totalCost including zero heurisitic
* @return
*/
private int calculateFCost() {
return gCost + strats.calculateHeuristic(this);
}
public Town getTown() {
return town;
}
public ArrayList<Job> getJobs() {
return jobsLeft;
}
public boolean jobsToDoIsEmpty() {
if(jobsLeft.isEmpty()) {
return true;
}
return false;
}
/**
* @precondition: jobs exists
* @param jobs
*/
public void printJobs(ArrayList<Job> jobs) {
String s = "";
s = recursiveTracePath(s);
String[] sArray = s.split(" ");
int size = sArray.length;
//figure out how to differentate prints
for(int i = 1; i < size - 1; i++) {
if(!checkIfJobIsEmpty(sArray[i], sArray[i+1], jobs)) {
System.out.print("Empty");
} else {
System.out.print("Job");
}
System.out.println(" "+ sArray[i] + " " + "to" + " " + sArray[i + 1]);
}
}
/**
* Checks if the town from and town to have a job existing
* @param townFrom
* @param townTo
* @param jobs
* @return
*/
public boolean checkIfJobIsEmpty(String townFrom, String townTo, ArrayList<Job> jobs) {
for(Job j : jobs) {
if(j.getTownFrom().getTown().equals(townFrom) && j.getTownTo().getTown().equals(townTo)) {
return true;
}
}
return false;
}
/**
* Recusive funciton to constantly pass through the strings of towns
* @param s
* @return
*/
public String recursiveTracePath(String s) {
if(prevTown != null) {
s = prevTown.recursiveTracePath(s);
}
s = s + " " + town.getTown();
return s;
}
/**
* Updates the array list of jobs to do
*/
public void updateTrips() {
Job toRemove = null;
for(Job j : jobsLeft) {
if (town.equals(j.getTownTo()) && prevTown.getTown().equals(j.getTownFrom())) {
toRemove = j;
break;
}
}
if(toRemove != null) {
jobsLeft.remove(toRemove);
}
}
}