turm_von_hanoi.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. a = "a"
  2. b = "b"
  3. c = "c"
  4. A = [a,b,c]
  5. B = []
  6. C = []
  7. aloc = ""
  8. bloc = ""
  9. cloc = ""
  10. def error():
  11. print("Error: You cant do this move.")
  12. #--------------------a-----------------------------------------------------#
  13. def a_location():
  14. global a
  15. global b
  16. global c
  17. global A
  18. global B
  19. global C
  20. global aloc
  21. if a in A:
  22. aloc = "A"
  23. return aloc
  24. elif a in B:
  25. aloc = "B"
  26. return aloc
  27. elif a in C:
  28. aloc = "C"
  29. return aloc
  30. def a_bewegen():
  31. global a
  32. global b
  33. global c
  34. global A
  35. global B
  36. global C
  37. global aloc
  38. global bloc
  39. global cloc
  40. if aloc == bloc or aloc == cloc:
  41. error()
  42. return
  43. elif aloc == "A":
  44. move = input("Wohin möchtest du a bewegen? (B/C) ")
  45. if move == "A":
  46. error()
  47. return
  48. elif aloc == "B":
  49. move = input("Where do you want to move? (A/C) ")
  50. if move == "B":
  51. error()
  52. return
  53. elif aloc == "C":
  54. move = input("Where do you want to move? (A/B) ")
  55. if move == "C":
  56. error()
  57. return
  58. if move == "A":
  59. if cloc == "A" or bloc == "A":
  60. error()
  61. return
  62. else:
  63. A.append(a)
  64. if aloc == "B":
  65. B.remove(a)
  66. elif aloc == "C":
  67. C.remove(a)
  68. elif move == "B":
  69. if cloc == "B" or bloc == "B":
  70. error()
  71. return
  72. else:
  73. B.append(a)
  74. if aloc == "A":
  75. A.remove(a)
  76. elif aloc == "C":
  77. C.remove(a)
  78. elif move == "C":
  79. if cloc == "C" or bloc == "C":
  80. error()
  81. return
  82. else:
  83. C.append(a)
  84. if aloc == "A":
  85. A.remove(a)
  86. elif aloc == "B":
  87. B.remove(a)
  88. #--------------------b-----------------------------------------------------#
  89. def b_location():
  90. global a
  91. global b
  92. global c
  93. global A
  94. global B
  95. global C
  96. global bloc
  97. if b in A:
  98. bloc = "A"
  99. return bloc
  100. elif b in B:
  101. bloc = "B"
  102. return bloc
  103. elif b in C:
  104. bloc = "C"
  105. return bloc
  106. def b_bewegen():
  107. global a
  108. global b
  109. global c
  110. global A
  111. global B
  112. global C
  113. global aloc
  114. global bloc
  115. global cloc
  116. if bloc == cloc :
  117. print("You can't move this.")
  118. return
  119. elif bloc == "A":
  120. move = input("Wohin möchtest du a bewegen? (B/C) ")
  121. if move == "A":
  122. error()
  123. return
  124. elif bloc == "B":
  125. move = input("Where do you want to move? (A/C) ")
  126. if move == "B":
  127. error()
  128. return
  129. elif bloc == "C":
  130. move = input("Where do you want to move? (A/B) ")
  131. if move == "C":
  132. error()
  133. return
  134. if move == "A":
  135. if cloc == "A":
  136. error()
  137. return
  138. else:
  139. A.append(b)
  140. if bloc == "B":
  141. B.remove(b)
  142. elif bloc == "C":
  143. C.remove(b)
  144. elif move == "B":
  145. if cloc == "B":
  146. error()
  147. return
  148. else:
  149. B.append(b)
  150. if bloc == "A":
  151. A.remove(b)
  152. elif bloc == "C":
  153. C.remove(b)
  154. elif move == "C":
  155. if cloc == "C":
  156. error()
  157. return
  158. else:
  159. C.append(b)
  160. if bloc == "A":
  161. A.remove(b)
  162. elif bloc == "B":
  163. B.remove(b)
  164. #--------------------c-----------------------------------------------------#
  165. def c_location():
  166. global a
  167. global b
  168. global c
  169. global A
  170. global B
  171. global C
  172. global aloc
  173. global bloc
  174. global cloc
  175. if c in A:
  176. cloc = "A"
  177. return cloc
  178. elif c in B:
  179. cloc = "B"
  180. return cloc
  181. elif c in C:
  182. cloc = "C"
  183. return cloc
  184. def c_bewegen():
  185. global a
  186. global b
  187. global c
  188. global A
  189. global B
  190. global C
  191. global cloc
  192. if cloc == "A":
  193. move = input("Wohin möchtest du a bewegen? (B/C) ")
  194. if move == "A":
  195. error()
  196. elif cloc == "B":
  197. move = input("Where do you want to move? (A/C) " )
  198. if move == "B":
  199. error()
  200. elif cloc == "C":
  201. move = input("Where do you want to move? (A/B) ")
  202. if move == "C":
  203. error()
  204. if move == "A":
  205. if cloc == "A":
  206. error()
  207. return
  208. else:
  209. A.append(c)
  210. if cloc == "B":
  211. B.remove(c)
  212. elif cloc == "C":
  213. C.remove(c)
  214. elif move == "B":
  215. if cloc == "B":
  216. error()
  217. return
  218. else:
  219. B.append(c)
  220. if cloc == "A":
  221. A.remove(c)
  222. elif cloc == "C":
  223. C.remove(c)
  224. elif move == "C":
  225. if cloc == "C":
  226. error()
  227. return
  228. else:
  229. C.append(c)
  230. if cloc == "A":
  231. A.remove(c)
  232. elif cloc == "B":
  233. B.remove(c)
  234. #----------------------------------Schleife-------------------------------------------#
  235. def game():
  236. while True:
  237. global a
  238. global b
  239. global c
  240. global A
  241. global B
  242. global C
  243. global aloc
  244. global bloc
  245. global cloc
  246. a_location()
  247. b_location()
  248. c_location()
  249. print("Plates:")
  250. print(" a -> large, b -> middle, c -> small")
  251. print(A)
  252. print(B)
  253. print(C)
  254. choose_move = input("Which plate do you want to move? (a/b/c) " )
  255. if choose_move == "a":
  256. a_bewegen()
  257. elif choose_move == "b":
  258. b_bewegen()
  259. elif choose_move == "c":
  260. c_bewegen()
  261. else:
  262. "unvalid input."
  263. #End of game
  264. if C == [a,b,c]:
  265. print("You solved the game! Congratulations!")
  266. trigger = input("Do you want to play again? (y/n) ")
  267. if trigger == "y":
  268. #Variabel reset
  269. A = [a,b,c]
  270. B = []
  271. C = []
  272. aloc = ""
  273. bloc = ""
  274. cloc = ""
  275. #Lauching game
  276. game()
  277. elif trigger == "n":
  278. break
  279. else:
  280. return
  281. game()