Reputation: 2074
In a tic-tac toe code, I had a do-while loop that checks if one of the players have won..so, something like this
do{
//does some player's input and other things..
}while(!win(x));
now, the big problem is in this loop, it will continue looping until one of the players win. now, how do i check a tie using this same do-while loop?
could I do something like this :
do{
//still the same checking
}while(!win(x)||!loose(x));
I did try this but it just messed up the code. How could I possibly check a tie in the game?
Thanks
Upvotes: 0
Views: 1558
Reputation: 14363
When you are writing:
!win(x)||!loose(x)
You say not won or not lost and the loop will terminate in the first go. You can use the following:
do{
//still the same checking
} while (!win(x)&&!loose(x));
or
do{
//still the same checking
} while (!(win(x)||loose(x)));
Upvotes: 0
Reputation: 212929
Your logic is slightly off - change the loop condition from:
do{
//still the same checking
}while(!win(x)||!loose(x));
to:
do{
//still the same checking
}while(!win(x)&&!loose(x));
or perhaps a more easily understood, but equivalent alternative:
do{
//still the same checking
}while(!(win(x)||loose(x)));
Upvotes: 2