Write a smart tic tac-toe AI, In Python.
- User
Write a smart tic tac-toe AI, In Python.
- Assistant
Write a smart tic tac-toe AI, In Python.
Creating a smart AI for Tic-Tac-Toe that plays optimally can be quite a challenge, but fortunately, Tic-Tac-Toe is a solved game. This means every game can end in a win for one player or a tie, given that both players are playing their best.
One of the most effective and simplest methods to implement a smart AI for Tic-Tac-Toe is by using the Minimax algorithm. Minimax is a backtracking algorithm used in decision making and game theory to find the optimal move for a player, assuming that the opponent is also playing optimally.
Here's a basic implementation of a Tic-Tac-Toe AI in Python using Minimax:
import math
def print_board(board):
for row in board:
print("|".join(row))
print("-" * 5)
def check_win(board, player):
win_conditions = [
[board[0][0], board[0][1], board[0][2]],
[board[1][0], board[1][1], board[1][2]],
[board[2][0], board[2][1], board[2][2]],
[board[0][0], board[1][0], board[2][0]],
[board[0][1], board[1][1], board[2][1]],
[board[0][2], board[1][2], board[2][2]],
[board[0][0], board[1][1], board[2][2]],
[board[2][0], board[1][1], board[0][2]],
]
return [player, player, player] in win_conditions
def get_available_moves(board):
return [(r, c) for r in range(len(board)) for c in range(len(board[0])) if board[r][c] == " "]
def minimax(board, depth, is_maximizing, player, opponent):
if check_win(board, player):
return 1
elif check_win(board, opponent):
return -1
elif not get_available_moves(board):
return 0
if is_maximizing:
best_score = -math.inf
for move in get_available_moves(board):
board[move[0]][move[1]] = player
score = minimax(board, depth + 1, False, player, opponent)
board[move[0]][move[1]] = " "
best_score = max(score, best_score)
return best_score
else:
best_score = math.inf
for move in get_available_moves(board):
board[move[0]][move[1]] = opponent
score = minimax(board, depth + 1, True, player, opponent)
board[move[0]][move[1]] = " "
best_score = min(score, best_score)
return best_score
def best_move(board, player, opponent):
move = (-1, -1)
best_score = -math.inf
for cell in get_available_moves(board):
board[cell[0]][cell[1]] = player
score = minimax(board, 0, False, player, opponent)
board[cell[0]][cell[1]] = " "
if score > best_score:
best_score = score
move = cell
return move
# Example usage:
board = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]
]
ai_player = "X"
human_player = "O"
# AI making the first move
move = best_move(board, ai_player, human_player)
board[move[0]][move[1]] = ai_player
print_board(board)
This code provides a basic Tic-Tac-Toe game AI. best_move function is the entry point to the AI, which chooses the best move by evaluating all possible outcomes using the minimax function. You can enhance this AI by adding features like a difficulty level, which can limit the depth of the minimax algorithm or by using Alpha-Beta pruning to reduce the number of nodes evaluated in the minimax algorithm. Note, to create a complete game, you'd need to add more code to alternate turns between the AI and a human player, and to check for end game conditions after each move.