skip to Main Content

I have made a simple tic tac toe game app using java.it is working as required, but after wining form one side it is keep taking the value. How to stop the execution after certain condition meet.
If this is one of the condition of declaring the winner

  else if (b1.equals(b4) && b4.equals(b7) && !b4.equals("")) {
                    Toast.makeText(this, "Winner is " + b1, Toast.LENGTH_LONG).show();

then what should do to stop taking the input from all the button until restart?

3

Answers


  1. i am not sure if i get your question but you can do it in while loop such as

    while(condition){
     // take the value
     }
    

    and you can just update the value of condition after it finished.

    Login or Signup to reply.
  2. You can always disable the buttons after that condition is met:

    button.setEnabled(false)
    

    Since you’re saying you don’t want the user to continue the game after it ended, you can also create an alert dialog, telling the game has ended, and when the user clicks "Ok" it restarts the game:

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
      
            // Set the message show for the Alert time
            builder.setMessage("Winner is (insert winner name)");
      
            // Set Alert Title
            builder.setTitle("Game Ended");
      
            // Set Cancelable false for when the user clicks on the outside the Dialog Box then it will remain showing
            builder.setCancelable(false);
      
            // Set the positive button with Ok
            builder.setPositiveButton("Ok", (DialogInterface.OnClickListener) (dialog, which) -> {
                // When the user click yes button then restart the game
                restartGame(); //Create this function to restart the game, for example
            });
      
            // Create the Alert dialog
            AlertDialog alertDialog = builder.create();
            // Show the Alert Dialog box
            alertDialog.show();
    
    Login or Signup to reply.
  3. You can set button.setEnabled(false) after your condition is satisfied.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search