Advertisement
Guest User

bot.py

a guest
Nov 21st, 2019
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. #!/usr/bin/python
  2. """###################################
  3.  
  4. DISCLAIMER:
  5. This program was written by
  6. Teddy Robbins
  7. in the spring of 2017 for a
  8. high school coding project.
  9.  
  10. It is being re-used in a modified
  11. format for use in a final project
  12. for EN1 at Tufts University in the
  13. fall of 2018.
  14.  
  15. #################################"""
  16.  
  17. positionAndBlock = {1:3,2:2,3:3,4:2,5:4,6:2,7:3,8:2,9:3} #list of positions as the key for the number of moves they block
  18.  
  19. def scanBoard(board, winCombos):
  20.  
  21. ####VARIABLE DECLARATIONS####
  22. possibleMoves = []
  23. cornerstrat=False
  24.  
  25. winCombosList=[]
  26. for i in winCombos:
  27. winCombosList.append(list(i))
  28.  
  29. #print winCombosList
  30.  
  31. winPossible=False
  32. winPossibleCombo=[]
  33.  
  34. ####MAKING POSSIBLE MOVES####
  35. for i in board:
  36. if not i == "X" and not i == "O":
  37. possibleMoves.append(i)
  38.  
  39. #############CORNERSTRAT SCAN##############
  40. moveTally=0
  41. for i in board: #counts how many moves have been made in the game
  42. try:
  43. if i.isalpha():
  44. moveTally=moveTally+1
  45. except AttributeError:
  46. moveTally=0
  47.  
  48. if moveTally<4: #specifically detects for a strategy to conquer 3 corners in TicTacToe
  49. if board[4]=="O":
  50. if (board[0]=="X" and board[8]=="X") or (board[2]=="X" and board[6]=="X"):
  51. cornerstrat=True
  52.  
  53. ####MAKING WINCOMBOS####
  54. indexI=-1
  55. for i in board:
  56. indexI=indexI+1
  57. #print "Looking at: "+ str(i)
  58. if i=="O":
  59. #print "INDEX == "+str(indexI)
  60. previousNumber=indexI+1
  61. #print "Previous number: "+str(previousNumber)
  62. for j in winCombosList:
  63. for k in j:
  64. #print "Just looked at element: "+str(k)+" in list: "+str(j)
  65. if k == previousNumber:
  66. #print "MATCHED"
  67. j.insert(j.index(k), "O")
  68. j.remove(k)
  69. #print "J NOW == "+str(j)
  70. elif i=="X":
  71. # print "INDEX == "+str(indexI)
  72. previousNumber=indexI+1
  73. #print "Previous number: "+str(previousNumber)
  74. for j in winCombosList:
  75. for k in j:
  76. #print "Just looked at element: "+str(k)+" in list: "+str(j)
  77. if k == previousNumber:
  78. #print "MATCHED"
  79. j.insert(j.index(k), "X")
  80. j.remove(k)
  81. #print "J NOW == "+str(j)
  82.  
  83. for i in winCombosList: #checks to see if it is possible to win the game
  84. if 'O' in i and not "X" in i:
  85. tally = 0
  86. for j in i:
  87. if j=='O':
  88. tally=tally+1
  89. if tally>1:
  90. winPossible=True
  91. #print "WIN POSSIBLE"
  92. winPossibleCombo = i
  93.  
  94.  
  95. if winPossible==False:
  96. #print 'WIN NOT POSSIBLE THIS TIME'
  97. winPossibleCombo=[]
  98. winCombosNoO=winCombosList
  99. for i in winCombosNoO[:]: #removes all the wincombos with a O because they are already blocked
  100. #print str(i) + " :JUST LOOPED THROUGH"
  101. if 'O' in i:
  102. #print str(i) + ": FOUND A O IN"
  103. winCombosNoO.remove(i)
  104. #print "PASSED THROUGH: "
  105. #print possibleMoves, winCombosNoO, winPossible, winPossibleCombo
  106. return possibleMoves, winCombosNoO, winPossible, winPossibleCombo, cornerstrat
  107. else:
  108. #print "PASSED THROUGH: "
  109. #print possibleMoves, winCombosList, winPossible, winPossibleCombo
  110. return possibleMoves, winCombosList, winPossible, winPossibleCombo, cornerstrat
  111.  
  112. #print "SCANNED BOARD. BOARD == "+str(board)
  113.  
  114. #print "WINCOMBOS NOW == "+str(winCombos)
  115.  
  116.  
  117.  
  118.  
  119.  
  120. def chooseMove(possibleMoves, winCombos, winPossible, winPossibleCombo, cornerstrat):
  121.  
  122. #print "------------------------"
  123. #print "Choosing Move:"
  124.  
  125. ####VARIABLE DECLARATIONS####
  126.  
  127. choice = 0
  128.  
  129. doubleX=False
  130. singleX=False
  131.  
  132. doubleXList=[]
  133. singleXList=[]
  134.  
  135. ########################OFFENSE############################
  136. if winPossible:
  137. #print "WIN POSSIBLE"
  138. for i in winPossibleCombo:
  139. if not i == "O":
  140. choice = i
  141. return choice
  142.  
  143. ####################CORNERSTRAT#####################
  144.  
  145. if cornerstrat:
  146. for i in possibleMoves:
  147. if i%2==0:
  148. choice=i
  149. return choice
  150.  
  151. ########################DEFENSE############################
  152.  
  153. xInCombos=0 #counter for #of x in combos
  154.  
  155. winCombosDict={} #win combo index as key for #of X's in that combo
  156.  
  157. for i in winCombos:
  158. xInCombos=0 #set to 0
  159. for j in i:
  160. if j=="X":
  161. xInCombos=xInCombos+1 #if it finds an X add one
  162. if xInCombos==2:
  163. winCombosDict[winCombos.index(i)]=2 #assign value 2 to key index of i
  164. elif xInCombos==1:
  165. winCombosDict[winCombos.index(i)]=1 #assign 1
  166. else:
  167. winCombosDict[winCombos.index(i)]=0 #assign 0
  168.  
  169. #print "WinCombosDict now == "+str(winCombosDict)
  170.  
  171. for i in range(0,len(winCombos)):
  172. if winCombosDict[i]==2: #if there are 2 X in any win combo, doubleX = true
  173. doubleX=True
  174. doubleXList.append(i) #add the index of the wincombo in winCombos to doubleXList
  175. elif winCombosDict[i]==1: #same as above but for if there is only 1 x in a wincombo
  176. singleX=True
  177. singleXList.append(i)
  178.  
  179. if doubleX: #if there is an instance of doubleX
  180. #print "DOUBLEX"
  181. if len(doubleXList)>1: #if there are 2 combos with 2 x's we've lost
  182. print("AI Lost.")
  183. choice = random.choice(possibleMoves) #make a random choice
  184. return choice
  185. else:
  186. #print "SINGLE INSTANCE DOUBLEX"
  187. for i in winCombos[doubleXList[0]]: #otherwise, if there is only one combo with doubleX then block it.
  188. if not i=="X":
  189. choice = i
  190. elif singleX and not doubleX: #if there is an instance of singleX
  191. #print "SINGLEX"
  192. if len(singleXList)>1: #more than one instance
  193. #print "MORE THAN 1 INSTANCE SINGLEX"
  194. #print singleXList
  195. choiceSet=[]
  196. choiceSetBlocks=[]
  197. for i in singleXList:
  198. for j in winCombos[i]:
  199. if type(j)==int:
  200. choiceSet.append(j)
  201. choiceSetBlocks.append(positionAndBlock[j])
  202. choice = max(set(choiceSet), key=choiceSet.count)
  203. count = 0
  204. for i in choiceSet:
  205. if i == choice:
  206. count=count+1
  207. if not count>1:
  208. choice = choiceSet[choiceSetBlocks.index(max(choiceSetBlocks))]
  209.  
  210. else: #one instance
  211. #print "ONLY 1 INSTANCE SINGLEX"
  212. choiceSet=[]
  213. choiceSetBlocks=[]
  214. for i in winCombos[singleXList[0]]:
  215. if not i=="X":
  216. choiceSet.append(i)
  217. choiceSetBlocks.append(positionAndBlock[i])
  218. choice = choiceSet[choiceSetBlocks.index(max(choiceSetBlocks))]
  219. elif not singleX and not doubleX:
  220. #print "NO THREATS"
  221. choiceSet=[]
  222. choiceSetBlocks=[]
  223. for i in winCombos:
  224. for j in i:
  225. if type(j)==int:
  226. choiceSet.append(j)
  227. choiceSetBlocks.append(positionAndBlock[j])
  228. choice = choiceSet[choiceSetBlocks.index(max(choiceSetBlocks))]
  229.  
  230. if not choice in possibleMoves:
  231. print("ERROR")
  232.  
  233. return choice
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement