Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2024
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. from array import array
  2. import numpy as np
  3.  
  4. boardState = array('i', [1, 0, 0, 0, -1, 0, 0, 0, 0])
  5.  
  6. boardDisp = array('u', [' '] * 9)
  7.  
  8. minimaxPlayer = 1
  9.  
  10.  
  11. def printBoard(board) :
  12.     i = 0
  13.     while i < len(board):
  14.         if board[i] == 1 :
  15.             boardDisp[i] = 'X'
  16.         elif board[i] == -1 :
  17.          boardDisp[i] = 'O'
  18.         else :
  19.             boardDisp[i] = ' '
  20.         i += 1
  21.     print('-----')
  22.     print(boardDisp[0], boardDisp[1], boardDisp[2])
  23.     print(boardDisp[3], boardDisp[4], boardDisp[5])
  24.     print(boardDisp[6], boardDisp[7], boardDisp[8])
  25.     print('-----')
  26.  
  27. def rowColumn(row, column) : # 0 indexed 0-2
  28.     i = (3 * row) + column
  29.     return i
  30.    
  31. def playerInput(player) :
  32.     row = int(input('Row? '))
  33.     column = int(input('Column? '))
  34.     if boardState[rowColumn(row, column)] == 0 :
  35.         boardState[rowColumn(row, column)] = player
  36.         printBoard(boardState)
  37.     else:
  38.         if checkWin(boardState) == 0 :
  39.             printBoard(boardState)
  40.             print('Square already filled. Try again')
  41.             playerInput(player)
  42.         else :
  43.             return
  44.  
  45. def checkWin(board1) :
  46.     board = np.array(board1).reshape(3, 3)
  47.     if sum(np.abs(boardState)) == 9 :
  48.         return 0
  49.     for i in range(3) :
  50.         if sum(board[i]) == 3 or sum(board[:, i]) == 3 :
  51.             return 1
  52.         elif sum(board[i]) == -3 or sum(board[:, i]) == -3 :
  53.             return -1
  54.     if sum(board.diagonal()) == 3 or sum(np.fliplr(board).diagonal()) == 3 :
  55.         return 1
  56.     elif sum(board.diagonal()) == -3 or sum(np.fliplr(board).diagonal()) == -3 :
  57.         return -1
  58.     else :
  59.         return 2
  60.  
  61.  
  62. def moveSearch(board, player) :
  63.     #printBoard(board)
  64.     if abs(checkWin(board)) == 1 :
  65.         return checkWin(board) * player * -1
  66.     elif checkWin(board) == 0:
  67.         return 0
  68.     elif checkWin(board) == 2 :
  69.         moveList = array('i', [])
  70.         i = 0
  71.         while i < 9 :
  72.             if board[i] == 0 :
  73.                 moveList.append(i)
  74.             i += 1
  75.         j = 0
  76.         valueList = array('f', [0] * len(moveList))
  77.         while j < len(moveList) :
  78.             board[moveList[j]] = player
  79.             valueList[j] = moveSearch(board, -1 * player)
  80.             board[moveList[j]] = 0
  81.             j += 1
  82.         return max(valueList) * -1
  83.  
  84. def topLevelSearch(board, player) :
  85.     printBoard(board)
  86.     if abs(checkWin(board)) == 1 :
  87.         return
  88.     elif checkWin(board) == 0:
  89.         return
  90.     elif checkWin(board) == 2 :
  91.         moveList = array('i', [])
  92.         i = 0
  93.         while i < 9 :
  94.             if board[i] == 0 :
  95.                 moveList.append(i)
  96.             i += 1
  97.         j = 0
  98.         valueList = array('f', [0] * len(moveList))
  99.         while j < len(moveList) :
  100.             board[moveList[j]] = player
  101.             valueList[j] = moveSearch(board, -1 * player)
  102.             board[moveList[j]] = 0
  103.             j += 1
  104.         bestMove = moveList[valueList.index(max(valueList))]
  105.         return bestMove
  106.  
  107.  
  108. minimaxMove = topLevelSearch(boardState, minimaxPlayer)
  109. boardState[minimaxMove] = minimaxPlayer
  110. printBoard(boardState)
  111. print(boardState)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement