Straight to the code.
# Left center and right towers are stored in a list left = list() center = list() right = list() """ Initialization Functions """ def init(): size = input("(Please enter a friendly integer.,unwritten judgment!)Please enter the number of floors:") #Initialize the list of towers, say 5 floors. Left tower puts 1-3-5-7-9, center and right puts 5 -1s. for i in range(1,int(size) + 1): (i*2-1) (-1) (-1) return int(size) """ Print Style Functions """ def printStyling(i,size,ta): if ta[i] != -1: # Space before printing for kong in range(int(size - (ta[i] - 1) / 2)): print(" ", end="") # Printing tower elements for le in range(ta[i]): print("X", end="") # Space after print for kong in range(int(size - (ta[i] - 1) / 2)): print(" ", end="") # This level of the left tower is a space else: # Print leading spaces for kong in range(size): print(" ", end="") # Print the middle stick print("|", end="") # Print trailing spaces for kong in range(size): print(" ", end="") """ Console Print Results """ def show(size): #modify print("-"*35) # The number of loop levels is equal to size for i in range(size): # Print the left tower printStyling(i,size,left) # Print intermediate towers printStyling(i,size,center) # Print the right tower printStyling(i,size,right) # Print one newline per line print() #modify print("-" * 35) """ Determine if it can be moved takeOff decreases, putOn increases, size layers, tSize and pSize remaining space. """ def judge(takeOff,putOn,size,tSize,pSize,count): # If the space in the left tower is empty, there's no element to move # if takeOff == size: print("Operation invalid!") return 0 # If the center tower is empty, it can be moved # if pSize == size: # The last element in the center is assigned the value of the first element in the left tower. putOn[pSize - 1] = takeOff[tSize] # The first element of the left tower is assigned a value of -1. takeOff[tSize] = -1 # Remaining space in the left tower +1 tSize += 1 # Remaining space in the central tower -1 pSize -= 1 # of steps +1 count += 1 #Move successfully, return the remaining space and the number of steps. return tSize,pSize,count # If the top element of the center tower is larger than the top element of the left tower, then it can be moved. elif putOn[pSize] > takeOff[tSize]: # The top element of the center tower is assigned the value of the top element of the left tower to the top element of the center tower (-1). putOn[pSize - 1] = takeOff[tSize] # Assign -1 to the topmost element of the left tower. takeOff[tSize] = -1 # +1 space remaining in the left tower tSize += 1 # Remaining space in the central tower - 1 pSize -= 1 # of steps +1 count += 1 # Move successfully, return the remaining space and the number of steps return tSize,pSize,count # Otherwise no movement # else: print("Operation invalid!") return 0 """ Main Run Functions """ def main(): #Initialize the game size = init() # Initial disk space remaining lSize left tower cSize middle tower rSize right tower lSize = 0 cSize = size rSize = size #Store operation steps count = 0 Introduction to the #PrintGame print("Moving the left tower intact to the right tower is a victory!") print("Left-1 Center-2 Right-3 To exit, type :quit.") print('For example, typing : "1-2" is putting the top element of the left tower into the center tower') print("The optimal number of steps for layer %d is %d."%(size,pow(2,size)-1)) # The game goes on while True: print("Currently moving %d steps."%(count)) # Show current tower status show(size) # Determine if there is no space left in the right tower, if there is no space left, you win and exit the game. if rSize == 0: if count == pow(2,size)-1: print("Congratulations on completing Hannukah's Tower using the least number of steps!") else: print("Congratulations on moving only %d steps to complete the Hannukah Tower mini-game!"%(count)) break #Get the player action select = input("Please operate:") # Left tower to center tower if select == "1-2": result = judge(left,center,size,lSize,cSize,count) if result == 0: continue else: lSize,cSize,count = result # Left tower to right tower, same below # elif select == "1-3": result = judge(left, right, size, lSize, rSize,count) if result == 0: continue else: lSize, rSize,count = result elif select == "2-1": result = judge(center, left, size, cSize, lSize,count) if result == 0: continue else: cSize, lSize,count = result elif select == "2-3": result = judge(center, right, size, cSize, rSize,count) if result == 0: continue else: cSize, rSize,count = result elif select == "3-1": result = judge(right, left, size, rSize, lSize,count) if result == 0: continue else: rSize, lSize,count = result elif select == "3-2": result = judge(right, center, size, rSize, cSize,count) if result == 0: continue else: rSize, cSize ,count= result # Type quit to exit the game elif select == "quit": break #If you're typing something else that's not recognized, bye-bye. else: print("Operational error!") continue main()
Results.
This article on the detailed python 100 lines of effective code to achieve the Hannauta game (minimalist version) of the article is introduced to this, more related python Hannauta content please search my previous posts or continue to browse the relevant articles below I hope that you will support me more in the future!