I'm trying to create a tic tac toe game for practice using simple buttons that change their text to x or o. Once a winner is found, all buttons should be reset to have the text "..." instead of x/o. It feels stupid to be doing this for each button but I can't figure out how to iterate over them. The buttons are named button1, button2, ..., and button9. Right now I'm using the same 3 lines of code for each button and just repeating that and that's not very DRY-friendly.
Tried to do a for loop and string concatenation (sort of like: findViewById(R.id."button"+i), but that obviously doesn't work hahah). I'm sure this is a stupid question but I've had this problem a couple of times now and I really wanna figure out how to so this better. [Image of the App][1]
if(winnerFound){
// print winner on screen and reset game
resetGame();
}
}
public void resetGame(){
// set all buttons clickable again and set text to "..."
// could be done in a for loop but idk how
Button button1 = (Button) findViewById(R.id.button1);
button1.setText("...");
button1.setEnabled(true);
Button button2 = (Button) findViewById(R.id.button2);
button2.setText("...");
button2.setEnabled(true);
Button button3 = (Button) findViewById(R.id.button3);
button3.setText("...");
button3.setEnabled(true);
Button button4 = (Button) findViewById(R.id.button4);
button4.setText("...");
button4.setEnabled(true);
// ... (9 buttons in total)'''
[1]: https://i.stack.imgur.com/1ocbZ.jpg