spacepaste

  1.  
  2. import numpy as np
  3. import random
  4. class Brain(object):
  5. def __init__(self):
  6. self.inputLayerSize = 9
  7. self.outputLayerSize = 1
  8. self.hiddenLayerSize = 14
  9. self.alpha = 0.05
  10. self.W1 = np.random.randn(self.inputLayerSize, self.hiddenLayerSize)
  11. self.W2 = np.random.randn(self.hiddenLayerSize, self.outputLayerSize)
  12. self.winchance = 0
  13. def forward(self, X):
  14. self.z2 = np.dot(X, self.W1)
  15. self.a2 = self.sigmoid(self.z2)
  16. print(self.a2.shape)
  17. self.z3 = np.dot(self.a2, self.W2)
  18. yhat = self.sigmoid(self.z3)
  19. return yhat
  20. def sigmoid(self, z):
  21. return 1/(1+np.exp(-z))
  22. def possiblemoves(self, X):
  23. iX = np.full((9), 3)
  24. np.copyto(iX, X)
  25. possibilities = {}
  26. if playerturn == 2:
  27. iX[iX == 1] = 2
  28. iX[iX == 0] = 1
  29. iX[iX == 2] = 0
  30. iX = iX.tolist()
  31. iterations = 1
  32. for cell in range(0,len(iX)):
  33. if iX[cell] == 3:
  34. newmove = list(iX)
  35. newmove[cell] = 0
  36. newmove = np.asarray(newmove)
  37. newwinchance = self.forward(newmove)[0]
  38. newmove = newmove.tolist()
  39. possibilities[iterations] = [newwinchance, newmove]
  40. iterations = iterations + 1
  41. return possibilities
  42. def backpropogate(self):
  43. self.W1 = (self.alpha * (self.target - self.winchance) * self.winchance * (1 - self.winchance) * self.W2 * self.a2 * (1 - self.a2) * self.lastinput) + self.W2
  44. self.W2 = (self.alpha * (self.target - self.winchance) * self.winchance * (1 - self.winchance) * self.a2) + self.W2
  45. def initialize():
  46. global gameboard
  47. gameboard = np.full((9), 3)
  48. def displayboard():
  49. global gameboard
  50. boardisplay = np.full((9), 0).astype('object')
  51. np.copyto(boardisplay, gameboard)
  52. boardisplay[boardisplay == 3] = ' '
  53. boardisplay[boardisplay == 1] = 'X'
  54. boardisplay[boardisplay == 0] = 'O'
  55. boardisplay = boardisplay.tolist()
  56. print('''
  57. {} | {} | {}
  58. ---+---+---
  59. {} | {} | {}
  60. ---+---+---
  61. {} | {} | {}
  62. '''.format(*boardisplay))
  63. def p1turn():
  64. global playerturn
  65. global gameboard
  66. global firstturn
  67. playerbrain.target = playerbrain.forward(gameboard)
  68. if firstturn == 0:
  69. print('trying to backprop')
  70. playerbrain.backpropogate()
  71. print("backprop success!")
  72. playerbrain.lastinput = gameboard
  73. moves = playerbrain.possiblemoves(gameboard)
  74. gameboard = gameboard.tolist()
  75. bestmove = max(moves, key=moves.get)
  76. gameboard = moves[bestmove][1]
  77. print(gameboard)
  78. firstturn = 0
  79. gameboard = np.asarray(gameboard)
  80. playerturn = 2
  81. def p2turn():
  82. global playerturn
  83. global gameboard
  84. gameboard = gameboard.tolist()
  85. rand = random.randint(0, 8)
  86. if gameboard[rand] == 3:
  87. gameboard[rand] = 1
  88. gameboard = np.asarray(gameboard)
  89. playerturn = 1
  90. else:
  91. gameboard = np.asarray(gameboard)
  92. def checkwin():
  93. global gameboard
  94. checkboard = gameboard.reshape((3, 3))
  95. if np.all(checkboard[0, :] == 1):
  96. return 2
  97. elif np.all(checkboard[1, :] == 1):
  98. return 2
  99. elif np.all(checkboard[2, :] == 1):
  100. return 2
  101. elif np.all(checkboard[:, 0] == 1):
  102. return 2
  103. elif np.all(checkboard[:, 1] == 1):
  104. return 2
  105. elif np.all(checkboard[:, 2] == 1):
  106. return 2
  107. elif np.all(np.diag(checkboard) == 1):
  108. return 2
  109. elif np.all(np.diag(np.fliplr(checkboard)) == 1):
  110. return 2
  111. elif np.all(checkboard[0, :] == 0):
  112. return 1
  113. elif np.all(checkboard[1, :] == 0):
  114. return 1
  115. elif np.all(checkboard[2, :] == 0):
  116. return 1
  117. elif np.all(checkboard[:, 0] == 0):
  118. return 1
  119. elif np.all(checkboard[:, 1] == 0):
  120. return 1
  121. elif np.all(checkboard[:, 2] == 0):
  122. return 1
  123. elif np.all(np.diag(checkboard) == 0):
  124. return 1
  125. elif np.all(np.diag(np.fliplr(checkboard)) == 0):
  126. return 1
  127. else:
  128. return 0
  129. def gameloop():
  130. global checkwn
  131. global firstturn
  132. global playerturn
  133. initialize()
  134. checkwn = 0
  135. playerturn = 1
  136. firstturn = 1
  137. while checkwn == 0:
  138. if 3 in gameboard:
  139. if playerturn == 1:
  140. p1turn()
  141. checkwn = checkwin()
  142. elif playerturn == 2:
  143. p2turn()
  144. checkwn = checkwin()
  145. else:
  146. checkwn = 4
  147. if checkwn == 4:
  148. playerbrain.target = 0
  149. playerbrain.backpropogate()
  150. print("The game was a tie")
  151. elif checkwn == 1:
  152. playerbrain.target = 1
  153. playerbrain.backpropogate()
  154. print("Player {} has won the game".format(checkwn))
  155. else:
  156. print("Player {} has won the game".format(checkwn))
  157. playerbrain = Brain()
  158. for x in range(1, 500):
  159. gameloop()
  160.