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
Post a Comment