Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import random
  2.  
  3. def GameBeg():
  4. print("Witaj w grze kółko i krzyżyk. Tak wygląda mapa współrzędnych w które będziesz wpisywał swój wybór: ")
  5. i = 1
  6. print(" ⎯⎯⎯⎯⎯⎯⎯⎯  ⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯⎯")
  7. while i <= 9:
  8. print("| ",i ," ", sep = "", end = "")
  9. if i % 3 == 0:
  10. print("|\n ⎯⎯⎯⎯⎯⎯⎯⎯  ⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯⎯")
  11. i += 1
  12. print("Powodzenia!\n")
  13.  
  14. def MainGame(game):
  15. turn = 0
  16. while turn < 9:
  17. print("ruch:", turn)
  18. cords_good = 0
  19. if turn % 2 == 0:
  20. player_no = 1
  21. else:
  22. player_no = 2
  23.  
  24. def GetAIMove():
  25. while True:
  26. if turn > 1:
  27. i = 0
  28. while i < 9:
  29. temp_str = ""
  30. temp_str += str(game[i])
  31. temp_str += str(game[i + 1])
  32. temp_str += str(game[i + 2])
  33. if temp_str == "011":
  34. return 0 + i
  35. if temp_str == "101":
  36. return 1 + i
  37. if temp_str == "110":
  38. return 2 + i
  39. i = int(i) + 3
  40.  
  41. i = 0
  42. while i < 3:
  43. temp_str = ""
  44. temp_str += str(game[i])
  45. temp_str += str(game[i + 3])
  46. temp_str += str(game[i + 6])
  47. if temp_str == "011":
  48. return 0
  49. if temp_str == "101":
  50. return 3 + i
  51. if temp_str == "110":
  52. return 6 + i
  53. i = int(i) + 1
  54.  
  55. temp_str = str(game[0]) + str(game[4]) + str(game[8])
  56. if temp_str == "011":
  57. return 0
  58. if temp_str == "101":
  59. return 4
  60. if temp_str == "110":
  61. return 8
  62.  
  63. temp_str = str(game[2]) + str(game[4]) + str(game[6])
  64. if temp_str == "011":
  65. return 2
  66. if temp_str == "101":
  67. return 4
  68. if temp_str == "110":
  69. return 6
  70.  
  71. i = random.randint(0, 8)
  72. if game[i] == 0:
  73. return int(i)
  74. else:
  75. pass
  76.  
  77. def GetPlayerMove():
  78. while cords_good == 0:
  79. try:
  80. print("TWÓJ RUCH. Wprowadź miejsce: ", sep="", end="")
  81. i = int(input()) - 1
  82. if 0 <= int(i) < 9:
  83. if game[int(i)] == 0:
  84. return int(i)
  85. else:
  86. print('To miejsce jest zajęte. Spróbuj ponownie...')
  87. pass
  88. else:
  89. print('Spróbuj ponownie...')
  90. pass
  91. except ValueError as e:
  92. print('Spróbuj ponownie...')
  93. pass
  94.  
  95. if player_no == 1:
  96. cord = GetPlayerMove()
  97. else:
  98. cord = GetAIMove()
  99. print('AI wybiera {}.'.format(cord + 1))
  100.  
  101. game[cord] = player_no
  102.  
  103. DrawGameBoard(game)
  104. turn += 1
  105. winner = CheckWinner(game)
  106. if turn == 9:
  107. print("Remis.")
  108. return 0
  109. if winner == 1:
  110. print("Brawo, wygrałeś.")
  111. return 0
  112. if winner == 2:
  113. print("Niestety tym razem przegrałeś.")
  114. return 0
  115.  
  116. def DrawGameBoard(game):
  117. i = 1
  118. print(" ⎯⎯⎯⎯⎯⎯⎯⎯  ⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯⎯")
  119. while i <= 9:
  120. print("| ",game[i - 1] ," ", sep = "", end = "")
  121. if i % 3 == 0:
  122. print("|\n ⎯⎯⎯⎯⎯⎯⎯⎯  ⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯⎯")
  123. i += 1
  124.  
  125. def CheckWinner(game):
  126. for i in range(3):
  127. if game[i] == game[i + 1] == game[i + 2] != 0:
  128. return game[i]
  129. i += 3
  130.  
  131. for i in range(3):
  132. if game[i] == game[i + 3] == game[i + 6] != 0:
  133. return game[i]
  134. i += 1
  135.  
  136. if game[0] == game[4] == game[8] != 0:
  137. return game[0]
  138. elif game[2] == game[4] == game[6] != 0:
  139. return game[2]
  140.  
  141. return 0
  142.  
  143. my_game = [0]*9
  144.  
  145. GameBeg()
  146. MainGame(my_game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement