tic tac toe

December 18, 2025

Code Mario

Super Tic Tac Toe in Python – User & PC Player Guide – 2025

What is Tic Tac Toe Game?

Tic Tac Toe is a simple two-player strategy game played on a 3×3 grid. The players take turns marking empty cells with their respective symbols, usually X and O. The objective of the game is to place three identical symbols in a horizontal, vertical, or diagonal line before the opponent does.

In Artificial Intelligence and programming, Tic Tac Toe is commonly used as an introductory problem to understand game logic, state representation, decision-making, and basic search strategies.

Game Rules:

  1. The game is played on a 3×3 grid.
  2. There are two players: Player X and Player O.
  3. Players take turns placing their symbol in an empty cell.
  4. A player wins if they place three of their symbols in a row, column, or diagonal.
  5. If all cells are filled and no player wins, the game ends in a draw.

Game Representation:

The Tic Tac Toe board can be represented using a simple data structure such as a list or matrix. Each cell of the grid represents a possible game state. The complete board configuration at any time represents the current state of the game.

Possible states include:

  • Empty cells
  • Cells occupied by X
  • Cells occupied by O

Winning Conditions:

A player is declared the winner if any one of the following conditions is satisfied:

  • All three symbols in any row are the same
  • All three symbols in any column are the same
  • All three symbols in any diagonal are the same

These conditions are checked after each move.

Role in Artificial Intelligence:    

Tic Tac Toe is often used to demonstrate fundamental AI concepts such as:

  • Game state evaluation
  • Turn-based decision making
  • Search space exploration
  • Strategy formulation

It serves as a foundation for understanding more complex AI game-playing algorithms such as Minimax.

Statement: Write a program to implement Tic Tac Toe Game in Python.

Procedure:

Steps:

  1. Open VS Code.
  2. Create .py file.
  3. Write the following code:
import random

board = [' ' for _ in range(9)]

def print_board():

    print()

    print(board[0], '|', board[1], '|', board[2])

    print('--+---+--')

    print(board[3], '|', board[4], '|', board[5])

    print('--+---+--')

    print(board[6], '|', board[7], '|', board[8])

    print()

def check_winner(player):

    win_conditions = [

        [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 condition in win_conditions:

        if board[condition[0]] == board[condition[1]] == board[condition[2]] == player:

            return True

    return False

def is_draw():

    return ' ' not in board

def computer_move():

    empty_positions = [i for i in range(9) if board[i] == ' ']

    return random.choice(empty_positions)

def play_game():

    print("You are Player X")

    print("Computer is Player O")

    while True:

        # User move

        print_board()

        move = int(input("Enter your position (1-9): ")) - 1

        if board[move] != ' ':

            print("Position already taken. Try again.")

            continue

        board[move] = 'X'

        if check_winner('X'):

            print_board()

            print("You win!")

            break

        if is_draw():

            print_board()

            print("Game Draw!")

            break

        # Computer move

        comp_move = computer_move()

        board[comp_move] = 'O'

        print(f"Computer chose position {comp_move + 1}")

        if check_winner('O'):

            print_board()

            print("Computer wins!")

            break

play_game()

  • Save and Run the File.

Result:

tic tac toe

Also Check The Falling Crown – Super Indie Game by Mursal Mateen – 2025







Leave a Comment