Commit 5e025bd1 authored by jak.moore's avatar jak.moore

Updates from week 4 lecture.

parents
public enum Actions {
// Possible actions
MOVE_BOAT_LEFT_TO_RIGHT, MOVE_BOAT_RIGHT_TO_LEFT,
WOLF_LEFT_TO_BOAT, WOLF_RIGHT_TO_BOAT,
WOLF_BOAT_TO_LEFT, WOLF_BOAT_TO_RIGHT,
CHICKEN_RIGHT_TO_BOAT, CHICKEN_LEFT_TO_BOAT,
CHICKEN_BOAT_TO_RIGHT, CHICKEN_BOAT_TO_LEFT,
}
import java.util.ArrayList;
public class GameState {
// Class member variables
public ArrayList<Integer> leftBank; // Chicken = 1, wolf = 2
public ArrayList<Integer> rightBank;
public ArrayList<Integer> raft;
public boolean isRaftOnLeftBank;
// Constructor for GameState class
public GameState(ArrayList<Integer> left, ArrayList<Integer> right, ArrayList<Integer> raft, boolean raftBank) {
leftBank = left;
rightBank = right;
this.raft = raft;
isRaftOnLeftBank = raftBank;
}
// Return a clone of a Game state object
@SuppressWarnings("unchecked")
public GameState cloneObject() {
ArrayList<Integer> lbClone, rbClone, raftClone;
// Cloning array lists
lbClone = (ArrayList<Integer>) leftBank.clone();
rbClone = (ArrayList<Integer>) leftBank.clone();
raftClone = (ArrayList<Integer>) raft.clone();
// Creating a new, cloned object to return
GameState clone = new GameState(lbClone, rbClone, raftClone, isRaftOnLeftBank);
return clone;
}
}
\ No newline at end of file
import java.util.ArrayList;
public class Main {
private boolean planCompleted = false;
public static void main(String[] args) {
// Starting game state
ArrayList<Integer> lBank = new ArrayList() {
};
ArrayList<Integer> rBank = new ArrayList() {
};
ArrayList<Integer> raft = new ArrayList() {
};
// Instantiating a new Game State object
GameState gs = new GameState(lBank, rBank, raft, true);
// Invoking get actions method and storing the results in an array list
ArrayList<Actions> validActions = getActions(gs);
// Array list to hold game states
ArrayList<GameState> newStates = new ArrayList();
// Iterate through valid actions simulating their game state
for (Actions a : validActions) {
gs = Simulate(gs, a);
// Storing possible game states in array list
newStates.add(gs);
}
}
// Get list of possible actions
private static ArrayList<Actions> getActions(GameState gs) {
// Array list to hold possible actions
ArrayList<Actions> possibleActions = new ArrayList<>();
// Counters to hold game information
int wolvesOnLBank = 0;
int wolvesOnRBank = 0;
int wolvesOnBoat = 0;
int chicksOnLBank = 0;
int chicksOnRBank = 0;
int chicksOnBoat = 0;
boolean raftFull = false;
// Getting current state of game
if (gs.raft.size() > 1) {
raftFull = true;
}
for (int i : gs.leftBank) {
if (i == 1) {
wolvesOnLBank++;
}
if (i == 2) {
chicksOnLBank++;
}
}
for (int i : gs.rightBank) {
if (i == 1) {
wolvesOnRBank++;
}
if (i == 2) {
chicksOnRBank++;
}
}
for (int i : gs.raft) {
if (i == 1) {
wolvesOnBoat++;
}
if (i == 2) {
chicksOnBoat++;
}
}
// Raft is on left bank
if (gs.isRaftOnLeftBank) {
possibleActions.add(Actions.MOVE_BOAT_LEFT_TO_RIGHT);
if (!raftFull) {
if (chicksOnLBank > 0) {
possibleActions.add(Actions.CHICKEN_LEFT_TO_BOAT);
}
if (wolvesOnLBank > 0) {
possibleActions.add(Actions.WOLF_LEFT_TO_BOAT);
}
}
if (wolvesOnBoat >= 1) {
possibleActions.add(Actions.WOLF_BOAT_TO_LEFT);
}
if (chicksOnBoat >= 1) {
possibleActions.add(Actions.CHICKEN_BOAT_TO_LEFT);
}
}
// Raft is on right bank
else {
possibleActions.add(Actions.MOVE_BOAT_RIGHT_TO_LEFT);
if (!raftFull) {
if (chicksOnRBank > 0) {
possibleActions.add(Actions.CHICKEN_RIGHT_TO_BOAT);
}
if (wolvesOnRBank > 0) {
possibleActions.add(Actions.WOLF_RIGHT_TO_BOAT);
}
if (wolvesOnBoat > 0) {
possibleActions.add(Actions.WOLF_BOAT_TO_RIGHT);
}
if (chicksOnBoat > 0) {
possibleActions.add(Actions.CHICKEN_BOAT_TO_RIGHT);
}
}
}
return possibleActions;
}
// Takes a game state and simulate its possible actions
private static GameState Simulate(GameState gs, Actions a) {
// Get clone of Game State object
gs = gs.cloneObject();
// Simulate what happens for all possible actions
switch (a) {
case CHICKEN_BOAT_TO_LEFT:
gs.raft.remove(1);
gs.leftBank.add(1);
break;
case CHICKEN_BOAT_TO_RIGHT:
gs.raft.remove(1);
gs.rightBank.add(1);
break;
case CHICKEN_LEFT_TO_BOAT:
gs.leftBank.remove(1);
gs.raft.add(1);
break;
case CHICKEN_RIGHT_TO_BOAT:
gs.rightBank.remove(1);
gs.raft.add(1);
break;
case WOLF_BOAT_TO_LEFT:
gs.raft.remove(2);
gs.leftBank.add(2);
break;
case WOLF_BOAT_TO_RIGHT:
gs.raft.remove(2);
gs.rightBank.add(2);
break;
case WOLF_LEFT_TO_BOAT:
gs.leftBank.remove(2);
gs.raft.add(2);
break;
case WOLF_RIGHT_TO_BOAT:
gs.rightBank.remove(2);
gs.raft.add(2);
break;
case MOVE_BOAT_LEFT_TO_RIGHT:
gs.isRaftOnLeftBank = false;
break;
case MOVE_BOAT_RIGHT_TO_LEFT:
gs.isRaftOnLeftBank = true;
break;
default:
break;
}
return gs;
}
// Goal test method
public boolean goalTest(GameState gs) {
int lengthR = gs.rightBank.size();
int lengthL = gs.leftBank.size();
int numberOfWolves = 0;
int numberOfChickens = 0;
// Get number of wolves/chickens on right bank
for (int x = 0; x <= lengthR; x++) {
if (gs.rightBank.get(x).equals(1)) {
numberOfChickens++;
} else if (gs.rightBank.get(x).equals(2)) {
numberOfWolves++;
}
// Check if problem completed
if (completed(numberOfWolves, numberOfChickens)) {
return true;
} else {
numberOfWolves = 0;
numberOfChickens = 0;
}
}
// Get number of chickens/wolves on right bank
for (int y = 0; y <= lengthL; y++) {
if (gs.leftBank.get(y).equals(1)) {
numberOfChickens++;
} else if (gs.leftBank.get(y).equals(2)) {
numberOfChickens++;
}
}
return completed(numberOfWolves, numberOfChickens);
}
// Check final counts
private boolean completed(int wolves, int chickens) {
if ((wolves == 3) && (chickens == 3)) {
planCompleted = true;
return true;
} else {
return false;
}
}
}
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