Tic-Tac-Toe Game in Python


This Python program is an implementation of the classic Tic-Tac-Toe game for two players. The game is played on a 3×3 grid, where players take turns marking a cell with their respective symbols (‘O’ or ‘X’). The winner is the first player to align three of their symbols horizontally, vertically, or diagonally.


  • Basic understanding of Python programming.
  • Familiarity with functions, global variables, and control structures in Python.
  • Python environment (like IDLE, Jupyter Notebook, or any IDE supporting Python).

Learning Objectives

  • Implementing a two-player game logic using Python.
  • Understanding the use of functions, global variables, and conditional statements.
  • Handling user inputs and validating them.

Installation Guide

No additional installation is required if Python is already installed. This game can be run in any Python environment.

Project Steps

  1. Initialization: Set up the game board and global variables.
  2. User Input: Prompt players to input their choices in each turn.
  3. Game Logic: Implement functions to update the board, check for a win or tie, and determine the game’s outcome.
  4. Gameplay Loop: Run the game in a loop until a player wins or the game ends in a tie.

Code Explanation

  1. Global Variables: l for the game board, p1c and p2c for player scores.
  2. Functions:
    • replaceUser(u, sign): Updates the game board based on the player’s choice.
    • horizontalcheck(): Checks for a horizontal win condition.
    • verticlecheck(): Checks for a vertical win condition.
    • diagonalcheck(): Checks for a diagonal win condition.
    • printMatrix(): Prints the current state of the game board.
    • resetdb(): Resets the game board to its initial state.

Flow Chart

TicTac Toe - flowchart

Pseudo Code

					Initialize the game board
Set player symbols as 'O' and 'X'

While the game is not over:
    Get Player 1's choice
    Update the board and check for a win or tie
    Get Player 2's choice
    Update the board and check for a win or tie

If a player wins or the game ties:
    Declare the result
    Reset the board for a new game


Challenges and Solutions

  • Challenge: Ensuring that players do not overwrite each other’s moves.
  • Solution: Validate each move to ensure the chosen cell is not already occupied.


  • Test the game with various inputs to ensure all possible game scenarios are handled correctly.
  • Verify that the game correctly identifies win conditions and ties.

Additional Resources


  1. Can the game be played with more than two players?
    The program will display an error message and will not include it in the bill or points calculation.

  2. How is the game board reset for a new game?
    Points are calculated based on the total bill. One point is earned for every 100 units of currency in the bill.

Project by Nimisha Agrawal, Team edSlash.