-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower_of_hanoi
41 lines (36 loc) · 1.2 KB
/
tower_of_hanoi
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
class TowerOfHanoi:
# default constructor
def __init__(self, N=3):
self.disk = np.ones(N, dtype=np.int)
self.size = len(self.disk)
self.peg = tuple(np.arange(1, 4))
def states(self):
print(self.disk)
print(self.peg)
def print_peg(self, p):
print([i for i in range(len(self.disk)) if self.disk[i] ==p])
def move(self, d, p):
if self.is_valid_move(d, p):
self.disk[d] = p
if self.is_winner():
print("Congratulations! You win!")
else:
print("Invalid move")
def is_winner(self):
winner_flag=False
for p in self.peg[1:]:
if np.all(self.disk == p * np.ones(self.size, dtype=np.int)):
return True
return False
def is_valid_move(self, d, p):
if p in self.peg:
disks_on_p = [i for i in range(len(self.disk)) if self.disk[i] == p]
if len(disks_on_p) > 0:
if np.max(disks_on_p) < d:
return False
else:
return True
else:
return True
else:
return False