Skip to main content

Easy Tic-Tc-Toe Game with Command-line UI in Python

Today we will create a very simple Tic-Tac-Toe game, which can be played between two players. The program has a basic input-output command-line process, and displays the game layout after each turn. You need a very simple understanding of Python to get this game off the ground.
This game will use a row-column system similar to Chess, which will require the player to input a specific command so that the program marks that particular tile. Don't forget to collect this python file at the end of this post to try it out instantly. So without any further ado, let's start!


The Code

Starting off, we initialize 3 lists, row1, row2, row3, that represent the 3 rows of the Tic-Tac-Toe game, and each value represents a tile of the game. Initially, the value is kept blank. As the players mark each tile, the values in the lists are updated with values either 'X' or 'O'. Instructions are provided at the start of the game, which clearly states how a player is supposed to input his turn. The input format is: 'r' + <row number> + <column number>. So the input for each tile will like this:

Moving on to the main body of our program, we initialize a variable n = 1 and update it in the upcoming while loop to determine if the next turn is of player 'X' or player 'Y', and update that value in the corresponding list at the correct index. 


In the while loop, we first use the if-else statements to determine which player's turn it is, as mentioned above. Then we call a user-defined function game_display(row1, row2, row3) which takes the lists as parameters and shows us the current game layout (We will come to this function again). Then we take the player input, and get the row number and column number. Using a couple of nested if-elif-else statements we determine the exact tile the player has chosen, and we update the value of the corresponding list, with the values either 'X' or 'O' (depending on whose turn it is) in the correct index.

Going forward, we are still inside the while loop, and we set the values of 9 variables (r11, r12,....r32, r33) as the corresponding values in the lists (easier to call each list element this way). This time, we have 9 if-elif statements to check if the game is over (either player wins or the game is a tie). Inside each of these statements, we declare the winner, the winning game layout, and ask the player if he wants to play again, with yet another user-defined function game_continuation(row1, row2, row3, n). If the player wants to continue, the lists are simply reset, and the while loop continues (but essentially a new game), else the program terminates.


We now come to our user-defined functions that we have used in the above lines. First up, we have game_display(row1, row2, row3), which contains only a series of print statements to display the current game layout, using the updated lists that it takes as parameters:


Finally, the second user-defined function is game_continuation(row1, row2, row3, n). It asks the user if they want to continue, and if the answer is yes, it resets the lists, the variable n (again, value of n determines whose turn it is, and for a new game it should start with 1), and returns these values back to the main body along with the user's choice (whether to continue or not):


And with that, we have completed this program and it is ready to be executed. Below you will find how this program works.

The Working

On startup, you see the instructions and the starting game layout. The game layout also shows row and column numbers, so that you can find out your tile with ease. Below that, the program is asking for your chosen position (tile) [Note that it also shows whose turn it is]:

After each player's turn, it shows the updated game layout like this:


On victory, the program shows the following output:

And when the player wishes to continue the game, the program reverts the game to the starting layout:


I'll let you find out other things for yourself.

And we have reached the end of this post. I hope you liked this small exercise, stay tuned with this blog for more in the future. Here's your ready-to-execute python file: Tic-Tac-Toe Python File.

Comments

Popular posts from this blog

Simple Omegle Bot Using Selenium With Python

Omegle is an online text-based and video-based chatting platform, which allows users from around the world to talk to complete strangers anonymously, for free! The text-based Omegle has a simple concept: Complete a Captcha  verification, connect automatically to a stranger, and after chatting get on to the next stranger. Apart from having a chit-chat with a stranger from the far side of the globe, Omegle poses as the perfect platform for other uses as well. At Omegle, you  can advertise your content, website, products and more for free. With access to about 40,000 strangers using Omegle at any given time, you can benefit if your ideas/advertisement is seen by potential customers. But of course, we can't advertise to each guy we meet again and again. I mean, come on, that's a lot of hard work, even if your message consists of a few words. But worry not, since that's where our Omegle Bot  comes in play. This bot works on a pre-determined set of messages that are to be conveye

Predicting Stock Prices using Machine Learning (XGBoost)

  Today, I'll show you how to create a machine learning program to predict stock prices. Machine learning is used in a variety of fields, and we can utilize its ability to learn and predict from data to predict useful variables, with minimal error. Therefore in theory, we can apply the same on stocks to predict the next closing price, so that we can make a killing gain. However, stock markets are highly unstable. Their price movement often depends on decisions taken by the company, favor and reaction of investors, social impact, human emotions, and price movements of some other related stocks. These types of data cannot be made available to a program. The prediction can be close only if the market remains relatively stable. You don't actually need to learn machine learning using python to understand how this program works, if not minimal. I'll describe each machine learning process. And don't forget to download the source code of this program, link provided at the end.

English Dictionary With Python And Tkinter!

English Dictionary on Python Have you ever tried to read from the small Oxford Dictionaries? Those small yet bulky books have thousands of words cramped on a page the length of your finger! And on top of that, not to forget the hassle of flipping across the pages, searching for your word, God knows where is hiding on which page! Nowadays though, hardly does anyone ever use the classic dictionaries. With the access of technology on our fingertips, one tap on Google, and you would be on with your way.  But how about making a program of your own which can do the same for you? Sounds interesting? Such a program can enable you to run it and keep it opened, while you are reading a book, so that you can search the definition of a new word you encountered. What if you are writing a book, perhaps a report? You know what you are supposed to write, but you choose to get a word for it, so that you sound professional. So you just search for a short definition and the program find the word with matc