0

I'm a newbie in java and I'm working on a tic-tac-toe project. I set up a toast to appear when player 1 or 2 wins, but I can't find out how to toast a draw. I got the idea of checking the players' toasts--if they hadn't been displayed, and there are no more empty spaces, then a toast for draw shows up. I can't really figure it out and any help would be very much appreciated

PS im sorry if that was too puzzling, so heres the summary: how can I check if a toast was displayed?

Thanks in advance

Aya Noaman
  • 334
  • 1
  • 2
  • 12
  • 3
    Does this answer your question? [How to test for the appearance of a Toast message](https://stackoverflow.com/questions/2405080/how-to-test-for-the-appearance-of-a-toast-message) – AgentP Jun 27 '20 at 14:36

2 Answers2

0

I can't imagine checking the toast is a reliable method and I definitely wouldn't use it, event if it is somehow possible (which doesn't seem to be the case, at least natively and without deprecated methods).

Although I don't know how your app is setup (local/remote multiplayer etc...), I would rather recommend using another variable, which indicates that the game is over, because one player has won already. Then you can check this variable and whether there are empty spaces, in order to toast a draw.

Another possibility would be to check first if one or the other player has won and only if that is not the case you would check if there are empty spaces left. If there are non, it's definitely a draw. (At least if you don't run the code again even if the game is over.)

Salo
  • 11
  • 2
0

Do one thing. Make two variables - p1Won and p2Won. Both the variables should be set to false initially.

boolean p1Won = false;
boolean p2Won = false;

If player 1 wins,

p1Won = true;

And if player 2 wins,

p2Won = true;

But, if you see that there is no free box left, then do this

if (!p1Won && !p2Won)
Toast.makeText(YourActivityName.this, "Draw", Toast.LENGTH_LONG).show();
Samudra Ganguly
  • 637
  • 4
  • 24