Tic Tac Toe Game in Python
Looking to learn how to create the classic Tic Tac Toe game in Python? This tutorial covers the basics of Python programming and walks you through the steps of building a simple Tic Tac Toe game. Start coding today and enhance your Python skills while having fun with this timeless game!
In this article, we will build the classic tic-tac-toe game using Python. Tic Tac Toe is a simple two-player game where players mark spaces on a 3×3 grid with X and O. The goal is to get three of your marks in a row, either horizontally, vertically, or diagonally.
So, without further ado, let’s start!
Tic Tac Toe Python Program
Here’s a basic implementation of a two-player Tic Tac Toe in Python. To understand the program below, you should have a basic idea about the following Python concepts:
- Python Functions
- Python Loops
Step 1
The following line initializes a list called board with 9 spaces, representing the 9 spaces on the Tic Tac Toe board.
board = [" " for x in range(9)]
Step 2
The following function print_board() will print the Tic Tac Toe board. It creates 3 strings, row1, row2, and row3, to represent each row of the board. The spaces in the board are filled with the values from the board list. The function then prints each row with separators to create the appearance of a 3×3 grid.
def print_board(): row1 = "| {} | {} | {} |".format(board[0], board[1], board[2]) row2 = "| {} | {} | {} |".format(board[3], board[4], board[5]) row3 = "| {} | {} | {} |".format(board[6], board[7], board[8])
print() print(row1) print(row2) print(row3) print()
Step 3
The following function then prints each row with separators to create the appearance of a 3×3 grid. The player_move() function allows a player to make a move by marking a space on the board with their icon (X or O). It takes the player’s icon as an argument and asks the player to enter their move (1-9) in the terminal. If the space the player chooses is blank, the function will place the player’s icon in that space. Otherwise, it will inform the player that the space is full.
def player_move(icon): if icon == "X": number = 1 elif icon == "O": number = 2 print("Your turn player {}".format(number)) choice = int(input("Enter your move (1-9): ").strip()) if board[choice - 1] == " ": board[choice - 1] = icon else: print() print("That space is already taken!")
Step 4
This function is_victory() checks if a player has won the game. It takes the player’s icon as an argument and checks if there are three of that icon in a row on the board. It returns True if a player has won, otherwise it returns False.
def is_victory(icon): if (board[0] == icon and board[1] == icon and board[2] == icon) or \ (board[3] == icon and board[4] == icon and board[5] == icon) or \ (board[6] == icon and board[7] == icon and board[8] == icon) or \ (board[0] == icon and board[3] == icon and board[6] == icon) or \ (board[1] == icon and board[4] == icon and board[7] == icon) or \ (board[2] == icon and board[5] == icon and board[8] == icon) or \ (board[0] == icon and board[4] == icon and board[8] == icon) or \ (board[2] == icon and board[4] == icon and board[6] == icon): return True else: return False
Step 5
This function is_draw() checks if the game is a draw by checking if all spaces on the board are filled. When all spaces are filled and no player has won, it returns True, otherwise it returns False.
def is_draw(): if " " not in board: return True else: return False
Step 6
The while loop continues the game until a player has won or the game is a draw. Within the loop, the board is printed. Then, the player_move() function calls and allows the first player to make a move. The board prints again to show the updated state. Then, calling the is_victory() function will check if the first player has won. When they win, the game ends and a victory message shows If not, the is_draw() function is called to check if the game is a draw.
If the game is a draw, a message is displayed and the game ends. If neither the first player nor the game has won or ended in a draw, the second player makes a move and the process repeats.
while True: print_board() player_move("X") print_board() if is_victory("X"): print("X wins! Congratulations!") break elif is_draw(): print("It's a draw!") break player_move("O") if is_victory("O"): print_board() print("O wins! Congratulations!") break elif is_draw(): print("It's a draw!") break
Output
In summary, this basic Tic Tac Toe game implementation in Python can help beginners who are just starting with the language to apply their knowledge and improve their logic skills while enjoying the game.
Endnotes
Hope this article proved helpful and fun in teaching you how to build a tic-tac-toe game in Python.
Contributed by – Prerna Singh
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio