here is my code : from tkinter import * import time class TowerOfHanoi: def __init__(self, num_disks): self.num_disks = num_disks self.moves = 0 self.towers = [[i for i in range(num_disks, 0, -1)], [], []] # Initialize GUI self.root = Tk() self.root.title("Tower of Hanoi") self.root.geometry("400x400") self.canvas = Canvas(self.root, width=400, height=400) self.canvas.pack() self.disk_height = 20 self.disk_width_unit = 20 self.disk_color = "#3399FF" self.rod_height = 200 self.rod_width = 5 self.rod_color = "#000000" self.draw_towers() self.draw_disks() self.root.mainloop() def draw_towers(self): # Draw the three rods for i in range(3): x0 = 50 + i*125 y0 = 350 x1 = x0 + self.rod_width y1 = y0 - self.rod_height self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.rod_color) def draw_disks(self): # Draw the disks on the rods for i in range(3): for j in range(len(self.towers[i])): disk_width = self.disk_width_unit * self.towers[i][j] x0 = 50 + i*125 - disk_width/2 y0 = 350 - (j+1)*self.disk_height x1 = x0 + disk_width y1 = y0 + self.disk_height self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.disk_color) def move_disk(self, from_rod, to_rod): if len(self.towers[from_rod]) == 0: return False if len(self.towers[to_rod]) == 0 or self.towers[from_rod][-1] < self.towers[to_rod][-1]: disk = self.towers[from_rod].pop() self.towers[to_rod].append(disk) self.moves += 1 return True else: return False def make_move(self, from_rod, to_rod): if self.move_disk(from_rod, to_rod): # calculate the x and y coordinates of the disk to be moved disk_width = self.disk_width_unit * self.towers[to_rod][-1] x0 = 50 + to_rod * 125 - disk_width / 2 y0 = 150 - len(self.towers[to_rod]) * self.disk_height # animate the movement of the disk for y in range(y0, 350, 5): self.canvas.delete("disk") self.draw_towers() for i in range(3): if i == from_rod: continue for j in range(len(self.towers[i])): disk_width = self.disk_width_unit * self.towers[i][j] x0 = 50 + i * 125 - disk_width / 2 y0 = 350 - (j + 1) * self.disk_height x1 = x0 + disk_width y1 = y0 + self.disk_height self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.disk_color, tags="disk") x1 = x0 + disk_width y1 = y0 + self.disk_height self.canvas.create_rectangle(x0, y, x1, y1, fill=self.disk_color, tags="disk") self.canvas.update() time.sleep(0.01) self.canvas.delete("disk") self.draw_towers() self.draw_disks() self.check_win() else: print("Invalid move") def check_win(self): if len(self.towers[0]) == 0 and len(self.towers[1]) == 0: self.canvas.create_text(200, 100, text=f"You win in {self.moves} moves!", font=("Arial", 24)) def solve(self, n, from_rod, to_rod, aux_rod): if n == 1: self.make_move(from_rod, to_rod) else: self.solve(n - 1, from_rod, aux_rod, to_rod) self.make_move(from_rod, to_rod) self.solve(n - 1, aux_rod, to_rod, from_rod) def move_all_disks_to_rod(self, start_rod, end_rod, temp_rod, n): if n == 1: self.make_move(start_rod, end_rod) else: self.move_all_disks_to_rod(start_rod, temp_rod, end_rod, n - 1) self.make_move(start_rod, end_rod) self.move_all_disks_to_rod(temp_rod, end_rod, start_rod, n - 1) # Start the game with 3 disks game = TowerOfHanoi(3) game.move_all_disks_to_rod(0, 2, 1, 3) ---------------------------------------------------------------- plz make sure to help me fix the issue , i need the disks to move from rod 1 to rod 3 and win the game , its the toer of hanoi, mus tbe done on python and via tkinter GUI
here is my code :
from tkinter import *
import time
class TowerOfHanoi:
def __init__(self, num_disks):
self.num_disks = num_disks
self.moves = 0
self.towers = [[i for i in range(num_disks, 0, -1)], [], []]
# Initialize GUI
self.root = Tk()
self.root.title("Tower of Hanoi")
self.root.geometry("400x400")
self.canvas = Canvas(self.root, width=400, height=400)
self.canvas.pack()
self.disk_height = 20
self.disk_width_unit = 20
self.disk_color = "#3399FF"
self.rod_height = 200
self.rod_width = 5
self.rod_color = "#000000"
self.draw_towers()
self.draw_disks()
self.root.mainloop()
def draw_towers(self):
# Draw the three rods
for i in range(3):
x0 = 50 + i*125
y0 = 350
x1 = x0 + self.rod_width
y1 = y0 - self.rod_height
self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.rod_color)
def draw_disks(self):
# Draw the disks on the rods
for i in range(3):
for j in range(len(self.towers[i])):
disk_width = self.disk_width_unit * self.towers[i][j]
x0 = 50 + i*125 - disk_width/2
y0 = 350 - (j+1)*self.disk_height
x1 = x0 + disk_width
y1 = y0 + self.disk_height
self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.disk_color)
def move_disk(self, from_rod, to_rod):
if len(self.towers[from_rod]) == 0:
return False
if len(self.towers[to_rod]) == 0 or self.towers[from_rod][-1] < self.towers[to_rod][-1]:
disk = self.towers[from_rod].pop()
self.towers[to_rod].append(disk)
self.moves += 1
return True
else:
return False
def make_move(self, from_rod, to_rod):
if self.move_disk(from_rod, to_rod):
# calculate the x and y coordinates of the disk to be moved
disk_width = self.disk_width_unit * self.towers[to_rod][-1]
x0 = 50 + to_rod * 125 - disk_width / 2
y0 = 150 - len(self.towers[to_rod]) * self.disk_height
# animate the movement of the disk
for y in range(y0, 350, 5):
self.canvas.delete("disk")
self.draw_towers()
for i in range(3):
if i == from_rod:
continue
for j in range(len(self.towers[i])):
disk_width = self.disk_width_unit * self.towers[i][j]
x0 = 50 + i * 125 - disk_width / 2
y0 = 350 - (j + 1) * self.disk_height
x1 = x0 + disk_width
y1 = y0 + self.disk_height
self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.disk_color, tags="disk")
x1 = x0 + disk_width
y1 = y0 + self.disk_height
self.canvas.create_rectangle(x0, y, x1, y1, fill=self.disk_color, tags="disk")
self.canvas.update()
time.sleep(0.01)
self.canvas.delete("disk")
self.draw_towers()
self.draw_disks()
self.check_win()
else:
print("Invalid move")
def check_win(self):
if len(self.towers[0]) == 0 and len(self.towers[1]) == 0:
self.canvas.create_text(200, 100, text=f"You win in {self.moves} moves!", font=("Arial", 24))
def solve(self, n, from_rod, to_rod, aux_rod):
if n == 1:
self.make_move(from_rod, to_rod)
else:
self.solve(n - 1, from_rod, aux_rod, to_rod)
self.make_move(from_rod, to_rod)
self.solve(n - 1, aux_rod, to_rod, from_rod)
def move_all_disks_to_rod(self, start_rod, end_rod, temp_rod, n):
if n == 1:
self.make_move(start_rod, end_rod)
else:
self.move_all_disks_to_rod(start_rod, temp_rod, end_rod, n - 1)
self.make_move(start_rod, end_rod)
self.move_all_disks_to_rod(temp_rod, end_rod, start_rod, n - 1)
# Start the game with 3 disks
game = TowerOfHanoi(3)
game.move_all_disks_to_rod(0, 2, 1, 3)
----------------------------------------------------------------
plz make sure to help me fix the issue , i need the disks to move from rod 1 to rod 3 and win the game , its the toer of hanoi, mus tbe done on python and via tkinter GUI
Step by step
Solved in 5 steps with 5 images