- 2020-10-29 20:56
*views 4*- Practice programming
- game
- Games
- Python

Direct code :

# The left, middle and right towers are stored in a list left = list() center = list() right = list() """ Initialization function """ def

init(): size = input("( Please enter integer friendly , Unwritten judgment !) Please enter the number of layers :") # Initialization tower list , as 5 layer Tower on the left

1-3-5-7-9, Middle and right 5 individual -1 for i in range(1,int(size) + 1): left.append(i*2-1) center.

append(-1) right.append(-1) return int(size) """ Plot 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="") # Print tower elements for le in range(ta[i]): print("X", end="") # Space after printing

for kong in range(int(size - (ta[i] - 1) / 2)): print(" ", end="") # This floor of the left tower is a space

else: # Print before space for kong in range(size): print(" ", end="") # Print the stick in the middle print("|",

end="") # Print space after for kong in range(size): print(" ", end="") """ Console print results """

def show(size): # modification print("-"*35) # The number of circulation layers is equal to size for i in range(size): # Print left tower

printStyling(i,size,left) # Print middle tower printStyling(i,size,center) # Print right tower

printStyling(i,size,right) # Print a new line per line print() # modification print("-" * 35) """ Judge whether it can be moved or not

takeOff reduce ,putOn increase ,size Number of layers ,tSize and pSize Remaining space """ def judge(takeOff,putOn,size,tSize,

pSize,count): # If the remaining space of the left tower is equal to size, It's empty , There is no element to move if tSize == size: print(" Invalid operation !")

return 0 # If the middle tower is empty , It can be moved if pSize == size: # The last element in the middle is assigned the value of the first element of the left tower putOn[pSize - 1

] = takeOff[tSize] # Assignment of the first element of the left tower -1 takeOff[tSize] = -1 # The remaining space of the left tower +1 tSize += 1 #

The remaining space of the middle tower -1 pSize -= 1 # Steps +1 count += 1 # Move successful , Returns the remaining space and the number of steps return tSize,pSize,count

# If the top element of the middle tower is larger than the top element of the left tower , It can be moved elif putOn[pSize] > takeOff[tSize]: #

The previous element of the current topmost element in the tower (-1) Assign the value of the top element of the left tower putOn[pSize - 1] = takeOff[tSize] # Assignment of the top element of the left tower -1

takeOff[tSize] = -1 # Left tower remaining space +1 tSize += 1 # Remaining space of middle tower -1 pSize -= 1 # Steps +1 count += 1

# Move successful , Returns the remaining space and the number of steps return tSize,pSize,count # Otherwise, it can't be moved else: print(" Invalid operation !") return

0 """ Main running functions """ def main(): # Initialize game size = init() # Storage of original disk space lSize Left tower cSize Central tower

rSize Right tower lSize = 0 cSize = size rSize = size # Storage operation steps count = 0 # Print game introduction print(

" Moving the left tower to the right tower completely is victory !") print(" Left -1 in -2 right -3 Exit, please enter :quit") print(

' For example, input :"1-2" That is to put the top element of the left tower into the middle tower ') print("%d The best number of steps for a layer is %d"%(size,pow(2,size)-1)) # The game goes on

while True: print(" Currently moved %d step "%(count)) # Displays the status of the current tower show(size)

# Judge whether there is no space left in the right tower , No is victory , And quit the game if rSize == 0: if count == pow(2,size)-1: print(

" Congratulations on completing the tower of Hanoi with the least number of steps !") else: print(" Congratulations, you just moved %d Step complete Hanoi Tower Games !"%(count)) break # Get player operation

select= input(" Please operate :") # Move the left tower to the middle tower if select == "1-2": result = judge(left,center,size

,lSize,cSize,count) if result == 0: continue else: lSize,cSize,count = result

# Move the left tower to the 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 # input quit Quit the game elif select

== "quit": break # If you input other unrecognized text , Bye else: print(" Wrong operation !") continue main()

Running results :

Let's have a challenge ! Please support me !

Technology

- Java361 articles
- Python187 articles
- Linux108 articles
- MySQL79 articles
- Vue73 articles
- SpringBoot66 articles
- Spring59 articles
- javascript55 articles
- more...

Daily Recommendation

views 3

©2019-2020 Toolsou All rights reserved,

How to prevent Mac put to sleep ? Tell you 4 This is an effective way be based on C# Students' grades / mis solve element-ui The drop-down box of has a value but cannot be selected Student information management system source code -C language VR,AR and MR The difference between these technologies 【 Data structure and algorithm 6】 Stack implementation of comprehensive calculator utilize python Yes monkey Automatic log analysis Laya Wechat games sub package loading Alibaba enterprise email smtp set up （ practice ）【Java Details of knowledge points 3】 Serializable and Deserialize