Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. '''
  4.  
  5. Basic Tic Tac Toe game - Chris Gleason 2015
  6.  
  7. '''
  8.  
  9.  
  10. from random import randint
  11.  
  12.  
  13. whowon = 'no'
  14. coords = [ [ '[1]' , '[2]' , '[3]'] , [ '[4]' ,'[5]' , '[6]' ] , [ '[7]' , '[8]' , '[9]' ] ]
  15.  
  16.  
  17.  
  18. def printmatrix():
  19.  
  20. '''
  21. Function to print out the Tic Tac Toe Matrix
  22. '''
  23.  
  24. print (' This is the matrix of the co-ordinates ')
  25. print ('''
  26. ''')
  27.  
  28.  
  29. print ('' , coords[0][0] , 't' , coords[0][1] , 't' , coords[0][2])
  30. print ('' , coords[1][0] , 't' , coords[1][1] , 't' , coords[1][2])
  31. print ('' , coords[2][0] , 't' , coords[2][1] , 't' , coords[2][2])
  32. print ('')
  33.  
  34. def wincondition():
  35.  
  36. '''
  37. This Fucntion defines wether a player has won and if so who has won before allowing
  38. execution of the playturn function.
  39. '''
  40.  
  41. if coords[0][0] == "[X]" and coords [0][1] == "[X]" and coords[0][2] == "[X]":
  42. iswon = 'yes'
  43. whowon = 'player'
  44.  
  45. elif coords[1][0] == "[X]" and coords [1][1] == "[X]" and coords[1][2] == "[X]":
  46. iswon = 'yes'
  47. whowon = 'player'
  48.  
  49. elif coords[2][0] == "[X]" and coords [2][1] == "[X]" and coords[2][2] == "[X]":
  50. iswon = 'yes'
  51. whowon = 'player'
  52.  
  53. elif coords[0][0] == "[X]" and coords [1][0] == "[X]" and coords[2][0] == "[X]":
  54. iswon = 'yes'
  55. whowon = 'player'
  56.  
  57. elif coords[0][1] == "[X]" and coords [1][1] == "[X]" and coords[2][1] == "[X]":
  58. iswon = 'yes'
  59. whowon = 'player'
  60.  
  61. elif coords[0][2] == "[X]" and coords [1][2] == "[X]" and coords[2][2] == "[X]":
  62. iswon = 'yes'
  63. whowon = 'player'
  64.  
  65. elif coords[0][0] == "[X]" and coords [1][1] == "[X]" and coords[2][2] == "[X]":
  66. iswon = 'yes'
  67. whowon = 'player'
  68.  
  69. elif coords[0][2] == "[X]" and coords [1][1] == "[X]" and coords[2][0] == "[X]":
  70. iswon = 'yes'
  71. whowon = 'player'
  72.  
  73. elif coords[0][0] == "[O]" and coords [0][1] == "[O]" and coords[0][2] == "[O]":
  74. iswon = 'yes'
  75. whowon = 'computer'
  76.  
  77. elif coords[1][0] == "[O]" and coords [1][1] == "[O]" and coords[1][2] == "[O]":
  78. iswon = 'yes'
  79. whowon = 'computer'
  80.  
  81. elif coords[2][0] == "[O]" and coords [2][1] == "[O]" and coords[2][2] == "[O]":
  82. iswon = 'yes'
  83. whowon = 'computer'
  84.  
  85. elif coords[0][0] == "[O]" and coords [1][0] == "[O]" and coords[2][0] == "[O]":
  86. iswon = 'yes'
  87. whowon = 'computer'
  88.  
  89. elif coords[0][1] == "[O]" and coords [1][1] == "[O]" and coords[2][1] == "[O]":
  90. iswon = 'yes'
  91. whowon = 'computer'
  92.  
  93. elif coords[0][2] == "[O]" and coords [1][2] == "[O]" and coords[2][2] == "[O]":
  94. iswon = 'yes'
  95. whowon = 'computer'
  96.  
  97. elif coords[0][0] == "[O]" and coords [1][1] == "[O]" and coords[2][2] == "[O]":
  98. iswon = 'yes'
  99. whowon = 'computer'
  100.  
  101. elif coords[0][2] == "[O]" and coords [1][1] == "[O]" and coords[2][0] == "[O]":
  102. iswon = 'yes'
  103. whowon = 'computer'
  104.  
  105. else:
  106. iswon = 'no'
  107.  
  108. if iswon == 'yes':
  109. print ('The game is over! ' , whowon , ' won!')
  110. exit(0)
  111.  
  112.  
  113. def playturn():
  114.  
  115. '''
  116. This Function adds logic to the players turn to decide wether or not
  117. the space is taken or or not and marks the grid accordingly
  118. '''
  119.  
  120. wincondition()
  121. goodroll = 'no'
  122. while goodroll != 'yes':
  123. playerchoice = input ('Which co-ordinate would you like to mark?')
  124.  
  125. if playerchoice == "1" and coords[0][0] == "[1]":
  126. coords[0][0] = "[X]"
  127. goodroll = 'yes'
  128.  
  129. elif playerchoice == "2" and coords[0][1] == "[2]":
  130. coords[0][1] = "[X]"
  131. goodroll = 'yes'
  132.  
  133. elif playerchoice == "3" and coords[0][2] == "[3]":
  134. coords[0][2] = "[X]"
  135. goodroll = 'yes'
  136.  
  137. elif playerchoice == "4" and coords[1][0] == "[4]":
  138. coords[1][0] = "[X]"
  139. goodroll = 'yes'
  140.  
  141. elif playerchoice == "5" and coords[1][1] == "[5]":
  142. coords[1][1] = "[X]"
  143. goodroll = 'yes'
  144.  
  145. elif playerchoice == "6" and coords[1][2] == "[6]":
  146. coords[1][2] = "[X]"
  147. goodroll = 'yes'
  148.  
  149. elif playerchoice == "7" and coords[2][0] == "[7]":
  150. coords[2][0] = "[X]"
  151. goodroll = 'yes'
  152.  
  153. elif playerchoice == "8" and coords[2][1] == "[8]":
  154. coords[2][1] = "[X]"
  155. goodroll = 'yes'
  156.  
  157. elif playerchoice == "9" and coords[2][2] == "[9]":
  158. coords[2][2] = "[X]"
  159. goodroll = 'yes'
  160.  
  161. else:
  162. print('None of those coordiantes are available!')
  163.  
  164. def compturn():
  165.  
  166. '''
  167. This function adds decision logic to the computers turn, picks a random number
  168. and keeps looping until it picks a valid unchosen spot on the grid.
  169. '''
  170.  
  171. wincondition()
  172. goodroll = 'no'
  173. while goodroll != 'yes':
  174. compchoice = randint(1,9)
  175.  
  176. if compchoice == 1 and coords[0][0] == "[1]":
  177. coords[0][0] = "[O]"
  178. goodroll = 'yes'
  179.  
  180. elif compchoice == 2 and coords[0][1] == "[2]":
  181. coords[0][1] = "[O]"
  182. goodroll = 'yes'
  183.  
  184. elif compchoice == 3 and coords[0][2] == "[3]":
  185. coords[0][2] = "[O]"
  186. goodroll = 'yes'
  187.  
  188. elif compchoice == 4 and coords[1][0] == "[4]":
  189. coords[1][0] = "[O]"
  190. goodroll = 'yes'
  191.  
  192. elif compchoice == 5 and coords[1][1] == "[5]":
  193. coords[1][0] = "[O]"
  194. goodroll = 'yes'
  195.  
  196. elif compchoice == 6 and coords[1][2] == "[6]":
  197. coords[1][0] = "[O]"
  198. goodroll = 'yes'
  199.  
  200. elif compchoice == 7 and coords[2][0] == "[7]":
  201. coords[2][0] = "[O]"
  202. goodroll = 'yes'
  203.  
  204. elif compchoice == 8 and coords[2][1] == "[8]":
  205. coords[2][1] = "[O]"
  206. goodroll = 'yes'
  207.  
  208. elif compchoice == 9 and coords[2][2] == "[9]":
  209. coords[2][2] = "[O]"
  210. goodroll = 'yes'
  211.  
  212. else:
  213. goodroll = 'no'
  214.  
  215. print('Computer choice is: ' , compchoice)
  216.  
  217. print ('Welcome to tic tac toe!')
  218. print ('You are X and the computer is O')
  219.  
  220. while whowon != 'yes':
  221. print ('''
  222. ''')
  223. printmatrix()
  224. playturn()
  225. compturn()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement