Advertisement
Guest User

Untitled

a guest
Feb 7th, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1.  
  2. '''
  3. Simple tic-tac-toe game built using minimax algorithm. AI recursively traverses game tree branches
  4. by alternating between minimum and maximum score values until it finds the optimal move for
  5. the given scenario.
  6. '''
  7.  
  8. board = [[0, 0, 0],
  9.          [0, 0, 0],
  10.          [0, 0, 0]]
  11.  
  12. HUMAN = 1
  13. COMPUTER = -1
  14.  
  15. def evaluate(currentBoard):
  16.     # returns score based on currentBoard
  17.     if checkGameOver(currentBoard, COMPUTER):
  18.         score = +10
  19.     elif checkGameOver(currentBoard, HUMAN):
  20.         score = -10
  21.     else:
  22.         score = 0
  23.     return score
  24.  
  25.  
  26. def validMove(x, y):
  27.     if [x, y] in getEmptySpots(board):
  28.         return True
  29.     else:
  30.         return False
  31.  
  32.  
  33. def checkGameOver(currentBoard, player):
  34.     # winning combinations
  35.     winState = [
  36.         [currentBoard[0][0], currentBoard[0][1], currentBoard[0][2]],
  37.         [currentBoard[1][0], currentBoard[1][1], currentBoard[1][2]],
  38.         [currentBoard[2][0], currentBoard[2][1], currentBoard[2][2]],
  39.         [currentBoard[0][0], currentBoard[1][0], currentBoard[2][0]],
  40.         [currentBoard[0][1], currentBoard[1][1], currentBoard[2][1]],
  41.         [currentBoard[0][2], currentBoard[1][2], currentBoard[2][2]],
  42.         [currentBoard[0][0], currentBoard[1][1], currentBoard[2][2]],
  43.         [currentBoard[2][0], currentBoard[1][1], currentBoard[0][2]],]
  44.     # check if player has 3 in a row in currentBoard
  45.     if [player, player, player] in winState:
  46.         return True
  47.     else:
  48.         return False
  49.  
  50.  
  51. def isGameOver(currentBoard):
  52.     # returns True if someone won
  53.     return checkGameOver(currentBoard, HUMAN) or checkGameOver(currentBoard, COMPUTER)
  54.  
  55.  
  56. def getEmptySpots(currentBoard):
  57.     # return available spots to move
  58.     emptyCells = []
  59.     for x, row in enumerate(currentBoard):
  60.         for y, cell in enumerate(row):
  61.             if cell == 0:
  62.                 emptyCells.append([x, y])
  63.                 print([x, y])
  64.     print('\n')
  65.     print(len(emptyCells)-1)
  66.     return emptyCells
  67.  
  68.  
  69. def minimax(currentBoard, player):
  70.     if isGameOver(currentBoard):
  71.         score = evaluate(currentBoard)
  72.         return score
  73.     for cell in getEmptySpots(currentBoard):
  74.         x = cell[0]
  75.         y = cell[1]
  76.         currentBoard[x][y] = player
  77.         bestScore = -1000000
  78.         score = minPlay(currentBoard, -player)
  79.         currentBoard[x][y] = 0
  80.         if score > bestScore:
  81.             bestScore = score
  82.             bestMove = cell
  83.             print('Best move:')
  84.             print(bestMove)
  85.             print('\n')
  86.         return bestMove
  87.  
  88.  
  89. def minPlay(currentBoard, player):
  90.     if isGameOver(currentBoard):
  91.         score = evaluate(currentBoard)
  92.         return score
  93.     for cell in getEmptySpots(currentBoard):
  94.         x = cell[0]
  95.         y = cell[1]
  96.         currentBoard[x][y] = player
  97.         bestScore = 1000000
  98.         score = maxPlay(currentBoard, -player)
  99.         currentBoard[x][y] = 0
  100.         if score < bestScore:
  101.             bestScore = score
  102.         return bestScore
  103.  
  104.  
  105. def maxPlay(currentBoard, player):
  106.     if isGameOver(currentBoard):
  107.         score = evaluate(currentBoard)
  108.         return score
  109.     for cell in getEmptySpots(currentBoard):
  110.         x = cell[0]
  111.         y = cell[1]
  112.         currentBoard[x][y] = player
  113.         bestScore = -1000000
  114.         score = minPlay(currentBoard, -player)
  115.         currentBoard[x][y] = 0
  116.         if score > bestScore:
  117.             bestScore = score
  118.         return bestScore
  119.  
  120.  
  121. def setMove(x, y, player):
  122.     board[x][y] = player
  123.  
  124.  
  125. def printGame(gameState):
  126.     print('|' + str(gameState[0][0]) + '|' + str(gameState[0][1]) + '|' + str(gameState[0][2]))
  127.     print('------')
  128.     print('|' + str(gameState[1][0]) + '|' + str(gameState[1][1]) + '|' + str(gameState[1][2]))
  129.     print('------')
  130.     print('|' + str(gameState[2][0]) + '|' + str(gameState[2][1]) + '|' + str(gameState[2][2]))
  131.  
  132.  
  133. def aiMove():
  134.     move = minimax(board, COMPUTER)
  135.     x = move[0]
  136.     y = move[1]
  137.     setMove(x, y, COMPUTER)
  138.  
  139.  
  140. def main():
  141.     moves = {
  142.         1: [0, 0], 2: [0, 1], 3: [0, 2],
  143.         4: [1, 0], 5: [1, 1], 6: [1, 2],
  144.         7: [2, 0], 8: [2, 1], 9: [2, 2],}
  145.     choice = input('Would you like to go first? [y/n]\n')
  146.     if choice == 'n':
  147.         aiMove()
  148.     while not isGameOver(board):
  149.         printGame(board)
  150.         move = int(input('Input coordinate as number from 1-9\n'))
  151.         coordinate = moves[move]
  152.         setMove(coordinate[0], coordinate[1], HUMAN)
  153.         aiMove()
  154.  
  155.  
  156. if __name__ == '__main__':
  157.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement