The game works fine for me when a cross wins, an alertdialog appears, in general everything works except a draw, here are the methods for winning

public void winnerx() { boolean finish = false; String s1 = btn1.getText().toString(); String s2 = btn2.getText().toString(); String s3 = btn3.getText().toString(); String s4 = btn4.getText().toString(); String s5 = btn5.getText().toString(); String s6 = btn6.getText().toString(); String s7 = btn7.getText().toString(); String s8 = btn8.getText().toString(); String s9 = btn9.getText().toString(); if (s1.equals("X") && s2.equals("X") && s3.equals("X") || s1.equals("X") && s4.equals("X") && s7.equals("X") || s1.equals("X") && s5.equals("X") && s9.equals("X") || s2.equals("X") && s5.equals("X") && s8.equals("X") || s3.equals("X") && s2.equals("X") && s1.equals("X") || s3.equals("X") && s6.equals("X") && s9.equals("X") || s3.equals("X") && s5.equals("X") && s7.equals("X") || s4.equals("X") && s5.equals("X") && s6.equals("X") || s7.equals("X") && s8.equals("X") && s9.equals("X")) { finish = true; AlertDialog.Builder builder = new AlertDialog.Builder(Main4Activity.this); builder.setTitle("Winner - X").setMessage("Do you want to play again?").setCancelable(false). setPositiveButton("Play again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Main4Activity.this, Main4Activity.class); startActivity(intent); finish(); } }).setNegativeButton("Exit", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Main4Activity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); } }); AlertDialog dialog = builder.create(); dialog.show(); } if (finish) { btn1.setEnabled(false); btn2.setEnabled(false); btn3.setEnabled(false); btn4.setEnabled(false); btn5.setEnabled(false); btn6.setEnabled(false); btn7.setEnabled(false); btn8.setEnabled(false); btn9.setEnabled(false); } } 

Here it is for zero

  public void winnero() { boolean finish = false; String s1 = btn1.getText().toString(); String s2 = btn2.getText().toString(); String s3 = btn3.getText().toString(); String s4 = btn4.getText().toString(); String s5 = btn5.getText().toString(); String s6 = btn6.getText().toString(); String s7 = btn7.getText().toString(); String s8 = btn8.getText().toString(); String s9 = btn9.getText().toString(); if (s1.equals("O") && s2.equals("O") && s3.equals("O") || s1.equals("O") && s4.equals("O") && s7.equals("O") || s1.equals("O") && s5.equals("O") && s9.equals("O") || s2.equals("O") && s5.equals("O") && s8.equals("O") || s3.equals("O") && s2.equals("O") && s1.equals("O") || s3.equals("O") && s6.equals("O") && s9.equals("O") || s3.equals("O") && s5.equals("O") && s7.equals("O") || s4.equals("O") && s5.equals("O") && s6.equals("O") || s7.equals("O") && s8.equals("O") && s9.equals("O")) { finish = true; AlertDialog.Builder builder = new AlertDialog.Builder(Main4Activity.this); builder.setTitle("Winner - O").setMessage("Do you want to play again?").setCancelable(false). setPositiveButton("Play again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Main4Activity.this, Main4Activity.class); startActivity(intent); finish(); } }).setNegativeButton("Exit", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Main4Activity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); } }); AlertDialog dialog = builder.create(); dialog.show(); } if (finish) { btn1.setEnabled(false); btn2.setEnabled(false); btn3.setEnabled(false); btn4.setEnabled(false); btn5.setEnabled(false); btn6.setEnabled(false); btn7.setEnabled(false); btn8.setEnabled(false); btn9.setEnabled(false); } } 

I tried this method for nobody, but it does not work

 public void draw(){ if(btn1.isPressed() && btn2.isPressed() && btn3.isPressed() && btn4.isPressed() && btn5.isPressed() && btn6.isPressed() && btn7.isPressed() && btn8.isPressed() && btn9.isPressed()){ AlertDialog.Builder builder = new AlertDialog.Builder(Main4Activity.this); builder.setTitle("DRAW").setMessage("Do you want to play again?").setCancelable(false). setPositiveButton("Play again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Main4Activity.this, Main4Activity.class); startActivity(intent); finish(); } }).setNegativeButton("Exit", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Main4Activity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); } }); AlertDialog dialog = builder.create(); dialog.show(); } } 

    2 answers 2

    Try this (although I would rewrite all the code in general so that it would be easier to understand):

     public void draw(){ String s1 = btn1.getText().toString(); String s2 = btn2.getText().toString(); String s3 = btn3.getText().toString(); String s4 = btn4.getText().toString(); String s5 = btn5.getText().toString(); String s6 = btn6.getText().toString(); String s7 = btn7.getText().toString(); String s8 = btn8.getText().toString(); String s9 = btn9.getText().toString(); if (!(s1.isEmpty() || s2.isEmpty() || s3.isEmpty() || s4.isEmpty() || s5.isEmpty() || s6.isEmpty() || s7.isEmpty() || s8.isEmpty() || s9.isEmpty())) { AlertDialog.Builder builder = new AlertDialog.Builder(Main4Activity.this); builder.setTitle("DRAW").setMessage("Do you want to play again?").setCancelable(false). setPositiveButton("Play again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Main4Activity.this, Main4Activity.class); startActivity(intent); finish(); } }).setNegativeButton("Exit", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Main4Activity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); } }); AlertDialog dialog = builder.create(); dialog.show(); } } 
    • I'll try and tell you this, ATP - name_surname
    • ATP all worked))) I do not know, Che was not written)) - name_surname
    • I'm not a big specialist in android, I suspect that isPressed did not return true in this case, and in the code above we used the same algorithm for getting the value through strings, as in other methods. - Viacheslav Vedenin

    Just like for a cross and a zero, get the text from each button, check that the text on one of them is not equal to "" (or what it is equal to by default), and then check that neither the cross, nor the zero has won.

    • It did not help, I did not succeed, I thought if removing the text would work, but alas not)) - name_surname
    • @ hayk.mkrtchyan2017 what exactly goes wrong? - pinguin
    • I do not understand it myself, I am writing the condition correctly but it does not work - name_surname