Kat Kat - 7 months ago 15
Java Question

Counter for Android Studio increments wrong

I'm making a tic tac toe android app and I want the score to be kept. I have everything working except for the increment. For some reason, instead of going up by 1, it goes up by 2.

This is the method I use for when I'm incrementing the score and checking the game to see if there is a winner or not:

private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private TextView winner;
private Button clearBoard;
private int scoreX;
private int scoreY;
private int tie;
private boolean turn = false; //determines who's turn it is

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

one = (Button) findViewById(R.id.box1);
one.setText("-");
two = (Button) findViewById(R.id.box2);
two.setText("-");
three = (Button) findViewById(R.id.box3);
three.setText("-");
four = (Button) findViewById(R.id.box4);
four.setText("-");
five = (Button) findViewById(R.id.box5);
five.setText("-");
six = (Button) findViewById(R.id.box6);
six.setText("-");
seven = (Button) findViewById(R.id.box7);
seven.setText("-");
eight = (Button) findViewById(R.id.box8);
eight.setText("-");
nine = (Button) findViewById(R.id.box9);
nine.setText("-");

scoreX = 0;
scoreY = 0;
tie = 0;

winner = (TextView) findViewById(R.id.win);
clearBoard = (Button) findViewById(R.id.newgame);
clearBoard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
one.setText("-");
one.setEnabled(true);
two.setText("-");
two.setEnabled(true);
three.setText("-");
three.setEnabled(true);
four.setText("-");
four.setEnabled(true);
five.setText("-");
five.setEnabled(true);
six.setText("-");
six.setEnabled(true);
seven.setText("-");
seven.setEnabled(true);
eight.setText("-");
eight.setEnabled(true);
nine.setText("-");
nine.setEnabled(true);

}
});
}

public void game(View view) {
Button b = (Button) view;
moves(b);
}

public void moves(Button b){
if(turn == false){
b.setText("X");
b.setTextSize(15);
} else {
b.setText("O");
b.setTextSize(15);
}

//once X or O is placed, cannot reuse that button till new game
b.setEnabled(false);
b.setTextSize(15);
turn = !turn;
check();

//if there is a winner, make it so buttons can't be pressed and new game must start
if(check() == true){
one.setEnabled(false);
two.setEnabled(false);
three.setEnabled(false);
four.setEnabled(false);
five.setEnabled(false);
six.setEnabled(false);
seven.setEnabled(false);
eight.setEnabled(false);
nine.setEnabled(false);
}
}

public boolean check() {
boolean finished = true;
int player = 1; //1 is player X, 0 is player O, 2 is a tie
//horizontal check
if((!one.getText().equals("-")) && one.getText().toString().equals(two.getText().toString()) && two.getText().toString().equals(three.getText().toString())) {
if (one.getText().equals("O")) {
player = 0;
}
} else if((!four.getText().equals("-")) && four.getText().toString().equals(five.getText().toString()) && five.getText().toString().equals(six.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if((!seven.getText().equals("-")) && seven.getText().toString().equals(eight.getText().toString()) && eight.getText().toString().equals(nine.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if((!one.getText().equals("-")) && one.getText().toString().equals(four.getText().toString()) && four.getText().toString().equals(seven.getText().toString())){
//vertical check
if (one.getText().equals("O")) {
player = 0;
}
} else if((!two.getText().equals("-")) && two.getText().toString().equals(five.getText().toString()) && five.getText().toString().equals(eight.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if((!three.getText().equals("-")) && three.getText().toString().equals(six.getText().toString()) && six.getText().toString().equals(nine.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if((!one.getText().equals("-")) && one.getText().toString().equals(five.getText().toString()) && five.getText().toString().equals(nine.getText().toString())){
//diagonal check
if (one.getText().equals("O")) {
player = 0;
}
} else if((!three.getText().equals("-")) && three.getText().toString().equals(five.getText().toString()) && five.getText().toString().equals(seven.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if ((!one.getText().equals("-")) && (!two.getText().equals("-")) && (!three.getText().equals("-")) && (!four.getText().equals("-")) && (!five.getText().equals("-"))
&& (!six.getText().equals("-")) && (!seven.getText().equals("-")) && (!eight.getText().equals("-")) && (!nine.getText().equals("-"))){
player = 2;
} else {
return finished = false;
}

if(player == 1){
scoreX = scoreX + 1;
winner.setText(String.format("Player X is the winner!\nGames won by player X: %d\nGames won by player O: %d\nNumber of ties: %d", scoreX, scoreY, tie));
} else if (player == 0){
scoreY = scoreY + 1;
winner.setText(String.format("Player O is the winner!\nGames won by player X: %d\nGames won by player O: %d\nNumber of ties: %d", scoreX, scoreY, tie));
} else if(player == 2){
tie= tie + 1;
winner.setText(String.format("It's a tie!\nGames won by player X: %d\nGames won by player O: %d\nNumber of ties: %d", scoreX, scoreY, tie));
}

return finished;
}
}


My variables, scoreX, scoreY, and tie are all member variables and set to 0. I must be missing something because I just can't see how my variables are incrementing by 2 instead of 1. Please help!

EDIT: I know the coding isn't pretty. I'm still a beginner. I added in all of my code. I know especially my check() method is long, though I'm not sure how I could shorten the length of that as I am checking the buttons on the game board.

Answer

It happens because you are calling your check function twice: I can't really give you the line numbers but this is where it happens (in the moves function):

check();

//if there is a winner, make it so buttons can't be pressed and new game   must start
if(check() == true){

What you should do instead is save the result of your first check call and then use that:

boolean result = check();

//if there is a winner, make it so buttons can't be pressed and new game   must start
if(result == true){