Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. # USER: TCG
  2. # Tic Tac Toe (User vs Computer)
  3.  
  4. import random
  5.  
  6. def place(num,x):
  7. # Returns all the moves made
  8. for i in range(len(x)):
  9. if x[i] == num:
  10. pos = x.index(num)
  11. return x[(pos + 1)]
  12. return str(num)
  13.  
  14. def print_grid(move,player,x):
  15.  
  16. pos_list.extend([move,player])
  17.  
  18. # Grid on which the player and computer play on
  19. grid = ["","-------------n",
  20. "|",place(1,pos_list),"|",place(2,pos_list),"|",place(3,pos_list),"|n",
  21. "|---+---+---|n",
  22. "|",place(4,pos_list),"|",place(5,pos_list),"|",place(6,pos_list),"|n",
  23. "|---+---+---|n",
  24. "|",place(7,pos_list),"|",place(8,pos_list),"|",place(9,pos_list),"|n",
  25. "-------------n"]
  26. if x == 2:
  27. # Only prints one the player has made a move
  28. print 'n', ' '.join(grid)
  29.  
  30. def winner(x,player,xx):
  31.  
  32. # Checks if there is a winner (Really messy, could do with code simplifying)
  33. if ((1 in x and 4 in x and 7 in x)or(1 in x and 2 in x and 3 in x)or(2 in x and 5 in x and 8 in x)or(3 in x and 6 in x and 9 in x)or
  34. (4 in x and 5 in x and 6 in x)or(7 in x and 8 in x and 9 in x)or(1 in x and 5 in x and 9 in x)or(3 in x and 5 in x and 7 in x)):
  35. # If prevents the A.I part from printing the statemnt
  36. if xx <> 1:
  37. print 'n'*5,"'%s'" %player, "HAS WON!"
  38. return 1 == 1
  39.  
  40. def computer_AI_part(listx):
  41. global computer_move
  42.  
  43. # Chceks all possible values which the player can and enter to win and blocks it
  44. for x in range(1,10):
  45. if x not in pos_list:
  46. listx.append(x)
  47. if (winner(listx,'Computer',1)) == True:
  48. del listx[-1]
  49. computer_move = x
  50. return 1
  51. del listx[-1]
  52.  
  53. def computer_and_player():
  54. global computer_move,pos_list,player_list,computer_list
  55. replay,draw = 0,0
  56.  
  57. while True:
  58.  
  59. # Replay's the game
  60. if replay == 1:
  61. restart = raw_input("Would you like to replay?: ")
  62. if restart == "yes":
  63. pass
  64. else:
  65. return
  66. else:
  67. print "nTic Tac Toe - Computer vs You", 'n'*2,"Computer goes firstn"
  68.  
  69. replay,computer_move,players_move,loop_count,pos_list,player_list,computer_list = 0,0,0,0,[],[],[]
  70.  
  71. for each in "XXXXX":
  72. loop_count += 1
  73.  
  74. # Computer's Move
  75. if computer_AI_part(computer_list) or computer_AI_part(player_list) == 1:
  76. pass
  77. else:
  78. while True:
  79. computer_move = random.randint(1,9)
  80. if computer_move in pos_list:
  81. continue
  82. break
  83. computer_list.append(computer_move)
  84. # Prints Grid
  85. print_grid(computer_move,'O',2)
  86.  
  87. if loop_count == 5:
  88. if winner(player_list,'player',2) == True or winner(computer_list,'Computer',2) == True:
  89. pass
  90. else:
  91. print "Match Was a draw!"
  92. replay = 1
  93. break
  94.  
  95. # Checks winner
  96. if winner(computer_list,'Computer',2) == True:
  97. replay = 1
  98. break
  99.  
  100. # Player's Move
  101. while True:
  102. try:
  103. players_move = int(raw_input("n'%s' Enter a value from the grid to plot your move: " %each))
  104. if players_move in pos_list or players_move < 1 or players_move > 9:
  105. print "Enter an available number that's between 1-9"
  106. continue
  107. break
  108. except:
  109. print "Enter a number"
  110.  
  111. player_list.append(players_move)
  112. # Sets player's move for printing
  113. print_grid(players_move,each,1)
  114.  
  115. # Checks winner again
  116. if winner(player_list,'player',1) == True:
  117. print_grid(players_move,each,2)
  118. winner(player_list,'player',2)
  119. replay = 1
  120. break
  121.  
  122. if __name__ == "__main__":
  123. computer_and_player()
  124.  
  125. 'X' Enter a value from the grid to plot your move: exit
  126. Enter a number
  127.  
  128. 'X' Enter a value from the grid to plot your move: leave
  129. Enter a number
  130.  
  131. 'X' Enter a value from the grid to plot your move: quit
  132. Enter a number
  133.  
  134. 'X' Enter a value from the grid to plot your move: ^CEnter a number
  135.  
  136. def place(num,x):
  137. # Returns the value in x at position num
  138. for i, v in enumerate(x):
  139. if v == num:
  140. return x[(i + 1)]
  141. return str(num)
  142.  
  143. template = """
  144. -------------
  145. | {} | {} | {} |
  146. |-----------|
  147. | {} | {} | {} |
  148. |-----------|
  149. | {} | {} | {} |
  150. --------------"""
  151.  
  152.  
  153. if x == 2:
  154. # Only prints if the player has made a move
  155. print template.format(*(place(num + 1, pos_list) for num in range(9)))
  156.  
  157. def winner(x,player,xx):
  158. wins = ((1, 2, 3), (4, 5, 6), (7, 8, 9), # Horizontal
  159. (1, 4, 7), (2, 5, 8), (3, 6, 9), # Vertical
  160. (1, 5, 9), (3, 5, 7)) # Diagonal
  161.  
  162. if any(all(pos in x for pos in win) for win in wins):
  163. if xx != 1:
  164. print 'n'*5, "'{}'".format(player), "HAS WON!"
  165. return True
  166. return False
  167.  
  168. def computer_AI_part(listx):
  169.  
  170. while True:
  171.  
  172. # Replay's the game
  173. if replay == 1:
  174. restart = raw_input("Would you like to replay?: ")
  175. if restart == "yes":
  176. pass
  177. else:
  178. return
  179. else:
  180. print "nTic Tac Toe - Computer vs You", 'n'*2,"Computer goes firstn"
  181.  
  182. while True:
  183.  
  184. # Replay's the game
  185. if replay:
  186. restart = raw_input("Would you like to replay?: ").lower()
  187. if restart in ("y", "yes"):
  188. pass
  189. elif restart in ("n", "no"):
  190. return
  191. else:
  192. print "Say 'yes' or 'no'"
  193. continue
  194. else:
  195. print "nTic Tac Toe - Computer vs You", 'n'*2,"Computer goes firstn"
  196.  
  197. replay,computer_move,players_move,loop_count,pos_list,player_list,computer_list = 0,0,0,0,[],[],[]
  198.  
  199. replay, computer_move, players_move, loop_count = 0, 0, 0, 0
  200. pos_list, player_list, computer_list = [], [], []
  201.  
  202. if computer_move in pos_list:
  203. continue
  204. break
  205.  
  206. if computer_move not in pos_list:
  207. break
  208.  
  209. if winner(computer_list,'Computer',2) == True:
  210.  
  211. except:
  212. print "Enter a number"
  213.  
  214. except ValueError:
  215. print "Enter a number"
  216. except (EOFError, KeyboardInterrupt):
  217. exit()
  218.  
  219. if __name__ == "__main__":
  220. computer_and_player()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement