Search code examples
javaandroid-studiotic-tac-toe

Popup Window to go first or second


I'm writing a simple tic tac toe game and after each win or loss, I want the player to chose whether they want to go first or second (be the X or 0). What is the best strategy for getting this information?


Solution

  • You can Display an AlertDialog for this to have RadioButton for 0 and X so its easy and recommended way to give an options to players .

    See sample code for your reference:

    AlertDialog playerDialog;
    
    // Strings to Show In Dialog with Radio Buttons
    final CharSequence[] items = {" Zero "," Cross "};
    
     // Creating and Building the Dialog 
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select Your Player");
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
    switch(item)
    {
    case 0:
    // Your code when first option selected
    break;
    case 1:
    // Your code when 2nd  option selected
    break;
    }
    playerDialog.dismiss();    
    }
    });
    playerDialog = builder.create();
    playerDialog.show();