How To Make Tic Tac Toe In Python

Today, I want to share with you how to create a classic game of tic-tac-toe using Python. I’ve always been fascinated by the simplicity and yet the strategic depth of this game. So, I thought it would be a fun project to dive into and create my own version of tic-tac-toe!

Setting Up Our Game Board

First things first, let’s start by setting up the game board. We’ll need a 3×3 grid to represent the tic-tac-toe board. To achieve this, we can create a list of lists in Python:

board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

Here, I’ve initialized each cell of the game board with a value of 0. This represents an empty cell. We can use other values to indicate whether a cell is occupied by player 1 or player 2.

Defining the Game Logic

Next, let’s define the logic for the game. We’ll need to determine when a player wins or when the game ends in a draw. To do this, we can create a function called check_winner() that checks for winning conditions:


def check_winner(board):
# Check rows
for row in board:
if len(set(row)) == 1 and row[0] != 0:
return row[0]

# Check columns
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] != 0:
return board[0][col]

# Check diagonals
if board[0][0] == board[1][1] == board[2][2] != 0:
return board[0][0]

if board[0][2] == board[1][1] == board[2][0] != 0:
return board[0][2]

# No winner
return 0

This function checks each row, column, and diagonal of the board to see if there is a winner. If it finds a row, column, or diagonal with the same values (either 1 or 2), it returns the value of the winning player. Otherwise, it returns 0, indicating no winner.

Putting It All Together

Now that we have the game board set up and the logic defined, let’s write the code that allows players to take turns and play the game. We can create a play_game() function that handles the game loop:


def play_game():
# Initialize player
current_player = 1

while True:
# Display the game board
display_board()

# Get input from the current player
print(f"It's player {current_player}'s turn.")
row = int(input("Enter the row (0-2): "))
col = int(input("Enter the column (0-2): "))

# Place the player's move on the board
board[row][col] = current_player

# Check for a winner
winner = check_winner(board)

if winner != 0:
display_board()
print(f"Player {winner} wins!")
break

# Switch to the other player
current_player = 3 - current_player

# Check for a draw
if all(cell != 0 for row in board for cell in row):
display_board()
print("It's a draw!")
break

This function starts by initializing the current player to 1. Then, it enters a loop where it asks the current player for their move, places it on the board, and checks for a winner. If a winner is found, it displays the final board and declares the winner. If no winner is found, it switches to the other player. If the board is full and no winner is found, it displays the final board and declares a draw.

Adding Personal Touches

Now that we have the basic game logic in place, you can add your own personal touches to make the game more enjoyable. You can add a scoreboard to keep track of wins for each player, implement a graphical user interface using a library like Tkinter, or even add a bit of AI to play against the computer.

Conclusion

Creating a tic-tac-toe game in Python is a great opportunity to enhance your programming skills and have some fun at the same time. By setting up the game board, defining the logic, and creating the game loop, you can create a fully functional game. So go ahead, give it a try, and let the X’s and O’s fill the game board!