Build Your First Game: Tic Tac Toe Python Code for Beginners

Want to make your first game with Python? Great choice! Let’s build a simple and fun classic — Tic Tac Toe. It’s a perfect project for beginners. You’ll learn about loops, conditionals, and functions — all while having fun.

This guide is easy to follow. No long explanations. Just bite-sized steps and a working game at the end. Ready? Let’s jump in!

What You Need

  • A computer with Python installed
  • A text editor (VS Code, Sublime, or even Notepad)
  • Some excitement!

Step 1: Plan the Game

Tic Tac Toe is a 2-player game. Each player picks X or O and places it on a 3×3 grid. First to get three in a row wins.

We want a board, a way to check if someone has won, and a way to switch turns.

Step 2: Create the Game Board

We’ll use a list of 9 elements to represent the 3×3 grid.

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

def print_board():
    print(f"{board[0]} | {board[1]} | {board[2]}")
    print("--+---+--")
    print(f"{board[3]} | {board[4]} | {board[5]}")
    print("--+---+--")
    print(f"{board[6]} | {board[7]} | {board[8]}")

This function sets up the board and prints it nicely. Each time someone makes a move, we’ll call this.

Step 3: Player Moves

Let’s write a function for the player to make a move.

def player_move(icon):
    if icon == "X":
        number = 1
    else:
        number = 2
    print(f"Your turn player {number}")
    choice = int(input("Enter your move (1-9): ").strip())
    if board[choice - 1] == " ":
        board[choice - 1] = icon
    else:
        print("That space is taken!")

This lets players input a number between 1 and 9 to place their icon.

Step 4: Check for a Win

We need to check if someone wins the game after every move.

def is_victory(icon):
    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]] == icon and board[condition[1]] == icon and board[condition[2]] == icon:
            return True
    return False

This function checks every possible win scenario: rows, columns, and diagonals.

Step 5: Make It a Game Loop

Now we just tie it all together in a loop so players keep taking turns until there’s a winner or a draw.

while True:
    print_board()
    player_move("X")
    if is_victory("X"):
        print_board()
        print("X wins! Congratulations!")
        break
    elif " " not in board:
        print("It’s a tie!")
        break

    print_board()
    player_move("O")
    if is_victory("O"):
        print_board()
        print("O wins! Congratulations!")
        break
    elif " " not in board:
        print("It’s a tie!")
        break

This loop runs until someone wins or the board is full. Simple and clean.

Awesome, You Did It!

You just built your first game in Python. High five! 🖐️

This project may be small, but you learned a ton:

  • How to create and print a board
  • How to take user input
  • Using conditionals and lists effectively
  • Building game logic step by step

What’s Next?

  • Add a score counter
  • Make an AI opponent
  • Create a GUI with Tkinter

There’s no limit to what you can build. Start simple, stay curious, and keep coding!