Home Python Project Tic-Tac-Toe using Python: A Classic Game, Coded!

Tic-Tac-Toe using Python: A Classic Game, Coded!

by adarshpal
8 minutes read

In this article, you’ll learn how to build Tic-Tac-Toe using Python and more.

Tic-Tac-Toe using Python game is very popular amongst all of us and even fun to build as a Python project. I am pretty sure most of us know how to play it but let me give a quick brush up. 

If you are not familiar with Tic-Tac-Toe, play it visually here to understand. Don’t worry, even if you don’t understand it, we are going to see it.

Tic-Tac-Toe using Python: A Classic Game, Coded!

Tic-Tac-Toe, a game of strategy and simple fun, is a perfect project for beginners learning Python. It allows you to apply fundamental programming concepts like lists, loops, conditional statements, and functions in a practical and engaging way. In this article, we’ll guide you through creating a Tic-Tac-Toe game in Python.

Game Logic:

Before diving into the code, let’s outline the game’s logic:

  1. Game Board: We’ll represent the 3×3 Tic-Tac-Toe board using a list of lists.
  2. Players: Two players take turns marking their symbols (‘X’ or ‘O’) on the board.
  3. Turns: Players alternate turns until either one wins or the board is full (a draw).
  4. Win Condition: A player wins if they have three of their symbols in a row (horizontally, vertically, or diagonally).
  5. Input: We’ll use input() to get the players’ moves.
  6. Validation: We’ll need to validate player input to ensure they choose a valid, empty cell.

The Code

def display_board(board):
    print("-------------")
    for row in board:
        print("| " + " | ".join(row) + " |")
        print("-------------")

def player_input():
    marker = ''
    while not (marker == 'X' or marker == 'O'):
        marker = input("Player 1, do you want to be X or O? ").upper()

    if marker == 'X':
        return ('X', 'O')
    else:
        return ('O', 'X')

def place_marker(board, marker, position):
    board[position // 3][position % 3] = marker

def win_check(board, mark):
    return ((board[0][0] == mark and board[0][1] == mark and board[0][2] == mark) or # across the top
    (board[1][0] == mark and board[1][1] == mark and board[1][2] == mark) or # across the middle
    (board[2][0] == mark and board[2][1] == mark and board[2][2] == mark) or # across the bottom
    (board[0][0] == mark and board[1][0] == mark and board[2][0] == mark) or # down the left side
    (board[0][1] == mark and board[1][1] == mark and board[2][1] == mark) or # down the middle
    (board[0][2] == mark and board[1][2] == mark and board[2][2] == mark) or # down the right side
    (board[0][0] == mark and board[1][1] == mark and board[2][2] == mark) or # diagonal
    (board[0][2] == mark and board[1][1] == mark and board[2][0] == mark)) # diagonal

import random  # This line should not be indented

def choose_first():
    if random.randint(0, 1) == 0:
        return 'Player 2'
    else:
        return 'Player 1'

def space_check(board, position):
    return board[position // 3][position % 3] == ' '

def player_choice(board, current_player):
    position = 0
    while position not in range(1, 10) or not space_check(board, position-1):
        try:
            position = int(input(f"{current_player}, choose a position (1-9): "))
        except ValueError:
            print("Invalid input. Please enter a number between 1 and 9.")
    return position - 1

def replay():
    return input("Do you want to play again? Enter Yes or No: ").lower().startswith('y')

print('Welcome to Tic-Tac-Toe!')

while True:
    theBoard = [[' '] * 3 for _ in range(3)]
    player1_marker, player2_marker = player_input()
    turn = choose_first()
    print(turn + ' will go first.')
    game_on = True

    while game_on:
        if turn == 'Player 1':
            current_player = "Player 1"
            display_board(theBoard)
            position = player_choice(theBoard, current_player)
            place_marker(theBoard, player1_marker, position)

            if win_check(theBoard, player1_marker):
                display_board(theBoard)
                print('Congratulations! Player 1 has won!')
                game_on = False
            else:
                if all(space_check(theBoard, i) == False for i in range(9)):
                    display_board(theBoard)
                    print('The game is a draw!')
                    game_on = False
                else:
                    turn = 'Player 2'

        else:
            current_player = "Player 2"
            display_board(theBoard)
            position = player_choice(theBoard, current_player)
            place_marker(theBoard, player2_marker, position)

            if win_check(theBoard, player2_marker):
                display_board(theBoard)
                print('Congratulations! Player 2 has won!')
                game_on = False
            else:
                if all(space_check(theBoard, i) == False for i in range(9)):
                    display_board(theBoard)
                    print('The game is a draw!')
                    game_on = False
                else:
                    turn = 'Player 1'

    if not replay():
        break

Output

Welcome to Tic-Tac-Toe!
Player 1, do you want to be X or O? X
Player 1 will go first.
-------------
| | | |
-------------
| | | |
-------------
| | | |
-------------
Player 1, choose a position (1-9): 5
-------------
| | | |
-------------
| | X | |
-------------
| | | |
-------------
Player 2, choose a position (1-9): 3
-------------
| | | O |
-------------
| | X | |
-------------
| | | |
-------------
Player 1, choose a position (1-9): 6
-------------
| | | O |
-------------
| | X | X |
-------------
| | | |
-------------
Player 2, choose a position (1-9): 1
-------------
| O | | O |
-------------
| | X | X |
-------------
| | | |
-------------
Player 1, choose a position (1-9): 4
-------------
| O | | O |
-------------
| X | X | X |
-------------
| | | |
-------------
Congratulations! Player 1 has won!
Do you want to play again? Enter Yes or No:

Explanation

This Python code implements the game of Tic-Tac-Toe. It’s structured using functions for better organization.

The Functions:

  • display_board(board): Shows the current state of the Tic-Tac-Toe board in the console. It takes the board (a list of lists) as input and prints it with separators to resemble the game grid.
  • player_input(): Gets Player 1’s choice of marker (X or O). It uses a loop to ensure the input is valid and returns a tuple containing Player 1’s marker and Player 2’s marker (which will be the opposite).
  • place_marker(board, marker, position): Places the given marker (X or O) on the board at the specified position (0-8, representing the 9 cells). It calculates the row and column index from the position and updates the board.
  • win_check(board, mark): Checks if the given marker has won the game by checking all possible winning combinations (rows, columns, and diagonals). It returns True if the marker has won, False otherwise.
  • choose_first(): Randomly decides which player goes first.
  • space_check(board, position): Checks if a specific position on the board is empty (contains a space).
  • player_choice(board, current_player): Gets the current player’s move. It prompts the player for a position (1-9), validates the input to ensure it’s within the valid range and the chosen cell is empty, and returns the position (0-8).
  • replay(): Asks the players if they want to play another game.

The Main Game Logic:

The game runs in a while True loop, allowing for multiple games. Inside this loop:

1. Initialization: A new empty board is created, player markers are assigned, and the starting player is randomly chosen.

2. Game Loop (while game_on): This loop handles the individual turns.

3. Player Turns: The code alternates between Player 1 and Player 2. For each player:

  • The board is displayed.
  • The player is prompted for their move.
  • The marker is placed on the board.
  • The win_check function is called to see if the player has won. If so, the game ends.
  • A check is also made to see if it is a draw. If so, the game ends.
  • If no one has won yet, the turn switches to the other player.

4. Replay: After a game finishes (either by a win or a draw), the replay function asks if the players want to play again. If not, the outer loop breaks, and the game ends.

Thanks For Scrolling, I hope you liked this article on Tic-Tac-Toe using Python. Hit me up with your views and suggestions in the comment down below.🙌

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.