AIM:

Create a minimal Tic-Tac-Toe game for single-player (you as X vs. unbeatable AI as O) or multiplayer (X vs. O), using the Minimax algorithm.

Description:

This Tic-Tac-Toe game supports single-player (you as X vs. AI as O) or multiplayer (X vs. O) modes. The board is a 1D list of 9 positions (0=empty, 1=O, -1=X), mapped to a 3x3 grid (positions 1-9). Players input a number (1-9) to place their mark. The AI uses the Minimax algorithm to evaluate all possible moves, ensuring it wins or forces a draw. The game displays the board after each move, checks for a winner (three in a row, column, or diagonal), and announces a win or draw. Key functions:

code:

# Tic-Tac-Toe with Minimax, super simple
def show_board(board): # Print 3x3 board
    for i in range(9):
        if i%3==0 and i>0: print()
        print("X " if board[i]==-1 else "O " if board[i]==1 else "- ", end="")
    print("\\n")

def player1_move(board): # Player X move
    pos = int(input("X's move [1-9]: "))-1
    if 0<=pos<=8 and board[pos]==0: board[pos] = -1
    else: print("Invalid!"); exit()

def player2_move(board): # Player O move
    pos = int(input("O's move [1-9]: "))-1
    if 0<=pos<=8 and board[pos]==0: board[pos] = 1
    else: print("Invalid!"); exit()

def check_winner(board): # Check for winner
    w = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
    for i in w:
        if board[i[0]]!=0 and board[i[0]]==board[i[1]]==board[i[2]]: return board[i[0]]
    return 0

def minimax(board, player): # Minimax for AI
    if check_winner(board): return check_winner(board)*player
    if all(cell!=0 for cell in board): return 0
    best = -2 if player==1 else 2
    for i in range(9):
        if board[i]==0:
            board[i] = player
            score = -minimax(board, -player)
            board[i] = 0
            best = max(score, best) if player==1 else min(score, best)
    return best or 0

def ai_move(board): # AI picks best move
    best, pos = -2, -1
    for i in range(9):
        if board[i]==0:
            board[i] = 1
            score = -minimax(board, -1)
            board[i] = 0
            if score>best: best, pos = score, i
    if pos>=0: board[pos] = 1

def play_game(): # Main game loop
    board = [0]*9
    choice = int(input("1=single, 2=multi: "))
    turn = int(input("1st(1)/2nd(2): ")) if choice==1 else 0
    for i in range(9):
        if check_winner(board): break
        show_board(board)
        if choice==1 and (i+turn)%2==0: ai_move(board)
        elif choice==1: player1_move(board)
        elif i%2==0: player1_move(board)
        else: player2_move(board)
    show_board(board)
    w = check_winner(board)
    print("Draw!" if w==0 else "X Wins!" if w==-1 else "O Wins!")

play_game() # Start game

Sample Output:

1=single, 2=multi: 1
1st(1)/2nd(2): 1

Current Board:
- - -
- - -
- - -

X's move [1-9]: 5
- - -
- X -
- - -

O - -
- X -
- - -

X's move [1-9]: 2
O X -
- X -
- - -

O X O
- X -
- - -

X's move [1-9]: 4
O X O
X X -
- - -

O X O
X X -
- - O

X's move [1-9]: 7
O X O
X X -
X - O

O X O
X X O
X - O

Draw!

Result:

Tic-Tac-Toe game using the Minimax algorithm implemented successfully. The game supports single-player (human vs. unbeatable AI) and multiplayer modes, using a 1D board (0=empty, 1=O, -1=X). It includes functions for board display, player moves, win checking, and AI decision-making.