-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTower.py
172 lines (157 loc) · 5.96 KB
/
Tower.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# ----------------------------------------#
# Michael Roudnitski
# Tower of Hanoi
# March 3, 2017
# ----------------------------------------#
import time
import pygame
pygame.init()
HEIGHT = 600
WIDTH = 1400
ground = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
#---------------------------------------------------------------------------------------------------#
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
disks = input("Enter the number of disks you would like in your tower... ")
steps = 0
source = []
temp = []
dest = []
complete = False
numDisks = disks
#---------------------------------------------------------------------------------------------------#
for i in range(disks): # appends values into source list depending on how many disks the user chose
source.append(numDisks)
numDisks -= 1
#---------------------------------------------------------------------------------------------------#
def AB():
""" Makes a legal move between source and temp
if source is not empty and then temp is empty or last value of source < last value of temp
Pops last item in source and appends it to temp
or
if temp is not empty and then source is empty or last value of temp < last value of source
Pops last item in temp and appends it to source
"""
if len(source) > 0 and (len(temp) == 0 or source[-1] < temp[-1]):
temp.append(source.pop())
elif len(temp) > 0 and (len(source) == 0 or temp[-1] < source[-1]):
source.append(temp.pop())
#---------------------------------------------------------------------------------------------------#
def AC():
""" Makes a legal move between source and destination
if source is not empty and then dest is empty or last value of source < last value of dest
Pops last item in source and appends it to dest
or
if dest is not empty and then source is empty or last value of dest < last value of source
Pops last item in dest and appends it to source
"""
if len(source) > 0 and (len(dest) == 0 or source[-1] < dest[-1]):
dest.append(source.pop())
elif len(dest) > 0 and (len(source) == 0 or dest[-1] < source[-1]):
source.append(dest.pop())
#---------------------------------------------------------------------------------------------------#
def BC():
""" Makes a legal move between temp and destination
if source is not empty and then dest is empty or last value of temp < last value of dest
Pops last item in source and appends it to dest
or
if dest is not empty and then temp is empty or last value of dest < last value of temp
Pops last item in dest and appends it to temp
"""
if len(temp) > 0 and (len(dest) == 0 or temp[-1] < dest[-1]):
dest.append(temp.pop())
elif len(dest) > 0 and (len(temp) == 0 or dest[-1] < temp[-1]):
temp.append(dest.pop())
#---------------------------------------------------------------------------------------------------#
def write():
""" Prints the value of the three lists (source, temp, dest)
>>> write()
source [5,4,3,2]
temp [1]
dest []
"""
print "source ", source
print "temp ", temp
print "dest ", dest
print " "
#---------------------------------------------------------------------------------------------------#
def is_done():
""" Checks if the destination list is full (all values are stored in dest list)
if it returns true, it breaks the loop
source = []
temp = []
dest = [5,4,3,2,1]
>>> is_done()
All done!
dest [5,4,3,2,1]
"""
if len(dest) != disks:
pass
else:
print "All done! "
print "That only took ", steps, " steps"
print "dest ", dest
return True
#---------------------------------------------------------------------------------------------------#
def redraw_screen():
pygame.event.clear()
screen.fill(WHITE)
#pegs
pygame.draw.rect(screen, BLACK, ((WIDTH / 4) - 5, 200, 10, 380))
pygame.draw.rect(screen, BLACK, ((WIDTH / 2) - 5, 200, 10, 380))
pygame.draw.rect(screen, BLACK, (WIDTH - (WIDTH / 4) - 5, 200, 10, 380))
# disks
for i, val in enumerate(source):
pygame.draw.rect(screen, RED, ((WIDTH / 4) - (val * 10), ground - (20 * i) - 40, (val * 10) * 2, 20))
for i, val in enumerate(temp):
pygame.draw.rect(screen, RED, ((WIDTH / 2) - (val * 10), ground - (20 * i) - 40, (val * 10) * 2, 20))
for i, val in enumerate(dest):
pygame.draw.rect(screen, RED, (WIDTH - (WIDTH / 4) - (val * 10), ground - (20 * i) - 40, (val * 10) * 2, 20))
pygame.display.update()
#time.sleep(0.1)
#---------------------------------------------------------------------------------------------------#
redraw_screen()
if disks % 2 == 0: #check if number of disks are even or odd
while complete is False:
AB() # make the legal move
steps += 1 #increase step counter
write() # write current state of lists to console
redraw_screen() # draw everything
if is_done(): # break the loop if we're done
break
AC()
steps += 1
write()
redraw_screen()
if is_done():
break
BC()
steps += 1
write()
redraw_screen()
if is_done():
break
else:
while complete is False:
AC()
steps += 1
write()
redraw_screen()
if is_done():
break
AB()
steps += 1
write()
redraw_screen()
if is_done():
break
BC()
steps += 1
write()
redraw_screen()
if is_done():
break