0

So, I have created a java class to implement onClickListener and inside this class I have written the onClick public method. Outside of this method, I have created an int object and I want to modify this object inside the onClick method. I have researched a lot by also checking other similar SO questions and I have tried many things, like creating the object as a public int, or making it a private int and have another method to change it and then call this method inside onClick. However, nothing seems to work.

The code shown below has the int object created as a private int and named turn. To change it inside onClick, I have first created a public method named changeTurn that modifies it and then I call this method inside onClick.

public class TicTacToe implements View.OnClickListener { Button buttons[] = new Button[9]; TextView result; public TicTacToe(Button[] buttonList, TextView text) { buttons = buttonList; result = text; } //public void private int turn = 1; // The object that needs to be modified in onCLick @Override public void onClick(View v) { Button b = (Button) v; if((((Button) v).getText() != "X") && (((Button) v).getText() != "O")) { if(this.turn == 1) { b.setText("X"); changeTurn(); // ***Should change the value of turn*** result.setText("Turn is: " + this.turn); } if(this.turn == 2) { b.setText("O"); changeTurn(); // ***Should change the value of turn*** result.setText("Turn is: " + turn); } } } public void changeTurn() { if(this.turn == 1) { this.turn = 2; } if(this.turn == 2) { this.turn = 1; } } } 

From what I've tried, the program goes only inside the first if every time I click any of my 9 buttons, whose setOnClickListeners are connected to this onClick method. Also, the value of turn is always 1 when I print it out, which basically means that its value is not changed by changeTurn inside the onClick method.

General info on the application: I'm trying to make a tic-tac-toe game in a 3x3 grid with 9 buttons. Since there would be 2 players, I'm trying to use this turn integer to keep track of whose turn it is to press a button. If turn is 1, the button's text gets changed to X and if turn is 2, it changes to O. Right now, every time I press a button, it always changes to X.

I would really appreciate any help or ideas.

2
  • Hey @Klaus,can you tell me how do you setting click listener to you buttons? Commented Feb 21, 2016 at 20:24
  • The way I do that is by creating a TicTacToe object and then passing that to the setOnClickListener() on the main. So like this: TicTacToe tic = new TicTacToe(buttons, text); Button one = (Button) findViewById(R.id.button1); one.setOnClickListener(tic); Commented Feb 21, 2016 at 20:41

2 Answers 2

2

You're setting the turn to 2 and then immediately setting it back to 1.

// turn == 1 if(this.turn == 1) { // true this.turn = 2; // turn == 2 } if(this.turn == 2) { // now true! this.turn = 1; // turn == 1 } 

The easiest thing to do is to only enter the second block if the first is skipped, i.e.:

if(this.turn == 1) { this.turn = 2; } else if(this.turn == 2) { this.turn = 1; } 

Alternatively, if you're expecting to expand the block with more turn numbers, use switch:

switch(this.turn) { case 1: this.turn = 2; break; case 2: this.turn = 1; break; } 

The only trouble with switch is if you forget a break statement you end up with an unpredictable mess.

Finally, a quick bit of advice: if you're trying to create a loop of numbers (1 .. n then back to 1) then you should consider the modulus operator (%) like x = x % n + 1;

Sign up to request clarification or add additional context in comments.

1 Comment

Yeap, that's a great catch. I actually had to also do the same thing for the two other if statements, so make them if and else if. Thanks for the help.
1

try use it like this

final private int[] turn = {0} 

Then change code to

if(turn[0] == 1) { b.setText("X"); turn[0]=2; // ***Should change the value of turn*** result.setText("Turn is: " + turn); } if(turn[0] == 2) { b.setText("O"); turn[0]=1; // ***Should change the value of turn*** result.setText("Turn is: " + turn); } 

1 Comment

I just tried it and it doesn't seem to work. When I click any of the buttons now, it doesn't change at all. I think it doesn't actually go inside any of the two if statements. I also tried setting turn[0] = 1 outside the onClick method (to sort of initialize it), but it gives me an error saying "unexpected token".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.