---
# System prepended metadata

title: Untitled

---

How to Make Tic Tac Toe in Python?

To create a tic-tac-toe game in Python, you will need to use a few key concepts:

* Representing the game state: You will need a way to represent the current state of the game, including which squares have been played, and which player's turn it is. One way to do this is to use a list of lists, where each inner list represents a row on the board, and the elements of the inner list are either 'X', 'O', or '' (an empty string) to represent which player has played in that square.
* Displaying the game board: You will need a way to display the current state of the game board to the user. This can be done using ASCII art, or by using a library like [Pygame](https://devhubby.com/thread/how-to-install-pygame-library-in-python) to create a graphical interface.
* Handling user input: You will need a way to allow the user to input their moves. This can be done using the built-in input() function, or by using a library like [Pygame](https://devhubby.com/thread/how-to-install-pygame-library-in-python) to create buttons that the user can click on.
* Checking for a win: You will need a way to check whether the current player has won the game. This can be done by checking the rows, columns, and diagonals of the game board to see if they are all filled by the same player's symbol.

Here is some sample code that puts these concepts together to create a simple text-based tic-tac-toe game:


```python
# Set up the game board as a list of lists
board = [
    ['', '', ''],
    ['', '', ''],
    ['', '', '']
]

# Set up variables to track the current player and game status
current_player = 'X'
game_running = True

# Set up a function to display the current game board
def display_board():
    for row in board:
        print(' '.join(row))

# Set up a function to handle user input
def handle_input():
    global current_player
    
    # Get the row and column from the user
    row = int(input('Enter a row: '))
    col = int(input('Enter a column: '))
    
    # Make sure the square is empty
    if board[row][col] != '':
        print('That square is already taken!')
        return
    
    # Place the player's symbol on the board
    board[row][col] = current_player
    
    # Switch players
    if current_player == 'X':
        current_player = 'O'
    else:
        current_player = 'X'

# Set up a function to check for a win
def check_win():
    # Check rows
    for row in board:
        if row[0] == row[1] == row[2] and row[0] != '':
            return row[0]
    
    # Check columns
    for col in range(3):
        if board[0][col] == board[1][col] == board[2][col] and board[0][col] != '':
            return board[0][col]
```