Tic-Tac-Toe in Python

Tic-Tac-Toe in Python

In the following tutorial, we will create a game known as Tic-Tac-Toe using the Python programming language from scratch. We have divided the whole program into various steps for better understanding. But before we get into the procedure, let us understand about the game.

What is Tic-Tac-Toe?

Tic-Tac-Toe is among the games played between two players played on a 3 x 3 square grid. Each player inhabits a cell in their respective turns, keeping the objective of placing three similar marks in a vertical, horizontal, or diagonal pattern. The first player utilizes the Cross (X) as the marker, whereas the other utilizes the Naught or Zero (O).

Now, let us understand the design of Tic-Tac-Toe.

Design of Tic-Tac-Toe

We will be using the command prompt in order to play Tic-Tac-Toe. Thus, it is the primary objective to construct a design for the Tic-Tac-Toe game.

Objective: If a player needs to mark a specific block, he/she must input the corresponding digit displayed in the grid. For instance, we wanted to occupy the upper right block, and then we have to enter digit 3 in the terminal.

Let us understand the snippet of code to generate the grid.

Program:

Explanation:

In the above snippet of code, we have defined a function for the Tic-Tac-Toe game that takes the values as the parameter. Here the val parameter is a list consists of the status of each cell in the grid. Within the function, we have then printed the design of the Tic-Tac-Toe grid.

Now, the next step is to store the data with the help of data structures.

Storing Data using the Data Structures

The principle of any game relies on the mechanics behind the game. Since we are creating a relatively simple and easy game, the mechanics included are simple as well.

At any point in Time, there are two crucial pieces of information required:

  1. Grid Status:We have to create a data structure that will contain the state of each cell. The state can either be occupied or vacant.
  2. Moves of each player:The knowledge of past and present moves of each player is somehow required, that is, the positions occupied by ‘X’ and ‘O’.

Note: The above data can be accessed with the help of Grid Status. However, it will require us to traverse this information every Time we want the positions of the player. This can be known as Time versus Space Complexity Trade-off. It is a general technique in order to conserve Time.

The syntax for the same is shown below:

Snippet of code:

Explanation:

In the above snippet of code, we have defined a function for a single game of Tic-Tac-Toe where the val represents the parameter for the previous function and playerpos stores the position of the block occupied by the cross (X) and naught (O), respectively.

There are generally three values in a list of characters that manages the Grid Status:

  1. ‘ ‘ – This character signifies the vacant or empty cell.
  2. ‘X’ – This character signifies that a Cell is inhabited by the X Player.
  3. ‘O’ – This character signifies that a Cell is inhabited by the O Player.

Moves of each player are kept as a dictionary of a list of integers where the keys are denoted by ‘X’ and ‘O’ for the corresponding player. Their respective lists consist of the digits provided to the cells in the grid they inhabit.

Note: The curplayer variable stores the current player making a move, as in ‘X’ or ‘O’.

Understanding the Game Loop

Every game consists of some type of game loop that allows the player to play the game until a player wins or the game is a tie. In the game of Tic-Tac-Toe, every loop iteration denotes a single move made by any player.

Let us consider the following snippet of code in order to design the Game Loop.

Syntax:

Explanation:

As we can observe, we have used the while loop to print the values for the function mytictactoe(), generating a game loop for a single game of Tic-Tac-Toe.

Handling the input from Player

For every iteration in the game, the player has to provide the input for his move. Let us consider the following syntax in order to handle the Player’s input.

Syntax:

Explanation:

For the above snippet of code, we have created a try block to handle the unintended value of the players. We have then handled the exception of ValueError so that the game must not be stopped. Later we have performed few sanity checks, such as whether the entered value is a valid position or not, and if it is a valid position, is it filled already?

Now, let us move onto the next step.

Updating the Game information

As per the input provided by the player, we have to update the information for the smooth working of the game. We can update the game information by adding the following snippet of code to the main project.

Syntax:

Explanation:

