Sai A
Updated date Jan 14, 2024
In this blog, we will learn Python by building a fun Tic-Tac-Toe game. Our easy-to-follow blog provides a simple program, visual results, and explanations for each step.

## Introduction:

Tic-Tac-Toe, a classic game loved by all, is not only a fun pastime but also a great starting point for those learning to program. In this blog, we will walk through the process of creating a simple Tic-Tac-Toe game in Python, step by step.

Let's dive into the Python code for our Tic-Tac-Toe game:

``````def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 9)

def check_winner(board):
# Check rows and columns
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] != ' ' or \
board[0][i] == board[1][i] == board[2][i] != ' ':
return True

# Check diagonals
if board[0][0] == board[1][1] == board[2][2] != ' ' or \
board[0][2] == board[1][1] == board[2][0] != ' ':
return True

return False

def is_board_full(board):
for row in board:
if ' ' in row:
return False
return True

def main():
board = [[' ' for _ in range(3)] for _ in range(3)]
player = 'X'

while True:
print_board(board)

row = int(input(f"Player {player}, enter row (0, 1, or 2): "))
col = int(input(f"Player {player}, enter column (0, 1, or 2): "))

if board[row][col] == ' ':
board[row][col] = player

if check_winner(board):
print_board(board)
print(f"Player {player} wins!")
break

if is_board_full(board):
print_board(board)
print("It's a tie!")
break

player = 'O' if player == 'X' else 'X'
else:

if __name__ == "__main__":
main()
``````

### Output:

``````  |   |
---------
|   |
---------
|   |
Player X, enter row (0, 1, or 2): 1
Player X, enter column (0, 1, or 2): 1
|   |
---------
| X |
---------
|   |
Player O, enter row (0, 1, or 2): 0
Player O, enter column (0, 1, or 2): 0
O |   |
---------
| X |
---------
|   |
...
``````

## Setting up the Game Board and Players

In the first part of our program, we initialize a 3x3 game board filled with empty spaces (' '). We also set the starting player to 'X'. The `print_board` function is responsible for displaying the current state of the board, making it easier for players to visualize their moves.

## Checking for a Winner

The `check_winner` function is crucial for determining if a player has won the game. It checks the rows, columns, and diagonals for three consecutive matching symbols ('X' or 'O'). If a winner is found, the game ends, and the winner is declared.

## Checking for a Tie

The `is_board_full` function checks if the board is completely filled, indicating a tie. If no player has won and the board is full, the game ends in a tie.

## Player Input and Move Execution

The `main` function handles the core logic of the game. It continuously prompts players for their moves, updating the board and checking for a winner or tie after each move. The players take turns entering their desired row and column, and the program ensures that the selected cell is empty before placing the player's symbol.

## Displaying Results

After each move, the current state of the board is displayed. If a player wins or the game ends in a tie, the final board is printed along with the corresponding result.

## Conclusion:

You have just created a simple Tic-Tac-Toe game in Python. This project is an excellent starting point for beginners, allowing them to practice fundamental programming concepts such as loops, conditionals, and functions. By following this step-by-step guide, you have gained insights into creating a functional game and understanding how different components work together.