Tic Tac Toe in Python

How to make Tic Tac Toe in Python? How do you play a game of Tic Tac Toe in Python?

In this blog, we will delve into the world of Python programming by creating the Tic Tac Toe Python project from scratch. This engaging project will cover fundamental concepts such as algorithms, data structures, data analysis, and GUI development using PyGame code snippets.

An image that explains how to use Python for Tic Tac Toe game.

Dec 22, 2023    By Team YoungWonks *

Setting Up the Game Board

Before we dive into coding, let's create the game board. Using a 2D list in Python, we represent the Tic-Tac-Toe grid. Each element in the list will represent a cell on the board, initialized with empty spaces.

tic tac toe board

The Core Functions

Now, let's define the core functions. The create_board function will iterate through the board, creating a visual representation for the players. The check_winner function will determine if a player wins.

tic tac toe show board

This function iterates through each row of the tic_tac_toe_board and prints the elements joined by a pipe (|) symbol. Additionally, it prints a line of hyphens (-) to separate the rows visually and print the board.

tic tac toe check winner

This function checks for a winner by examining rows, columns, and diagonals. If it finds a winning condition, it returns the symbol of the winning player (player X or player O). If there is no winner and the board is full (a tie), it returns 'Tie'. If the game is still ongoing, it returns None. You can also work on adding an option to ask the player if they would like to play again.

You can customize and expand this function based on your requirements or game variations.

Player Moves: Taking User Input

To handle player moves, we'll use the get_player_input function to accept user input and update the board accordingly. This function will append checks for invalid moves, ensuring a smooth gameplay experience.

tic tac toe player input

Minimax Algorithm

To make our game unbeatable, we'll implement the Minimax algorithm for the AI opponent. The minimax function will calculate the best move, ensuring a challenging gameplay experience.

tic tac toe ai

The Minimax algorithm is a recursive algorithm that explores all possible moves in a game tree and assigns scores to each move. The goal is to find the best move for the maximizing player (in this case, the AI player) and the worst move for the minimizing player (the human player). If you would like, you can let the game be a two-player game and not add the minimax algorithm.

Add the following get_ai_move function for getting the AI move after the get_player_input function:

tic tac toe ai move

Tic Tac Toe with PyGame

To add an extra layer of excitement, let's introduce PyGame to our project. We'll utilize it to create a visually appealing game window, handle user input more intuitively using mouse clicks, and also add a main function. Note that current_player needs to be made a global variable so that it can be accessed by the main function later on.

tic tac toe pygame tic tac toe pygame board

True or False: The Game Loop

Our game will run inside a while loop, ensuring continuous gameplay until there's a winner or a tie. The current_player variable will keep track of the player taking their turn i.e. the current player. The current player will start from X in the beginning as the first player starts from X.

tic tac toe pygame code

Game Over: Wrapping It Up

As we reach the end of our Python Tic Tac Toe journey, you now have a fully functional game, complete with a graphical interface and a challenging AI opponent. This project covers essential programming language concepts and serves as an excellent hands-on experience for both beginners and experienced programmers. Make sure to commit the source code of your projects to your GitHub profile.

Whether you're a beginner looking to learn Python coding or a software engineering enthusiast seeking a fun project, this Tic Tac Toe Python tutorial is a true gem.

Tic Tac Toe with Tkinter

We can use buttons to represent the cells on the Tic Tac Toe board, allowing players' moves to be allowed. Then replace the command line-based display with code that updates the Tkinter GUI elements (buttons or labels) to reflect the current state of the board.

Coding Classes for Kids

In the dynamic world of game development, Python has established itself as a versatile programming language. It grants developers the flexibility to create intricate game algorithms, while simultaneously simplifying the learning curve for beginners. Python's powerful machine learning capabilities offer developers the chance to incorporate predictive behaviors within their games, making them more interactive and challenging. This is what makes learning Python very important. Moreover, by harnessing the power of APIs, developers can integrate their Python-based games with Java or JavaScript coded systems, enabling cross-platform functionality.

At YoungWonks, we believe in the power of making coding an exciting adventure for kids! Our Coding Classes for Kids are carefully designed to provide hands-on experiences that ignite their passion for coding. Our Python Coding Classes for Kids offer beginners an incredible opportunity to bring their ideas to life through coding. We also offer Raspberry Pi, Arduino, and Game Development Coding Classes, where kids can dive into the endless possibilities that coding has to offer. Join us and let's embark on an exhilarating coding journey together!

*Contributors: Written by Reuben Johns; Edited by Rohit Budania; Lead image by Shivendra Singh

This blog is presented to you by YoungWonks. The leading coding program for kids and teens.

YoungWonks offers instructor led one-on-one online classes and in-person classes with 4:1 student teacher ratio.

Sign up for a free trial class by filling out the form below:

By clicking the "Submit" button above, you agree to the privacy policy
Share on Facebook Share on Facebook Share on Twitter Share on Twitter
Schedule a free trial class help