In the above snippet of code, we have updated the game information by updating the status of the grid and the position of the player. The val list will update the cell filled as per the current player. The position of the player will add the position just occupied by the current player.

Once the val list is updated, we will call the mytictactoe() function, and the grid would look like the following:

Output:

             |     |
          1  |  2  |  3
        _____|_____|_____
             |     |
          4  |  5  |  6
        _____|_____|_____
             |     |
          7  |  8  |  9
             |     |

Checking Win or Tie

After each move, we need to check if any player has won the match or the match has been tied. We can check it with the help of the syntax given below:

Syntax:

Explanation:

In the above syntax, we have used the if statement to check for Win or Tie. The singlegame() function will return the current player if he/she wins the game. Otherwise, the game is tied, ‘D’ is sent back.

Let us consider the function checking whether any player has won.

Syntax:

Explanation:

In the above snippet of code, we have defined the functions to check the Victory or Tie. These functions are check_Victory() and check_Tie(), respectively.

  1. check_Victory(): It stores all the combinations for winning the game. The function checks if the position of the current player satisfies any of the winning combinations. If it does, it will return TRUE; else, it will FALSE for not satisfying the requirement.
  2. check_Tie(): It is pretty simple, which checks if all ‘Nine’ positions are occupied, the game is tied.

Switching the current player

Every player got only one chance at a time. Thus, after every successful more, the current player will be swapped. Let us consider the following snippet of code for the same:

Syntax:

Explanation:

In the following snippet of code, we have used the if-else statement in order to switch moves of the player in such a way that if the current player marks one position, then the current player will be changed, and the other player will mark their move.

These are some steps we need to be concerned about while making a single game. However, we will be creating a Scoreboard System to keep track of the players wanting to play multiple games.

Entering the Names of the Players

Since we are creating a Scoreboard, it becomes necessary for us to display the names of each player.

Here is the syntax is shown below for the same:

Syntax:

Explanation:

As we can observe, we have used the special variable __name__ to have the value of “__main__”. We have then provided the input for the names of the first and second players, respectively. This will become the entry point for the program, and when the program we will be executed, it will ask for the names first.

Storing the Information regarding the Game

We have to store the information such as the current player, the player’s selection (i.e., X or O), the available selections (X or O), and the scoreboard.

Syntax:

Explanation:

In the above snippet of code, we have set the current player as the First player. We have also stored the selections made by Players, available options, and the scoreboard.

Designing the Scoreboard

We will design a scoreboard in a dictionary data structure. For this scoreboard, the player names will act as the keys, and their total number of victories will act as the values. Let us consider the following snippet of code to design the Scoreboard for Tic-Tac-Toe.

Syntax:

Explanation:

In the above snippet of code, we have defined the function as myscoreboard that takes the parameter as the scoreboard. We have then printed the design for the Score Board. We have defined the variable that stores the names of the players as a list using the .keys() function. We have then indexed them into the scoreboard and displayed the scores.

Creating an Outer Game Loop

In order to maintain multiple matches of Tic-Tac-Toe, we require another loop for the game. The current player will choose the mark for each match. The menu for selection should be displayed in every iteration of the game.

Let us consider the following syntax to create the Outer Gamer Loop.

Syntax:

Explanation:

In the above snippet of code, we have created a while loop to display the main menu for the Players where the current player can make the selection between the marks (Cross ‘X’ or Naught ‘O’) or exit the game.

Output:

First Player
Specify the Name: Andy


Second Player
Specify the Name: Carlo


        --------------------------------
                 SCORE BOARD       
        --------------------------------
            Andy             0
            Carlo            0
        --------------------------------

Andy will make the choice:
Press 1 for X
Press 2 for O
Press 3 to Quit

Handling and Assigning the Selections for Player

We need to handle and store the choice of the current player for each iteration. Let us consider the following snippet of code for the same.

Syntax:

Explanation:

In the above snippet of code, we have used the try-exception block to handle any exception for the the_choice input. We have then used the if-else statement to create the choice menu for the current player to select from.

