Tic-Tac-Toe Game in Python

In this article we’ll see how to develop tic-tac-toe game in Python.

The tic-tac-toe game we are going to develop is a two player game played in the command line. Steps in the program are as follows-

  1. Give an option to the player 1 to choose symbol (‘X’ or ‘O’).
  2. Create a tic-tac-toe board with index numbers making it easy for the players to input the index where they want their symbol to be placed.
  3. Give each player a chance to place the symbol alternatively until one of the players has won the game or the game is drawn.

tic-tac-toe Python

tic-tac-toe board

Tic-tac-toe game – Python code

def user_choice():
    choice = ' '
    flag = False
    while not choice.isdigit() or not flag:
        choice = input('Please input a number (0-10): ')
        if choice.isdigit() and int(choice) in range(0, 10):
            flag = True
            return int(choice)
            print('Enter number between 0 and 10')
            flag = False

def display_board(board):
    print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
    print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
    print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])

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')
        return ('O', 'X')

def place_symbol(board, symbol, position):
    if board[position] in ('X', 'O'):
        print('Position already marked')
    board[position] = symbol

def check_marked_position(board, position):
    if board[position] in ('X', 'O'):
        print('Position already marked')
        return False
    return True

def select_player_position(board, player):
    position = 0
    while position not in range(1, 10) or not check_marked_position(board, position):
        position = int(input(player + ' Choose your next position: (1-9) '))
    return position

def is_winner(board, symbol):
    return ((board[1] == symbol and board[2] == symbol and board[3] == symbol) or  # top row
            (board[4] == symbol and board[5] == symbol and board[6] == symbol) or  # middle row
            (board[7] == symbol and board[8] == symbol and board[9] == symbol) or  # bottom row
            (board[1] == symbol and board[4] == symbol and board[7] == symbol) or  # first column
            (board[2] == symbol and board[5] == symbol and board[8] == symbol) or  # second column
            (board[3] == symbol and board[6] == symbol and board[9] == symbol) or  # third column
            (board[1] == symbol and board[5] == symbol and board[9] == symbol) or  # diagonal
            (board[3] == symbol and board[5] == symbol and board[7] == symbol))  # diagonal

def is_board_full(board):
    full_flag = True
    for i in range(1, 10):
        if board[i] not in ('X', 'O'):
            full_flag = False
    return full_flag

def start_play():
    while True:
        player1_symbol, player2_symbol = player_input()
        tictac_board = ['#', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        turn = 'Player1'
        play_again_flag = 'Y'
        symbol = ''
        while True:
            if turn == 'Player1':
                symbol = player1_symbol
                symbol = player2_symbol

            position = select_player_position(tictac_board, turn)
            place_symbol(tictac_board, symbol, position)
            if is_winner(tictac_board, symbol):
                print('Yeey! ' + turn + ' won!')
            if is_board_full(tictac_board):
                print('It''s a draw!')
                turn = 'Player2' if turn is 'Player1' else 'Player1'

        play_again_flag = input('Do you want to play again? Enter Y or N: ')
        if play_again_flag.lower() != 'y':

  1. start_play() function is the main function where first task is to ask player 1 to choose preferred symbol out of ‘X’ or ‘O’. This is done by calling player_input() function.
  2. To display tic-tac-toe board display_board() function is called passing list tictac_board = [‘#’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’] as an argument. Since index 0 is not required so that has a placeholder value ‘#’.
  3. In a while loop each player gets a chance to place their respective symbol. Functions used for this task are select_player_position() and place_symbol().
  4. While placing a symbol it is required to check if the selected position is already marked or not that is done using check_marked_position() function.
  5. After each turn it is also required to check if there is a winner that is done using is_winner() function. Another thing to check is whether the board is full that is done using is_board_full() function.

Related Posts

That’s all for the topic Tic-Tac-Toe Game in Python. If something is missing or you have something to share about the topic please write a comment.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.