The game "tic-zeroes". Created 9 buttons, placed in an array to declare and distribute id. I can not figure out how to make sure that after my pressing the button (to enter X), a null appears on another random button. I understand that you need to use Math.random (). Just like ... A simple question, ashamed to ask, please ask a farewell.

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button button[]= new Button[9]; Button b1pl,b2pl; private Integer allid[]={ R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8, R.id.button9}; boolean first= true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b2pl = (Button) findViewById(R.id.b2pl); b2pl.setOnClickListener(this); b1pl = (Button) findViewById(R.id.b1pl); b1pl.setOnClickListener(this); for (int i=0;i<allid.length;i++){ button[i]=findViewById(allid[i]); button[i].setOnClickListener(this); } } @SuppressLint("ResourceAsColor") @Override public void onClick(View view) { Button current = new Button(this); current.setTextSize(24); switch (view.getId()){ case R.id.b1pl: if (first){ current.setTypeface(null,Typeface.BOLD); current.setTextColor(R.color.colorPrimaryDark); current.setText("X"); } else { current.setText("0"); current.setTextColor(R.color.colorPrimaryDark); } first=!first; } } } 

    2 answers 2

    Specifically on your question I can advise the following:

    Create your own MyButton class with, for example, such functions:

     psfi BLANK = 0; psfi CROSS = 1; psfi ZERO = 2; int state; void setState(int state){ String text = ""; switch (state){ case CROSS: text="X"; break; case ZERO: text="0"; break; } this.setText(text); this.state = state; } int getState(){ return state; } 

    In the activation you need to add all the buttons in the ArrayList<MyButton> buttons

    and get random empty by this method:

     MyButton getRandomButton(){ int random = 0; do { random = new Random().nextInt(buttons.size()); } while (buttons.get(random).getState() != MyButton.BLANK); return buttons.get(random); } 

    where you need:

     getRandomButton().setState(MyButton.CROSS); 
    • What does the psfi class mean? (1-3 line) - Oleg Khegay
    • one
      public static final int . Try typing in Studio and clicking enter. There are several such chips that speed up the process of creating code. - Jarvis_J
    • Thank. What is this.setText (text) for; this.state = state; ? - Oleg Khegay
    • one
      this.state remembers the state of each button, and this.setText refers to the button itself and changes its value (text). Or did not understand what the question was :) - Jarvis_J
    • understandably. One more thing ... Why do you have the class name MyButton in front of the getRandomButton method? There are no access modifiers and the type of the variable returned is int? - Oleg Khegay

    In this implementation, I suppose you need to store the id of the buttons in a certain list. Every time a user clicks on any button - you remove its id from this list, generate a random number from 0 to N (list length), search for this element, set the value of a zero in this random cell, delete the id of this button from the general list. In truth, it will be boring to play with such a computer) I advise you not to think about the randomization, but about the implementation of the computer. Cristiki tac-toe game is simple - it is limited to M number of options.

    Again, general tips for beginners. Read about binding (the need to write findViewById will disappear). Write code for OOP (not all in one method). Your task is to learn and embrace as much as possible, and not to write tic-tac-toe - remember this! Good luck.

    enter image description here