As per the selection made by the player, the data will be stored. This is significant as it will tell us which player won after every match.

Executing the Game

Once all the required information is stored, we can execute an independent match and record the victory mark.

The syntax for the same has been shown below.

Syntax:

Explanation:

In the above snippet of code, we have stored the winner details for a single game of Tic-Tac-Toe.

Updating the Scoreboard

We have to update the scoreboard after every match of the Tic-Tac-Toe game.

Let us consider the following snippet of code to update the scoreboard.

Syntax:

Explanation:

In the above snippet of code, we have used the if statement to check if the match is not tied. Once the match is not drawn, the scoreboard will be updated.

Switching the Selecting Player

While playing a game, it becomes mandatory to switch the chance of choosing the mark. So, let us consider the following syntax to understanding the swapping.

Syntax:

Explanation:

In the above snippet of code, we have again used the if-else statement to switch between the players to choose the marks (Cross or Naught).

Hence, we have successfully constructed our very own Tic-Tac-Toe game. The code for the game can be downloaded from this link: Click Here To Download

It’s Gameplay Time

Since all the steps are finally completed, here is the final output for the game.

Output:

First Player
Specify the Name: Andy


Second Player
Specify the Name: Carlo


        --------------------------------
                 SCORE BOARD       
        --------------------------------
            Andy             0
            Carlo            0
        --------------------------------

Andy will make the choice:
Press 1 for X
Press 2 for O
Press 3 to Quit
1


             |     |
             |     |
        _____|_____|_____
             |     |
             |     |
        _____|_____|_____
             |     |
             |     |
             |     |


Player  X  turn. Choose your Block : 5


             |     |
             |     |   
        _____|_____|_____
             |     |
             |  X  |   
        _____|_____|_____
             |     |
             |     |   
             |     |


Player  O  turn. Choose your Block : 3


             |     |
             |     |  O
        _____|_____|_____
             |     |
             |  X  |   
        _____|_____|_____
             |     |
             |     |   
             |     |


Player  X  turn. Choose your Block : 1


             |     |
          X  |     |  O
        _____|_____|_____
             |     |
             |  X  |   
        _____|_____|_____
             |     |
             |     |   
             |     |


Player  O  turn. Choose your Block : 9


             |     |
          X  |     |  O
        _____|_____|_____
             |     |
             |  X  |   
        _____|_____|_____
             |     |
             |     |  O
             |     |


Player  X  turn. Choose your Block : 6


             |     |
          X  |     |  O
        _____|_____|_____
             |     |
             |  X  |  X
        _____|_____|_____
             |     |
             |     |  O
             |     |


Player  O  turn. Choose your Block : 4


             |     |
          X  |     |  O
        _____|_____|_____
             |     |
          O  |  X  |  X
        _____|_____|_____
             |     |
             |     |  O
             |     |


Player  X  turn. Choose your Block : 2


             |     |
          X  |  X  |  O
        _____|_____|_____
             |     |
          O  |  X  |  X
        _____|_____|_____
             |     |
             |     |  O
             |     |


Player  O  turn. Choose your Block : 8


             |     |
          X  |  X  |  O
        _____|_____|_____
             |     |
          O  |  X  |  X
        _____|_____|_____
             |     |
             |  O  |  O
             |     |


Player  X  turn. Choose your Block : 7


             |     |
          X  |  X  |  O
        _____|_____|_____
             |     |
          O  |  X  |  X
        _____|_____|_____
             |     |
          X  |  O  |  O
             |     |


Game Tied


        --------------------------------
                 SCORE BOARD       
        --------------------------------
            Andy             0
            Carlo            0
        --------------------------------

Carlo will make the choice:
Press 1 for X
Press 2 for O
Press 3 to Quit
3
The Final Scores
        --------------------------------
                 SCORE BOARD
        --------------------------------
            Andy             0
            Carlo            0
        --------------------------------

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263502.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论