Guest User

Untitled

a guest
May 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. import copy
  2. import sys
  3. from random import choice
  4.  
  5. NEXT_PLAYER = {'X':'O', 'O':'X'}
  6.  
  7. class board(list):
  8. WINS = ((0,1,2), (3,4,5), (6,7,8), (0, 3, 6), (1,4,7), (2,5,8), (0,4,8), (2,4,6))
  9. def done(self):
  10. for w in board.WINS:
  11. # print("SELF.BOARD_CHECK: ", self.board[w[0]], self.board[w[1]], self.board[w[2]])
  12. if self[w[0]] == self[w[1]] == self[w[2]]:
  13. if self[w[0]] == 'X':
  14. return True, 'X'
  15. elif self[w[0]] == 'O':
  16. return True, 'O'
  17. if all([x in ['O', 'X'] for x in self]) == True:
  18. return True, 'TIE'
  19. else:
  20. return False, 'GOING'
  21. def printboard(self):
  22. rows = self[0:3], self[3:6], self[6:]
  23.  
  24. print("\n")
  25. for row in reversed(rows):
  26. for cell in row:
  27. if isinstance(cell, int) or cell.isdigit():
  28. cell = str(int(cell) + 1)
  29. print(cell, end=" | ")
  30. print("\n--------------")
  31.  
  32. def empty_spots(self):
  33. return [int(i) for i in self if i.isdigit()]
  34.  
  35. class Move:
  36. def __init__(self, score=0, idx=0):
  37. self.score = score
  38. self.idx = idx
  39.  
  40. class Game():
  41.  
  42. def __init__(self, player='X', difficulty=9):
  43. self.current_player = 'X'
  44. self.difficulty = difficulty
  45. self.board = board([str(i) for i in range(9)])
  46. self.AI_PLAYER = False
  47. # 0 1 2
  48. # 3 4 5
  49. # 6 7 8
  50.  
  51. def change_player(self):
  52. self.current_player = NEXT_PLAYER[self.current_player]
  53.  
  54.  
  55. def getbestmove(self, board, player):
  56. done, winner = board.done()
  57. # print("BOARD: ", board)
  58. if done is True:
  59. if winner == self.AI_PLAYER:
  60. return Move(10, None)
  61. elif winner != 'TIE': #human
  62. return Move(-10, None)
  63. else:
  64. return Move(0, None)
  65.  
  66. empty_spots = board.empty_spots()
  67. # print("EMPTY INDICES: ", empty_spots)
  68. moves = []
  69. for idx in empty_spots:
  70. newboard = copy.copy(board)
  71. newboard[idx] = player
  72. move = Move()
  73. move.score = self.getbestmove(newboard, NEXT_PLAYER[player]).score
  74. move.idx = idx
  75. moves.append(move)
  76.  
  77. if player == self.AI_PLAYER:
  78. return max(moves, key=lambda m: m.score)
  79. else:
  80. return min(moves, key=lambda m: m.score)
  81.  
  82.  
  83. def startgame(self):
  84. while True:
  85. self.board.printboard()
  86. # print(self.AI_PLAYER, self.current_player)
  87. if self.AI_PLAYER != self.current_player:
  88. move = input("Enter move: ")
  89. self.board[int(move)-10] = self.current_player
  90. else:
  91. if self.current_player == self.AI_PLAYER:
  92. if len(self.board.empty_spots()) <= self.difficulty:
  93. print("AI MOVE..")
  94. move = self.getbestmove(self.board, self.AI_PLAYER)
  95. self.board[move.idx] = self.AI_PLAYER
  96. else:
  97. print("RANDOM GUESS")
  98. self.board[choice(self.board.empty_spots())] = self.AI_PLAYER
  99.  
  100. self.change_player()
  101. done, winner = self.board.done()
  102.  
  103. if done is True:
  104. self.board.printboard()
  105. if winner == 'TIE':
  106. print("TIE")
  107. else:
  108. print("WINNER IS :", winner )
  109. break
  110.  
  111. def main():
  112. g = Game(player='X')
  113. g.AI_PLAYER = 'O'
  114. g.startgame()
  115.  
  116. if __name__ == '__main__':
  117. main()
Add Comment
Please, Sign In to add comment