Advertisement
Guest User

Untitled

a guest
Feb 6th, 2021
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. # Player 1 is X, player 2 is O
  5. INIT_WEIGHT = 10000
  6.  
  7. class boardState:
  8.     def __init__(self, placements, children, weights):
  9.         self.placements = placements
  10.         self.children = children
  11.         self.weights = weights
  12.        
  13.     def __str__(self):
  14.         def lamba(x):
  15.             if x == 1: return 'x'
  16.             if x == -1: return 'o'
  17.             if x == 0: return '_'
  18.            
  19.         rep = list(map(lamba, self.placements))
  20.         rep.insert(3, '\n')
  21.         rep.insert(7, '\n')
  22.        
  23.         return ''.join(rep)
  24.        
  25. def checkWin(board):
  26.     # Check rows
  27.     if board[0] == board[1] == board[2] and board[0] != 0:
  28.         return board[0]
  29.     if board[3] == board[4] == board[5] and board[3] != 0:
  30.         return board[6]
  31.     if board[6] == board[7] == board[8] and board[6] != 0:
  32.         return board[6]
  33.    
  34.     # Check columns
  35.     if board[0] == board[3] == board[6] and board[0] != 0:
  36.         return board[0]
  37.     if board[1] == board[4] == board[7] and board[1] != 0:
  38.         return board[1]
  39.     if board[2] == board[5] == board[8] and board[2] != 0:
  40.         return board[2]
  41.    
  42.     # Check diagonals
  43.     if board[0] == board[4] == board[8] and board[0] != 0:
  44.         return board[0]
  45.     if board[2] == board[4] == board[6] and board[2] != 0:
  46.         return board[2]
  47.        
  48.     # Otherwise return 0
  49.     return 0
  50.      
  51. def populateMovelist(board, player1=True):
  52.     # Check that we are not at a won game
  53.     if checkWin(board.placements) != 0: return
  54.  
  55.     # Loop over positions in the board for empty ones
  56.     for i in range(9):
  57.         # If we find an empty one we place there
  58.         if board.placements[i] == 0:
  59.             child_placements = board.placements.copy()
  60.             if player1:
  61.                 child_placements[i] = 1
  62.             else:
  63.                 child_placements[i] = -1
  64.                
  65.             # We create the child object, and append it and its initial weight to our list
  66.             child = boardState(child_placements, [], [])
  67.             board.children.append(child)
  68.             board.weights.append(INIT_WEIGHT)
  69.            
  70.             # Finally call this method for our child
  71.             populateMovelist(child, not player1)
  72.  
  73. def makeMove(boardstate):
  74.     """Asks the AI to make a move in a current board state"""
  75.     move = random.choices(
  76.         population=boardstate.children,
  77.         weights=boardstate.weights,
  78.         k=1
  79.     )
  80.    
  81.     return move[0]
  82.  
  83. def playGame():
  84.     moves_made = []
  85.    
  86.     current_player = ai_player
  87.    
  88.     # Go through the rest of the game
  89.     while checkWin(current_player.placements) == 0:
  90.         # Make a move and save it
  91.         current_player = makeMove(current_player)
  92.         moves_made.append(current_player)
  93.        
  94.         # Print board position
  95.         #print(current_player)
  96.         #print()
  97.        
  98.         # Check if we need to break at a draw
  99.         if all(current_player.placements):
  100.             break
  101.  
  102.     # Print that the game has ended, and update weights
  103.     #print("End of game!")
  104.     foo = ai_player
  105.     for i in range(0,len(moves_made)):
  106.         # Find the index of the move made
  107.         weight_index = foo.children.index(moves_made[i])
  108.        
  109.         # If X won
  110.         if checkWin(current_player.placements) == 1:
  111.             if i % 2 == 0:
  112.                 foo.weights[weight_index] += 10
  113.             else:
  114.                 foo.weights[weight_index] -= 10
  115.                 if foo.weights[weight_index] <= 0: foo.weights[weight_index] = 0
  116.                
  117.         # If O won
  118.         if checkWin(current_player.placements) == -1:
  119.             if i % 2 == 0:
  120.                 foo.weights[weight_index] -= 10
  121.                 if foo.weights[weight_index] <= 0: foo.weights[weight_index] = 0
  122.             else:
  123.                 foo.weights[weight_index] += 10
  124.        
  125.         # If it was a draw
  126.         if checkWin(current_player.placements) == 0:
  127.             if i % 2 == 0:
  128.                 foo.weights[weight_index] += 5
  129.             else:
  130.                 foo.weights[weight_index] += 5
  131.    
  132.         foo = foo.children[weight_index]
  133.        
  134.     return checkWin(current_player.placements)
  135.    
  136.  
  137. # Create initial player
  138. board = [0,0,0,0,0,0,0,0,0]
  139. ai_player = boardState(board, [], [])
  140.  
  141. # Recursively fill out possible game states
  142. populateMovelist(ai_player)
  143.  
  144. x_wins = 0
  145. o_wins = 0
  146. draws = 0
  147. games = 0
  148.  
  149. while True:
  150.     winner = playGame()
  151.     if winner == 1: x_wins += 1
  152.     if winner == -1: o_wins += 1
  153.     if winner == 0: draws += 1
  154.     games += 1
  155.    
  156.     if games % 10000 == 0:
  157.         print(x_wins / games * 100)
  158.         print(o_wins / games * 100)
  159.         print(draws / games * 100)
  160.         print()
  161.    
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement