Build a Tic Tac Toe Game in Python with W3Schools Guide

Logan Jul 13, 2026

Ever wondered how you can create classic games like Tic Tac Toe using Python? Well, today, we're going to explore exactly that, with a dash of help from our friends at w3schools.com. Before we dive in, let's assume you have a basic understanding of Python programming. If not, don't worry, we'll keep it simple and understandable.

Python Colorful Tic Tac Toe Game using PyQt GUI - EasyCodeBook.com
Python Colorful Tic Tac Toe Game using PyQt GUI - EasyCodeBook.com

Python, a high-level programming language, is perfect for creating such games due to its clarity and readability. Let's start by understanding how we'll structure our Tic Tac Toe game. The game will be played in a 3x3 grid, with two players taking turns to place their marks (X or O) in empty cells. The first player to get three of their marks in a row horizontally, vertically, or diagonally wins the game. Sound simple? Let's code it.

an image of a tic - board with the word x on it and some other symbols
an image of a tic - board with the word x on it and some other symbols

The Game Board

The game board is a 3x3 2D list in Python, where each row and column represent a cell in the board. We'll initialize it with empty spaces represented by dashes.

Tic Tac Toe in Python With Source Code
Tic Tac Toe in Python With Source Code

Here's how we can create the board:

Initializing the Board

Tic Tac Toe JavaScript | Step by Step Tutorial - Letstacle
Tic Tac Toe JavaScript | Step by Step Tutorial - Letstacle

We'll create a 3x3 list called 'board'. The '*' operator will be used to initialize the list with empty spaces.

```python board = [['-'] * 3 for _ in range(3)] ```

Printing the Game Board

Build a Tic Tac Toe Game in Python
Build a Tic Tac Toe Game in Python

To print the game board, we'll use a nested for loop. We'll also add some formatting for legibility.

```python def print_board(board): for row in board: print(' | '.join(row)) print('---------') print_board(board) ```

Game Logic

Tic-Tac-Toe Game Using Python | TechieYan Technologies
Tic-Tac-Toe Game Using Python | TechieYan Technologies

Now that we have our game board set up, let's move on to the game's logic. This involves player turns, move validation, and game winning conditions.

We'll need functions for each of these operations. Let's start with player turns.

Learn to create a Tic Tac Toe game in Python
Learn to create a Tic Tac Toe game in Python
Python Tic Tac Toe Game Build - PyQt5 GUI
Python Tic Tac Toe Game Build - PyQt5 GUI
I Built Tic Tac Toe With JavaScript - Mitchum.Blog
I Built Tic Tac Toe With JavaScript - Mitchum.Blog
Tic Tac Toe Game in C++ with Source Code
Tic Tac Toe Game in C++ with Source Code
How to win at Tic-Tac-Toe
How to win at Tic-Tac-Toe
Tic Tac Toe game Tkinter Python using Visual Studio Code with source code
Tic Tac Toe game Tkinter Python using Visual Studio Code with source code
Strategic Tic-Tac-Toe Board Slam - Family Style Schooling
Strategic Tic-Tac-Toe Board Slam - Family Style Schooling
How to Play Ultimate Tic-Tac-Toe - with Printable
How to Play Ultimate Tic-Tac-Toe - with Printable
Ultimate Tic Tac Toe [FREE PRINTABLE PDF TEMPLATE]
Ultimate Tic Tac Toe [FREE PRINTABLE PDF TEMPLATE]
Python Tic Tac Toe Game Tutorial
Python Tic Tac Toe Game Tutorial
Math = Love - Hands-On Lessons and Printable Logic Puzzles
Math = Love - Hands-On Lessons and Printable Logic Puzzles
Unbeatable Tic Tac Toe AI Scratch_ I Bet You Can't Beat It
Unbeatable Tic Tac Toe AI Scratch_ I Bet You Can't Beat It
Android源码下载 Tic Tac Toe:简单且简约的游戏
Android源码下载 Tic Tac Toe:简单且简约的游戏
Games Similar To Tic Tac Toe, Games Like Tic Tac Toe, Interesting Games, Tic Tac Toe Hacks, Easy Games, How To Play Tac Toe, How To Play Tic Tac Toe, How To Play Tic Tac Toe Variations, How To Play Noughts And Crosses
Games Similar To Tic Tac Toe, Games Like Tic Tac Toe, Interesting Games, Tic Tac Toe Hacks, Easy Games, How To Play Tac Toe, How To Play Tic Tac Toe, How To Play Tic Tac Toe Variations, How To Play Noughts And Crosses
a piece of paper that has some circles on it and three yellow dots next to it
a piece of paper that has some circles on it and three yellow dots next to it
15 JavaScript Tic Tac Toe Games
15 JavaScript Tic Tac Toe Games
How to Build Tic Tac Toe with JavaScript, HTML and CSS - Tutorial
How to Build Tic Tac Toe with JavaScript, HTML and CSS - Tutorial
Super Tic Tac Toe
Super Tic Tac Toe
Spider Web Tic-Tac-Toe Game for Halloween | Math = Love
Spider Web Tic-Tac-Toe Game for Halloween | Math = Love
How to Create Stunning Memory Games for Kids Printable with Fun Printable Games Designs
How to Create Stunning Memory Games for Kids Printable with Fun Printable Games Designs

Player Turns

We'll create a function 'player_turn' that takes the current state of the board, the player's mark, and the list of winning combinations as arguments. It will print whose turn it is, get the user's move, validate the move, and update the board if the move is valid.

```python def player_turn(board, mark, combinations): print(f"Player {mark}'s turn") while True: move = input("Enter your move (row,col): ") row, col = map(int, move.split(',')) if 0 <= row < 3 and 0 <= col < 3 and board[row][col] == '-': board[row][col] = mark break else: print("Invalid move. Try again.") ```

Game Winning Conditions

Next, we'll create a function 'check_win' that takes the board and the winning combinations as arguments. It checks if any player has won by checking if any row, column, or diagonal contains the same mark.

```python def check_win(board, combinations): for combination in combinations: if all(board[i][j] == player for i, j in combination): print(f"Player {board[i][j]} wins!") return True return False ```

Draw Condition

Lastly, we'll add a draw condition to our game. After each player's turn, we'll check if the board is full. If it is, we'll declare it a draw.

```python def check_draw(board): return all(board[i][j] != '-' for i in range(3) for j in range(3)) ```

And there you have it! With these functions, we can create a fully functional Tic Tac Toe game in Python. With practice and some creativity, you can even add more features like AI playing level or different game themes.

Don't forget to check out other games and tutorials on w3schools.com to expand your coding skills. Happy coding!