Skip to main content

Logic Behind the Code

Until now, we have seen design of the game. Now, lets discuss about the logic.
As you know, our project in divide into basic 3 parts.

  1. User setting
  2. One user Mode
  3. Two User Mode
In this blog, we will see about the basic and common logic which is used in both modes.
Step by step explanation:


·         The game is basically made for two players as ‘X’ and ‘O’. So, it is necessary to make sure that after the player ‘X’, player ‘O’ gets the chance to play and after player ‘O’ again player ‘X’ gets the chance. i.e. Toggling of the player is important.
For this we have used Boolean variable flag.

Pseudo code:

If (flag==true)
         //player ‘X’ will be playing
else
        //player ‘O’  will be playing

flag = !flag             //Toggling the player

·         The second important thing in this game is that if one player uses a button, neither the other player nor the same player himself should be able to use that button again. i.e. The button should get locked after one use. It can be achieved by simple logic that the button can be used only if the label on the button is blank.
Pseudo  code:

If(button.getLabel().equals(””))
             //player can use the button

·         As in every game, there should be condition to win the game. For the Tic tac toe, the player wins if he successfully completes one row/column/diagonal column of his symbol. To check it, we write a function win().

In our game, this function is being called after every player’s turn to check whether the winning condition has been fulfilled.    
Pseudo code:

void win()
{
          If( //row 1 has the same label )
               {
                         If(label==”X”)
                                       // ’X’ is winner
                          If(label==”O”)
                                      // ‘O’ is winner
               }
          else if( //row 2 has the same label )
               {
                         If(label==”X”)
                                       // ’X’ is winner
                          If(label==”O”)
                                      // ‘O’ is winner
               }
          else if( //row 3 has the same label )
               {
                         If(label==”X”)
                                       // ’X’ is winner
                          If(label==”O”)
                                      // ‘O’ is winner
               }
          else if( //column 1 has the same label )
               {
                         If(label==”X”)
                                       // ’X’ is winner
                          If(label==”O”)
                                      // ‘O’ is winner
               }
          else if( //column 2 has the same label )
               {
                         If(label==”X”)
                                       // ’X’ is winner
                          If(label==”O”)
                                      // ‘O’ is winner
               }
          else if( //column 3 has the same label )
               {
                         If(label==”X”)
                                       // ’X’ is winner
                          If(label==”O”)
                                      // ‘O’ is winner
               }
          else if( diagonal column 1 has the same label )
               {
                         If(label==”X”)
                                       // ’X’ is winner
                          If(label==”O”)
                                      // ‘O’ is winner
               }
          else if( //diagonal column 2 has the same label )
               {
                         If(label==”X”)
                                       // ’X’ is winner
                          If(label==”O”)
                                      // ‘O’ is winner
               }

}

·         To check the winning condition, we need to call function win() after every player’s turn.
Pseudo code:

If (flag==true)
         //player ‘X’ will be playing
         win();
else
        //player ‘O’  will be playing
        win();

flag = !flag  

·         If one player wins the game, then no one should able to make another move. After clicking on ‘Refresh’ button, new game should start. For this, after clicking on ‘Refresh button’, make label of every button blank.

Pseudo code:
Button.setLabel(“”);
This way, the new game will start. 

This is the basic algorithm behind our project. For the better understanding please refer to the attached flowchart.
In the next blog we will see what is the difference between the one user mode and the two user mode is. Until then HAPPY CODING!


Comments

  1. Thanks for the step wise code explanation.

    ReplyDelete
  2. Very nice work. It is a very informative blog.

    ReplyDelete
  3. Good logic!!nice attempt for one user mode!!

    ReplyDelete
  4. Veri Impressive πŸ’―πŸ™ŒπŸ»

    ReplyDelete
  5. You made it really easy to understand! Great!

    ReplyDelete

Post a Comment