Note that there are some explanatory texts on larger screens.

plurals
  1. POTicTacToe AI - Minimax Algorithm
    primarykey
    data
    text
    <p>I've just been busy creating an AI for TicTacToe in python. I got the whole thing running the first "round", but when it comes to the second the AI just does nothing, the board just keeps the same. Maybe someone could help me find the errors in my code.</p> <pre><code>import math def drawBoard(): print(board[0] + board[1] + board[2] + "\n" + board[3] + board[4] + board[5] + "\n" + board[6] + board[7] + board[8] + "\n") def getUserMov e(): uin = 0 uin = int(input("Enter your move(" + playerLet + "): ")) while uin &gt; 9 or uin &lt; 1 or board[uin-1] != ' ': print("Invalid Move") uin = int(input("Enter your move (" + playerLet + "): ")) return uin def userMove(): global board move = getUserMove() makeMove(board, move, playerLet) def comMove(): global board val = minimax([board, 0, 0], 8, True) makeMove(board, val[2], comLet) def minimax(node, depth, maximizing): value = getStateValue(node[0], comLet, playerLet) if depth == 0 or math.isinf(value): return [board, value, 0] moves = getPossibleMoves(node[0]) if maximizing: best = [node[0], float("-inf"), 0] for i in moves: boardcopy = [] + node[0] makeMove(boardcopy, i, comLet) val = minimax([boardcopy, value, i], depth - 1, False) if best[1] &lt; val[1]: best = [] + val return best else: best = [node[0], float("inf"), 0] for i in moves: boardcopy = [] + node[0] makeMove(boardcopy, i, playerLet) val = minimax([boardcopy, value, i], depth - 1, True) if best[1] &gt; val[1]: best = [] + val return best def getStateValue(board, maxletter, minletter): val = float(0) if checkWon(board, maxletter): val = float("+inf") if checkWon(board, minletter): val = float("-inf") return val def makeMove(board, move, letter): board[move - 1] = letter def getPossibleMoves(board): pMoves = [] for i in range(0, 8): if board[i] == ' ': pMoves.append(i+1) return pMoves def checkWon(board, letter): if ((board[0] == letter and board[1] == letter and board[2] == letter) or (board[3] == letter and board[4] == letter and board[5] == letter) or (board[6] == letter and board[7] == letter and board[8] == letter) or (board[0] == letter and board[3] == letter and board[6] == letter) or (board[1] == letter and board[4] == letter and board[7] == letter) or (board[2] == letter and board[5] == letter and board[8] == letter) or (board[0] == letter and board[4] == letter and board[8] == letter) or (board[2] == letter and board[4] == letter and board[6] == letter)): return True else: return False board = [' '] * 9 playerLet = 'X' comLet = 'O' won = '' print("--- TicTacToe ---") print("Input like this: \n123\n456\n789") while True: userMove() drawBoard() if checkWon(board, playerLet): won = 'Player' break comMove() drawBoard() if checkWon(board, comLet): won = 'Computer' break print(won + " won!") </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload