-
Notifications
You must be signed in to change notification settings - Fork 0
/
wolveschicks.py
62 lines (52 loc) · 3.08 KB
/
wolveschicks.py
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
from solver import Solver
class WolvesChicksState:
def __init__(self, chicks1, wolves1, chicks2, wolves2, last_move, raft_location):
self.chicks1 = chicks1
self.wolves1 = wolves1
self.chicks2 = chicks2
self.wolves2 = wolves2
self.last_move = last_move
self.raft_location = raft_location
def __repr__(self):
return 'C'*self.chicks1 + 'W'*self.wolves1 + ' ' + self.last_move + ' ' + 'C'*self.chicks2 + 'W'*self.wolves2 + '\n'
def __hash__(self):
return hash((self.chicks1, self.wolves1, self.chicks2, self.wolves2, self.raft_location))
def __eq__(self, other):
if other == None:
return False
return self.chicks1 == other.chicks1 and self.chicks2 == other.chicks2 \
and self.wolves1 == other.wolves1 and self.wolves2 == other.wolves2 \
and self.raft_location == other.raft_location
class WolvesChicksSolver(Solver):
def solve(self, chicks, wolves):
self.solve_optimal(WolvesChicksState(chicks, wolves, 0, 0, "", 0))
def get_next_states(self, state):
states = []
if state.raft_location == 0:
#Move one chick
states.append(WolvesChicksState(state.chicks1 - 1, state.wolves1, state.chicks2 + 1, state.wolves2, "C->", 1))
#Move two chicks
states.append(WolvesChicksState(state.chicks1 - 2, state.wolves1, state.chicks2 + 2, state.wolves2, "CC->", 1))
#Move one wolf
states.append(WolvesChicksState(state.chicks1, state.wolves1 - 1, state.chicks2, state.wolves2 + 1, "W->", 1))
#Move two wolves
states.append(WolvesChicksState(state.chicks1, state.wolves1 - 2, state.chicks2, state.wolves2 + 2, "WW->", 1))
#Move chick and wolf
states.append(WolvesChicksState(state.chicks1 - 1, state.wolves1 - 1, state.chicks2 + 1, state.wolves2 + 1, "CW->", 1))
else:
#Move one chick
states.append(WolvesChicksState(state.chicks1 + 1, state.wolves1, state.chicks2 - 1, state.wolves2, "<-C", 0))
#Move two chicks
states.append(WolvesChicksState(state.chicks1 + 2, state.wolves1, state.chicks2 - 2, state.wolves2, "<-CC", 0))
#Move one wolf
states.append(WolvesChicksState(state.chicks1, state.wolves1 + 1, state.chicks2, state.wolves2 - 1, "<-W", 0))
#Move two wolves
states.append(WolvesChicksState(state.chicks1, state.wolves1 + 2, state.chicks2, state.wolves2 - 2, "<-WW", 0))
#Move chick and wolf
states.append(WolvesChicksState(state.chicks1 + 1, state.wolves1 + 1, state.chicks2 - 1, state.wolves2 - 1, "<-CW", 0))
return states
def check_state(self, state):
return state.chicks1 >= 0 and state.chicks2 >= 0 and state.wolves1 >= 0 and state.wolves2 >= 0 and (state.chicks1 >= state.wolves1 or state.chicks1 == 0) and (state.chicks2 >= state.wolves2 or state.chicks2 == 0)
def check_finish(self, state):
return state.chicks1 == 0 and state.wolves1 == 0
WolvesChicksSolver().solve(3, 3)