-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathstarcraft.js
34 lines (28 loc) · 1.12 KB
/
starcraft.js
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
var strips = require('strips');
strips.verbose = true;
// Load the domain and problem.
strips.load('./examples/starcraft/domain.txt', './examples/starcraft/marine.txt', function(domain, problem) {
// Use A* search to run the problem against the domain.
var solutions = strips.solve(domain, problem, cost);
// Display solution.
var solution = solutions[0];
console.log('- Solution found in ' + solution.steps + ' steps!');
for (var i = 0; i < solution.path.length; i++) {
console.log((i + 1) + '. ' + solution.path[i]);
}
});
function cost(state) {
// This is our A* heuristic method to calculate the cost of a state.
// For Starcraft, the heuristic will be how many required buildings have been built. Subtract x from cost for each correct building, with 0 meaning all required buildings have been made and we're done.
var cost = 10;
for (var i in state.actions) {
var action = state.actions[i].action;
if (action == 'depot') {
cost -= 5;
}
else if (action == 'barracks') {
cost -= 5;
}
}
return cost;
}