Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. #This is a game of Tic Tac Toe, coded by Aziz Hayat.
  2. from random import *
  3.  
  4. #GameOn = True
  5. #col = 0
  6. #row = 0
  7. #end = 0
  8.  
  9. board = []
  10. difficulty = ''
  11.  
  12. print "*************************************"
  13. print "Welcome to the game of Tic Tac Toe! Built by Aziz!"
  14. print "*************************************"
  15. print "In this game, you will be playing against SkyNet!"
  16.  
  17. for x in range(3):
  18. board.append(["#", "#", "#"])
  19.  
  20. def print_board(board):
  21. for row in board:
  22. print " ".join(row)
  23.  
  24. def row_position(number):
  25. row = 0
  26.  
  27. if number == 1:
  28. row = 0
  29. elif number == 2:
  30. row = 0
  31. elif number == 3:
  32. row = 0
  33. elif number == 4:
  34. row = 1
  35. elif number == 5:
  36. row = 1
  37. elif number == 6:
  38. row = 1
  39. elif number == 7:
  40. row = 2
  41. elif number == 8:
  42. row = 2
  43. elif number == 9:
  44. row = 2
  45.  
  46. return row
  47.  
  48. def col_position(number):
  49. col = 0
  50.  
  51. if number == 1:
  52. col = 0
  53. elif number == 2:
  54. col = 1
  55. elif number == 3:
  56. col = 2
  57. elif number == 4:
  58. col = 0
  59. elif number == 5:
  60. col = 1
  61. elif number == 6:
  62. col = 2
  63. elif number == 7:
  64. col = 0
  65. elif number == 8:
  66. col = 1
  67. elif number == 9:
  68. col = 2
  69.  
  70. return col
  71.  
  72. while difficulty != 'easy' and difficulty != 'hard':
  73. difficulty = str(raw_input("What difficulty level do you prefer; easy or hard? (type it out) \n"))
  74. difficulty = difficulty.lower()
  75.  
  76. def player_choice():
  77. print "Enter the number of the empty position you select:"
  78. print "Top Left = 1"
  79. print "Top Center = 2"
  80. print "Top Right = 3"
  81. print "Center Left = 4"
  82. print "Dead Center = 5"
  83. print "Center Right = 6"
  84. print "Bottom Left = 7"
  85. print "Bottom Center = 8"
  86. print "Bottom Right = 9"
  87. print "********"
  88. print_board(board)
  89.  
  90. flag = 0
  91. #proceed = False
  92.  
  93. while flag == 0:
  94. player_choice = 0
  95. while player_choice > 9 or player_choice < 1:
  96. player_choice = int(raw_input("Enter Number: "))
  97.  
  98. test_row = row_position(player_choice)
  99. test_col = col_position(player_choice)
  100.  
  101. if board[test_row][test_col] == "#":
  102. flag = 1
  103. else:
  104. print 'Uh-Oh! Try Again! Choose a spot without an X or an O!'
  105.  
  106.  
  107. if flag == 1:
  108. player_row = row_position(player_choice)
  109. player_col = col_position(player_choice)
  110. board[player_row][player_col] = "X"
  111.  
  112. def comp_choice():
  113. flag = 0
  114. comp_choice = 0
  115.  
  116. while flag != 1:
  117. comp_choice = int(randint(1,10))
  118.  
  119. comp_row = row_position(comp_choice)
  120. comp_col = col_position(comp_choice)
  121.  
  122. if board[comp_row][comp_col] == "#":
  123. board[comp_row][comp_col] = "O"
  124. flag = 1
  125. else:
  126. flag = 0
  127.  
  128. ###################################################
  129. def comp_choice_hard():
  130. flag = 0
  131. while flag != 1:
  132. '''
  133. Need to figure out what to do here in case it fails.
  134. If user inputs 5 the computer does not make a move.
  135. '''
  136. if board[0][0] == 'X' and board[2][2] == '#':
  137. board[2][2] = 'O'
  138. flag = 1
  139. elif board [0][2] == 'X' and board[2][0] == '#':
  140. board[2][0] = 'O'
  141. flag = 1
  142. elif board[2][0] == 'X' and board[0][2] == '#':
  143. board[0][2] = 'O'
  144. flag = 1
  145. elif board [2][2] == 'X' and board[0][0] == '#':
  146. board[0][0] = 'O'
  147. flag = 1
  148. elif board[0][1] == 'X' and board[2][1] == '#':
  149. board[2][1] = 'O'
  150. flag = 1
  151. elif board[2][1] == 'X' and board[0][1] == '#':
  152. board[0][1] = 'O'
  153. flag = 1
  154. elif board[2][0] == 'X' and board[2][2] == '#' :
  155. board[2][2] = 'O'
  156. flag = 1
  157. elif board[2][2] == 'X' and board[2][0] == '#':
  158. board[2][0] = 'O'
  159. flag = 1
  160. ####################################################
  161.  
  162. def check_win(n):
  163. if board[0][0] == n and board[1][0] == n and board[2][0] == n:
  164. print n, " Wins! Congratulations!"
  165. #GameOn = False
  166. return 1
  167. #Checks left vertical win
  168. elif board[0][1] == n and board[1][1] == n and board[2][1] == n:
  169. print n, " Wins! Congratulations!"
  170. # GameOn = False
  171. return 1
  172. #Checks center vertical win
  173. elif board[0][2] == n and board[1][2] == n and board[2][2] == n:
  174. print n, " Wins! Congratulations!"
  175. #GameOn = False
  176. return 1
  177. #Checks right vertical win
  178. elif board[0][0] == n and board[0][1] == n and board[0][2] == n:
  179. print n, " Wins! Congratulations!"
  180. #GameOn = False
  181. return 1
  182. #Checks top horizontal win
  183. elif board[1][0] == n and board[1][1] == n and board[1][2] == n:
  184. print n, " Wins! Congratulations!"
  185. #GameOn = False
  186. return 1
  187. #Checks center horizontal win
  188. elif board[2][0] == n and board[2][1] == n and board[2][2] == n:
  189. print n, " Wins! Congratulations!"
  190. #GameOn = False
  191. return 1
  192. #Checks bottom horizontal win
  193. elif board[0][0] == n and board[1][1] == n and board[2][2] == n:
  194. print n, " Wins! Congratulations!"
  195. #GameOn = False
  196. return 1
  197. #Checks top left to bottom right diagonal win
  198. elif board[0][2] == n and board[1][1] == n and board[2][0] == n:
  199. print n, " Wins! Congratulations!"
  200. #GameOn = False
  201. return 1
  202. #Checks top right to bottom left diagonal win
  203. else:
  204. return 0
  205.  
  206. def check_tie(board):
  207. check = 0
  208. for i in board:
  209. for j in i:
  210. if j == '#':
  211. check = 1
  212.  
  213. while check == 0:
  214. print "Looks like a tie!"
  215. break
  216.  
  217. #Main game loop if difficulty = easy:
  218. if difficulty == 'easy':
  219. end1 = 0
  220. end2 = 0
  221.  
  222. while end1 == 0 or end2 == 0:
  223.  
  224. player_choice()
  225. print_board(board)
  226. end1 = check_win("X")
  227. if end1 != 0:
  228. break
  229. check_tie(board)
  230. comp_choice()
  231. #print_board(board)
  232. end2 = check_win("O")
  233. if end2 != 0:
  234. print_board(board)
  235. break
  236.  
  237. #Main game loop if difficulty = hard:
  238. if difficulty == 'hard':
  239. end1 = 0
  240. end2 = 0
  241. diff_count = 1
  242.  
  243. while end1 == 0 or end2 == 0:
  244.  
  245. player_choice()
  246. print_board(board)
  247. end1 = check_win("X")
  248. if end1 != 0:
  249. break
  250. check_tie(board)
  251. if diff_count % 2 == 0:
  252. comp_choice_hard()
  253. diff_count += 1
  254. else:
  255. comp_choice()
  256. diff_count += 1
  257.  
  258. #print_board(board)
  259. end2 = check_win("O")
  260. if end2 != 0:
  261. print_board(board)
  262. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement