Commit 70e39fec authored by jak.moore's avatar jak.moore

node

parent e25aa278
This diff is collapsed.
...@@ -6,6 +6,6 @@ public enum Actions { ...@@ -6,6 +6,6 @@ public enum Actions {
WOLF_LEFT_TO_BOAT, WOLF_RIGHT_TO_BOAT, WOLF_LEFT_TO_BOAT, WOLF_RIGHT_TO_BOAT,
WOLF_BOAT_TO_LEFT, WOLF_BOAT_TO_RIGHT, WOLF_BOAT_TO_LEFT, WOLF_BOAT_TO_RIGHT,
CHICKEN_RIGHT_TO_BOAT, CHICKEN_LEFT_TO_BOAT, CHICKEN_RIGHT_TO_BOAT, CHICKEN_LEFT_TO_BOAT,
CHICKEN_BOAT_TO_RIGHT, CHICKEN_BOAT_TO_LEFT, CHICKEN_BOAT_TO_RIGHT, CHICKEN_BOAT_TO_LEFT, VOID
} }
...@@ -2,7 +2,7 @@ import java.util.ArrayList; ...@@ -2,7 +2,7 @@ import java.util.ArrayList;
public class Main { public class Main {
private boolean planCompleted = false; private static boolean planCompleted = false;
public static void main(String[] args) { public static void main(String[] args) {
...@@ -17,19 +17,40 @@ public class Main { ...@@ -17,19 +17,40 @@ public class Main {
// Instantiating a new Game State object // Instantiating a new Game State object
GameState gs = new GameState(lBank, rBank, raft, true); GameState gs = new GameState(lBank, rBank, raft, true);
// Invoking get actions method and storing the results in an array list // Closed list
ArrayList<Actions> validActions = getActions(gs); ArrayList<GameState> closedList = new ArrayList();
// Array list to hold game states // Array list to hold game states
ArrayList<GameState> newStates = new ArrayList(); ArrayList<Node> frontier = new ArrayList();
frontier.add(new Node(Actions.VOID, gs));
while (true) {
Node firstNode = frontier.get(0);
if (goalTest(firstNode.gs)) {
System.out.print("COMPLETE");
planCompleted = true;
}
ArrayList<Actions> validActions = getActions(firstNode.gs);
// Iterate through valid actions simulating their game state // Iterate through valid actions simulating their game state
for (Actions a : validActions) { for (Actions a : validActions) {
gs = Simulate(gs, a); gs = Simulate(firstNode.gs, a);
if (!closedList.contains(gs)) {
// Storing possible game states in array list // Storing possible game states in frontier
newStates.add(gs); frontier.add(new Node(a, gs));
}
}
closedList.add(firstNode.gs);
} }
...@@ -39,7 +60,7 @@ public class Main { ...@@ -39,7 +60,7 @@ public class Main {
private static ArrayList<Actions> getActions(GameState gs) { private static ArrayList<Actions> getActions(GameState gs) {
// Array list to hold possible actions // Array list to hold possible actions
ArrayList<Actions> possibleActions = new ArrayList<>(); ArrayList<Actions> possibleActions = new ArrayList();
// Counters to hold game information // Counters to hold game information
int wolvesOnLBank = 0; int wolvesOnLBank = 0;
...@@ -53,7 +74,9 @@ public class Main { ...@@ -53,7 +74,9 @@ public class Main {
// Getting current state of game // Getting current state of game
if (gs.raft.size() > 1) { if (gs.raft.size() > 1) {
raftFull = true; raftFull = true;
} }
for (int i : gs.leftBank) { for (int i : gs.leftBank) {
...@@ -238,10 +261,11 @@ public class Main { ...@@ -238,10 +261,11 @@ public class Main {
} }
return gs; return gs;
} }
// Goal test method // Goal test method
public boolean goalTest(GameState gs) { public static boolean goalTest(GameState gs) {
int lengthR = gs.rightBank.size(); int lengthR = gs.rightBank.size();
int lengthL = gs.leftBank.size(); int lengthL = gs.leftBank.size();
...@@ -293,7 +317,7 @@ public class Main { ...@@ -293,7 +317,7 @@ public class Main {
} }
// Check final counts // Check final counts
private boolean completed(int wolves, int chickens) { private static boolean completed(int wolves, int chickens) {
if ((wolves == 3) && (chickens == 3)) { if ((wolves == 3) && (chickens == 3)) {
...@@ -303,7 +327,9 @@ public class Main { ...@@ -303,7 +327,9 @@ public class Main {
} else { } else {
return false; return false;
} }
} }
} }
public class Node {
Node parent;
GameState gs;
public Node(Actions actionTaken, GameState gs) {
this.gs = gs;
}
public void setParent(Node parent) {
this.parent = parent;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment