-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
57 lines (47 loc) · 1.72 KB
/
common.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
def is_win(game_state):
return game_state[0] == game_state[1] == 0
def boat_is_at_a(game_state):
return game_state[2] == 'a'
def boat_is_at_b(game_state):
return game_state[2] == 'b'
def get_boat_at_a_new_states(game_state):
new_states = []
for missionary in range(game_state[0] + 1):
for cannibal in range(game_state[1] + 1):
if missionary + cannibal < 1 or missionary + cannibal > 2:
continue
new_state = (game_state[0] - missionary,
game_state[1] - cannibal,
'b')
if 0 < new_state[0] < new_state[1]:
continue
if 0 < 3 - new_state[0] < 3 - new_state[1]:
continue
new_states.append(new_state)
return new_states
def get_boat_at_b_new_states(game_state):
new_states = []
for missionary in range(3 - game_state[0] + 1):
for cannibal in range(3 - game_state[1] + 1):
if missionary + cannibal < 1 or missionary + cannibal > 2:
continue
new_state = (game_state[0] + missionary,
game_state[1] + cannibal,
'a')
if 0 < new_state[0] < new_state[1]:
continue
if 0 < 3 - new_state[0] < 3 - new_state[1]:
continue
new_states.append(new_state)
return new_states
def find_best_frontier(frontier_states, side, compute_fn):
min_value = 100000
min_state = -1
for state in frontier_states:
if state[2] != side:
continue
fn = compute_fn(state)
if fn < min_value:
min_value = fn
min_state = state
return min_state, min_value