-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfourtozero.py
60 lines (46 loc) · 1.18 KB
/
fourtozero.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
class FourToZero:
def __init__(self, i):
self.position = i
def initial_position(self):
return self.position
def primitive(self, pos):
return pos == 0
def gen_moves(self):
if self.position == 0:
return []
elif self.position == 1:
return [0]
else:
return [self.position - 1, self.position - 2]
def do_moves(self, move):
if self.primitive(self.position):
print("No more tokens remaining, the game is over.")
elif move == 1 or move == 2:
self.position -= move
print(self.position)
else:
print("Invalid move. Remove 1 or 2 tokens per turn.")
state_map = dict()
state_map[0] = 0 #0 stands for losing position, 1 for winning
def main():
for i in range(50):
game = FourToZero(i)
solve(game)
print(state_map)
def solve(game):
children = [state_type(pos) for pos in game.gen_moves()]
if 0 in children:
state_map[game.initial_position()] = 1
else:
state_map[game.initial_position()] = 0
return state_map[game.initial_position()]
def state_type(pos):
if pos in state_map.keys():
return state_map[pos]
children = [state_type(pos) for pos in game.gen_moves]
if 0 in children:
return 1
else:
return 0
if __name__ == "__main__":
main()