Day 6 Tower of Hanoi (A project done by stack)
from stack import Stack print("\nLet's play Towers of Hanoi!!") #Create the Stacks stacks = [] left_stack = Stack("Left") middle_stack = Stack("Middle") right_stack = Stack("Right") stacks.append(left_stack) stacks.append(middle_stack) stacks.append(right_stack) #Set up the Game num_disks = int(input("\nHow many disks do you want to play with?\n")) while num_disks < 3: num_disks = int(input("Enter a number greater than or equal to 3\n")) for i in range(num_disks,0,-1): left_stack.push(i) num_optimal_moves = 2 ** num_disks -1 print("\nThe fastest you can solve this game is in %d moves" %num_optimal_moves ) #Get User Input def get_input(): choices = [stack.get_name()[0] for stack in stacks] while True: for i in range(len(stacks)): name = stacks[i].get_name() letter = choices[i] print("Enter {0} for {1}".format(letter,name)) user_input = input() for i in range(len(stacks)): if user_input == choices[i]: return stacks[i] #Play the Game num_user_moves = 0 while right_stack.get_size() != num_disks: print("\n\n\n...Current Stacks...") for i in stacks: i.print_items() while True: print("\nWhich stack do you want to move from?\n") from_stack = get_input() print("\nWhich stack do you want to move to?\n") to_stack = get_input() if from_stack.is_empty(): print("\n\nInvalid Move. Try Again") elif to_stack.is_empty() or from_stack.peek() < to_stack.peek(): disk = from_stack.pop() to_stack.push(disk) num_user_moves+=1 break else: print("\n\nInvalid Move. Try Again") print("\n\nYou completed the game in {0} moves, and the optimal number of moves is {1}".format(num_user_moves,num_optimal_moves)) print("\n\nYou completed the game in {0} moves, and the optimal number of moves is {1}".format(num_user_moves,num_optimal_moves))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通