Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. import random
  2.  
  3. def AI_turn_easy(board, possible_nums, AI_XO):
  4.  
  5. # generates a random, available space on the board and makes it an 'O'
  6. randChoice = random.choice(possible_nums)
  7. possible_nums.remove(randChoice)
  8. board.spaces[randChoice] = AI_XO
  9.  
  10. return possible_nums
  11.  
  12.  
  13. def AI_turn_hard(board, possible_nums, AI_XO):
  14.  
  15. # All of the possible winning configurations
  16. possible_configs = [["1", "2", "3"],
  17. ["1", "4", "7"],
  18. ["1", "5", "9"],
  19. ["2", "5", "8"],
  20. ["3", "6", "9"],
  21. ["3", "5", "7"],
  22. ["4", "5", "6"],
  23. ["7", "8", "9"]]
  24.  
  25. # Shuffles the configurations so the computer doesn't run through the same order every time
  26. random.shuffle(possible_configs)
  27.  
  28. # Check each configuration; if there is two of the three spaces equal each other, put an 'O' in the other space
  29. for config in possible_configs:
  30. if board.spaces[config[0]] == board.spaces[config[1]]:
  31. if board.spaces[config[2]] in possible_nums:
  32. possible_nums.remove(board.spaces[config[2]])
  33. board.spaces[config[2]] = AI_XO
  34. return possible_nums
  35. elif board.spaces[config[0]] == board.spaces[config[2]]:
  36. if board.spaces[config[1]] in possible_nums:
  37. possible_nums.remove(board.spaces[config[1]])
  38. board.spaces[config[1]] = AI_XO
  39. return possible_nums
  40. elif board.spaces[config[1]] == board.spaces[config[2]]:
  41. if board.spaces[config[0]] in possible_nums:
  42. possible_nums.remove(board.spaces[config[0]])
  43. board.spaces[config[0]] = AI_XO
  44. return possible_nums
  45. else:
  46. pass
  47.  
  48. # If there aren't any matches of two in any of the rows, choose a random, available space
  49. return AI_turn_easy(board, possible_nums, AI_XO)
  50.  
  51.  
  52. def AI_turn_impossible(board, possible_nums, AI_XO):
  53. # All of the possible winning configurations
  54. possible_configs = [["1", "2", "3"],
  55. ["1", "4", "7"],
  56. ["1", "5", "9"],
  57. ["2", "5", "8"],
  58. ["3", "6", "9"],
  59. ["3", "5", "7"],
  60. ["4", "5", "6"],
  61. ["7", "8", "9"]]
  62.  
  63. # Shuffles the configurations so the computer doesn't run through the same order every time
  64. random.shuffle(possible_configs)
  65.  
  66. # Check each configuration; if there is two of the three spaces equal each other, put an 'O' in the other space
  67. for config in possible_configs:
  68. if board.spaces[config[0]] == board.spaces[config[1]]:
  69. if board.spaces[config[2]] in possible_nums:
  70. possible_nums.remove(board.spaces[config[2]])
  71. board.spaces[config[2]] = AI_XO
  72. return possible_nums
  73. elif board.spaces[config[0]] == board.spaces[config[2]]:
  74. if board.spaces[config[1]] in possible_nums:
  75. possible_nums.remove(board.spaces[config[1]])
  76. board.spaces[config[1]] = AI_XO
  77. return possible_nums
  78. elif board.spaces[config[1]] == board.spaces[config[2]]:
  79. if board.spaces[config[0]] in possible_nums:
  80. possible_nums.remove(board.spaces[config[0]])
  81. board.spaces[config[0]] = AI_XO
  82. return possible_nums
  83. else:
  84. pass
  85.  
  86. # Take either the middle or a corner piece, which makes it impossible to win (I think)
  87. for space in random.shuffle(['5', '1', '7', '3', '9']):
  88. if space in possible_nums:
  89. possible_nums.remove(space)
  90. board.spaces[space] = AI_XO
  91. return possible_nums
  92.  
  93. return AI_turn_easy(board, possible_nums, AI_XO)
  94.  
  95. from board_class import Board
  96. from difficulty import difficulty
  97. from chooseXO import chooseXO
  98. from AI_turn import AI_turn_easy, AI_turn_hard, AI_turn_impossible
  99. from user_turn import p1_turn
  100. from check_win import check_win
  101. from play_again import play_again
  102. import time
  103.  
  104. def main():
  105. def tic_tac_toe():
  106. # print("n" * n) - makes it appear as if it is a new screen
  107.  
  108. print("n" * 500)
  109.  
  110. diff = difficulty()
  111. time.sleep(1.5)
  112.  
  113. board = Board()
  114. turns_taken = 0
  115. possible_nums = [str(i) for i in range (1,10)]
  116. last_move, AI_XO, p1_XO = chooseXO()
  117.  
  118. while turns_taken < 9:
  119. print("n" * 200)
  120. board.print_board()
  121.  
  122. if last_move == "p1":
  123. print("AI's turn")
  124. time.sleep(1.5)
  125. if diff == "E":
  126. possible_nums = AI_turn_easy(board, possible_nums, AI_XO)
  127. elif diff == "H":
  128. possible_nums = AI_turn_hard(board, possible_nums, AI_XO)
  129. elif diff == "I":
  130. possible_nums = AI_turn_impossible(board, possible_nums, AI_XO)
  131. last_move = "AI"
  132.  
  133. elif last_move == "AI":
  134. possible_nums = p1_turn(board, possible_nums, p1_XO)
  135. last_move = "p1"
  136.  
  137. win = check_win(board, turns_taken)
  138. if win == None:
  139. pass
  140. else:
  141. break
  142.  
  143. turns_taken += 1
  144.  
  145. print("n" * 200)
  146. board.print_board()
  147.  
  148. if win == AI_XO:
  149. print("AI wins. You lose :(")
  150. elif win == p1_XO:
  151. print("You win :) Congratulations!")
  152. elif win == "draw":
  153. print("It was a draw")
  154.  
  155. time.sleep(2)
  156.  
  157. tic_tac_toe()
  158.  
  159. times_played = 1
  160. while times_played < 10:
  161. print("n" * 200)
  162. if play_again():
  163. tic_tac_toe()
  164. times_played += 1
  165. else:
  166. break
  167.  
  168. print("Goodbye")
  169. time.sleep(1.5)
  170. quit()
  171.  
  172. if __name__ == "__main__":
  173. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement