Gaming is one of the entertainment that humans have. We can find different types of games on the web, mobile, desktop, etc. We are not here to make one of those heavy games now. We are going to create a CLI tic-tac-toe game using Python. If you are not familiar with Tic Tac Toe, play it visually here to understand. Don’t worry, even if you don’t understand it, we are going to see it.

Tic Tac Toe

The tutorial is divided into three different sections. In the first section, you will get to know how to play the tic-tac-toe game. After that, we will see an algorithm that helps us to come up with the game logic. Finally, we will see the structured code and its explanation. You may skip the first section if you already know how to play Tic Tac Toe. So, without further ado, let’s dive into our first section.

Playing Tic Tac Toe

There will be two players in a game. Two signs represent each player. The general signs used in the game are X and O. Finally, there will be a board with 9 boxes. See the tic-tac-toe board visually. The gameplay will be as follows.

First, one user will place their sign in one of the available empty boxes. Next, the second user will place their sign in one of the available empty boxes. The goal of the players is to place their respective signs completely row-wise or column-wise, or diagonally. The game goes on until a player wins the game or it ended up in a draw by filling all boxes without a winning match.

Let’s see some gameplays visually. The player X wins the game in the above gameplay. All boxes diagonally fill with X signs. So, the respective player wins the game. There are a total of 8 ways to arrange the same sign and win the game. Let’s see all the 8 arrangements that can win the game. And finally, a draw fills the board without any winning arrangement. I hope you understand how to Tic Tac Toe now. Now, it’s playtime for you. You may go here and play it to understand the gameplay completely. Leave it if you have already got it. Now, it’s time to move the algorithm section.

 Algorithm

We will now discuss the algorithm to write the code. This algorithm will help you to write code in any programming language of your choice. Let’s see how it’s done.

Create a board using a 2-dimensional array and initialize each element as empty. You can represent empty using any symbol you like. Here, we are going to use a hyphen. ‘-’. Write a function to check whether the board is filled or not. Iterate over the board and return false if the board contains an empty sign or else return true. Write a function to check whether a player has won or not. We have to check all the possibilities that we discussed in the previous section. Check for all the rows, columns, and two diagonals. Write a function to show the board as we will show the board multiple times to the users while they are playing. Write a function to start the game. Select the first turn of the player randomly. Write an infinite loop that breaks when the game is over (either win or draw). Show the board to the user to select the spot for the next move. Ask the user to enter the row and column number. Update the spot with the respective player sign. Check whether the current player won the game or not. If the current player won the game, then print a winning message and break the infinite loop. Next, check whether the board is filled or not. If the board is filled, then print the draw message and break the infinite loop. Finally, show the user the final view of the board.

You may be able to visualize what’s happening. Don’t worry, even if you didn’t understand it completely. You will get more clarity once you see the code. So, let’s jump into the code section. I assume you have Python installed on your PC to try the code.

Code

Go through the below code. Check out the sample output of the code. Some major points that help you understand the structure of the code.

We have used a class to have all the methods in one place. It can easily be a reusable bundle in some other code as well. Next, we have defined different functions for each responsibility, even if it is a small task. It helps to maintain the code with ease. The above two approaches help us update the app effortlessly if we want to update the game.

Feel free to adapt the structure and improve it based on your project. Structuring the code is not limited.

Final Words

Hurray! 😎 You have created a game completely from scratch. It is not one of the visual games that we play daily. But it helps you to write logic and maintain clean structure in code. Follow similar guidelines to create some interesting games like this. You can find similar games if you go back some years to your childhood. Happy Coding! 👩‍💻 Next, explore how to create number guessing game and Unit Testing with Python unittest Module.

How to Create a Tic Tac Toe Game in Python  - 71How to Create a Tic Tac Toe Game in Python  - 23How to Create a Tic Tac Toe Game in Python  - 94