Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. from os import system, name
  2. from termcolor import colored
  3. import random
  4.  
  5.  
  6. def print_menu():
  7. system('clear')
  8. print(colored(" TICTACTOE", "red"))
  9. print("")
  10. print(" Wybierz tryb rozgrywki poprzez wpisanie 1,2 lub 3 i zatwierdzenie Enterem:")
  11. print(" 1. Player vs. Player")
  12. print(" 2. Player vs. CPU")
  13. print(" 3. CPU vs. Player")
  14. Input_Done = False
  15. while (Input_Done == False):
  16. Choice = input()
  17. if (Choice == '1' or Choice == '2' or Choice == '3'):
  18. Input_Done = True
  19. else:
  20. print("Brak takiej opcji")
  21. Input_Done = False
  22. return Choice
  23.  
  24.  
  25. def print_board():
  26. system('clear')
  27. print(" _" +
  28. pos[0]+"_|"+"_"+pos[1]+"_|"+"_"+pos[2]+"_")
  29. print(" _" +
  30. pos[3]+"_|"+"_"+pos[4]+"_|"+"_"+pos[5]+"_")
  31. print(" _" +
  32. pos[6]+"_|"+"_"+pos[7]+"_|"+"_"+pos[8]+"_")
  33. # ---------------------------------------------------------------------------------------------------
  34.  
  35.  
  36. def if_Won():
  37. # winners = [
  38. # (0, 1, 2),
  39. # (3, 4, 5),
  40. # (6, 7, 8),
  41. # (0, 3, 6),
  42. # (1, 4, 7),
  43. # (2, 5, 8),
  44. # (0, 4, 8),
  45. # (2, 4, 6),
  46. # ]
  47.  
  48. if(pos[0] == pos[1] and pos[1] == pos[2]):
  49. return True
  50. elif(pos[3] == pos[4] and pos[4] == pos[5]):
  51. return True
  52. elif(pos[6] == pos[7] and pos[7] == pos[8]):
  53. return True
  54. elif(pos[0] == pos[3] and pos[3] == pos[6]):
  55. return True
  56. elif(pos[1] == pos[4] and pos[4] == pos[7]):
  57. return True
  58. elif(pos[2] == pos[5] and pos[5] == pos[8]):
  59. return True
  60. elif(pos[0] == pos[4] and pos[4] == pos[8]):
  61. return True
  62. elif(pos[2] == pos[4] and pos[4] == pos[6]):
  63. return True
  64.  
  65. return False
  66. # ---------------------------------------------------------------------
  67.  
  68.  
  69. def if_Draw():
  70. for i in range(9):
  71. if (pos[i] != 'X' and pos[i] != 'O'):
  72. return False
  73. return True
  74.  
  75.  
  76. def set_board():
  77. for i in range(9):
  78. pos.append(str(i+1))
  79.  
  80.  
  81. def repair_board():
  82. for i in range(9):
  83. pos[i] = str(i+1)
  84.  
  85. pos = []
  86. random_correct_X = False
  87. random_correct_O = False
  88. Input_Done = False
  89. If_Play = True
  90. set_board()
  91. Choice = print_menu()
  92. print_board()
  93. while (If_Play == True):
  94. while(not if_Won()):
  95. while (Input_Done == False):
  96.  
  97. if(Choice == '1' or Choice == '2'):
  98. try:
  99. tempX = int(input("Wybierz pole(X):"))
  100. Pole_Zajete_X = True
  101. Input_Done = True
  102. except:
  103. print("Robisz cos nie tak, sprobuj jeszcze raz!")
  104. else:
  105. while(random_correct_X == False):
  106. tempX = random.randint(0, 9)
  107. if(pos[tempX-1] != 'X' and pos[tempX-1] != 'O'):
  108. random_correct_X = True
  109. random_correct_X = False
  110. Pole_Zajete_X = True
  111. Input_Done = True
  112. Input_Done = False
  113.  
  114. while (Pole_Zajete_X == True):
  115. Pole_Zajete_X = False
  116. if (pos[tempX-1] != "X" and pos[tempX-1] != "O"):
  117. pos[tempX-1] = "X"
  118. print_board()
  119. if(if_Won()):
  120. print("WYGRAL X !!!")
  121. Pole_Zajete_X = False
  122. break
  123.  
  124. else:
  125. print("Pole X zajete")
  126. while (Input_Done == False):
  127. try:
  128. tempX = int(input("Wprowadz jeszcze raz X:"))
  129. Pole_Zajete_X = True
  130. Input_Done = True
  131. except:
  132. print("Robisz cos nie tak, sprobuj jeszcze raz!")
  133. Input_Done = False
  134. if(if_Draw()):
  135. print("Brak mozliwosci dalszych ruchow, REMIS")
  136. Pole_Zajete_X = False
  137. break
  138. if(not if_Won() and not if_Draw()):
  139. while(Input_Done == False):
  140. if(Choice == '1' or Choice == '3'):
  141. try:
  142. tempY = int(input("Wybierz pole(O):"))
  143. Pole_Zajete_O = True
  144. Input_Done = True
  145. except:
  146. print("Robisz cos nie tak, sprobuj jeszcze raz!")
  147. else:
  148. while(random_correct_O == False):
  149. tempY = random.randint(0, 9)
  150. if(pos[tempY-1] != 'X' and pos[tempY-1] != 'O'):
  151. random_correct_O = True
  152. random_correct_O = False
  153. Pole_Zajete_O = True
  154. Input_Done = True
  155.  
  156. Input_Done = False
  157.  
  158. while (Pole_Zajete_O == True):
  159. Pole_Zajete_O = False
  160. if (pos[tempY-1] != "X" and pos[tempY-1] != "O"):
  161. pos[tempY-1] = "O"
  162. print_board()
  163. if(if_Won()):
  164. print("WYGRAL O !!!")
  165. Pole_Zajete_O = False
  166. break
  167. else:
  168. print("Pole O zajete")
  169. while(Input_Done == False):
  170. try:
  171. tempY = int(input("Wprowadz jeszcze raz O:"))
  172. Pole_Zajete_O = True
  173. Input_Done = True
  174. except:
  175. print("Robisz cos nie tak, sprobuj jeszcze raz!")
  176. Input_Done = False
  177. If_play = False
  178. if (input("Jesli chcesz grac dalej wpisz 'tak' i wcisnij enter, aby zakonczyc cokolwiek innego:") == 'tak'):
  179. If_play = True
  180. repair_board()
  181. print_board()
  182. else:
  183. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement