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.- User setting
- One user Mode
- Two User Mode
In this blog, we
will see about the basic and common logic which is used in both modes.
·
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!
Helpful! π
ReplyDeleteInformative.!!
ReplyDeleteThank you
DeleteThanks for the step wise code explanation.
ReplyDeleteThank you so much
DeleteVery nice work. It is a very informative blog.
ReplyDeleteThanks
DeleteAll Informative
ReplyDeleteVery impressive!!!!
ReplyDeleteThanks
DeleteVery helpful!!
ReplyDeleteGood information
ReplyDeleteThanks
DeleteHelpful information π
ReplyDeleteNiceππ
ReplyDeleteGreat quality of information!
ReplyDeleteThanks
DeleteGood logic!!nice attempt for one user mode!!
ReplyDeleteNice ππ»π―
ReplyDeleteThank you
DeleteVeri Impressive π―ππ»
ReplyDeleteThanks
DeleteWell portrayed !
ReplyDeleteThank you so much
DeleteYou made it really easy to understand! Great!
ReplyDeleteThank you so much
DeleteNicely explained
ReplyDeleteThank you
DeleteInteresting stuff to read !
ReplyDeleteGood credible information!
ReplyDeleteThank you
Delete