class State: left_cats = 0 left_dogs = 0 right_cats = 0 right_dogs = 0 boat_side = "left" # actions = [] def __init__(self, lc, ld, side): self.left_cats = lc self.left_dogs = ld self.right_cats = 3 - lc self.right_dogs = 3 - ld self.boat_side = side self.actions = [] def row(self, direction, cats, dogs): for act in self.actions: if (act.direction == direction and act.required_cats == cats and act.required_dogs == dogs): return act.result_state def add_action(self, act): self.actions.append(act) def get_action_from(self, pool): for item in pool: for act in item.actions: if act.result_state is self: return act print("Could not find action leading to self") def get_action_to(self, target): for act in self.actions: if act.result_state is target: return act # print("Could not find action leading to target") def to_short_string(self): return ("[" + str(self.left_cats) + "," + str( self.left_dogs) + "(" + ("L" if self.boat_side == "left" else " ") + ("R" if self.boat_side == "right" else " ") + ")" + str( self.right_cats) + "," + str(self.right_dogs) + "]") def to_full_string(self): return ("Boat is on " + self.boat_side + " shore with " + str( self.left_cats) + " cats, " + str(self.left_dogs) + " dogs on the left shore, and " + str(self.right_cats) + " cats, " + str(self.right_dogs) + " dogs on the right shore") def to_string(self): return self.to_short_string() + "\t" + self.to_full_string() class Action: required_cats = 0 required_dogs = 0 direction = "left" result_state = None def __init__(self, source, dest): self.required_cats = abs(source.left_cats - dest.left_cats) self.required_dogs = abs(source.left_dogs - dest.left_dogs) self.direction = "left" if source.boat_side == "left": self.direction = "right" self.result_state = dest source.add_action(self) def to_short_string(self): if self.direction == "left": return ("[" + str(self.required_cats) + "," + str( self.required_dogs) + "(L ) ]") else: return ("[ ( R)" + str(self.required_cats) + "," + str( self.required_dogs) + "]") def to_full_string(self): return ("Rowing " + str(self.required_cats) + " cats, " + str( self.required_dogs) + " dogs to " + self.direction + " shore") def to_string(self): return self.to_short_string() + "\t" + self.to_full_string() start = State(3, 3, "left") # Starting point g31 = State(3, 1, "right") g22 = State(2, 2, "right") g32 = State(3, 2, "right") Action(start, g31) Action(start, g22) Action(start, g32) b32 = State(3, 2, "left") g30 = State(3, 0, "right") b31 = State(3, 1, "left") g11 = State(1, 1, "right") Action(g31, b32) Action(g22, b32) Action(b32, g30) Action(g30, b31) Action(b31, g11) g01 = State(0, 1, "right") b03 = State(0, 3, "left") g02 = State(0, 2, "right") b22 = State(2, 2, "left") Action(g11, b22) Action(b22, g02) Action(g02, b03) Action(b03, g01) b02 = State(0, 2, "left") b11 = State(1, 1, "left") b01 = State(0, 1, "left") Action(g01, b02) Action(g01, b11) end = State(0, 0, "right") Action(b02, end) Action(b11, end) Action(b01, end) def is_goal(state): return state.right_cats == 3 and state.right_dogs == 3