Edgar.A Edgar.A - 1 year ago 69
Android Question

Android TicTacToe - random game endings

So today I was making TicTacToe for learning purposes, everything seemed good until i tried playing it myself - at some cases game ends before it should. Let's say this is my tictactoe grid:

1|2|3
4|5|6
7|8|9


So for example when i place X at 2 and O game suddenly endes concluding that player 2 (O user) won.

Here's my function for finding out if somebody won. I'm probably silly for doing calculations like this, but i don't really know any other methods of finding when somebody won and deciding who is the winner. Just simple 8 if statements on all possible outcomes which includes winner.

(plr1, plr2 - String with values of "X" and "O", text1,2,3 are textviews for X and O and they are arranged same as in grid above)

if(text1.getText().toString() == text2.getText().toString() && text2.getText().toString() == text3.getText().toString()) {
if(text1.getText().toString() == plr1) {
status.setText("Player 1 won!");
gameRunning = false;
}
else if(text1.getText().toString() == plr2){
status.setText("Player 2 won!");
gameRunning = false;
}
}
else if(text4.getText().toString() == text5.getText().toString() && text5.getText().toString() == text6.getText().toString()) {
if(text4.getText().toString() == plr1) {
status.setText("Player 1 won!");
gameRunning = false;
}
else if(text1.getText().toString() == plr2){
status.setText("Player 2 won!");
gameRunning = false;
}
}
else if(text7.getText().toString() == text8.getText().toString() && text8.getText().toString() == text9.getText().toString()) {
if(text1.getText().toString() == plr1) {
status.setText("Player 1 won!");
gameRunning = false;
}
else if(text1.getText().toString() == plr2){
status.setText("Player 2 won!");
gameRunning = false;
}
}
else if(text1.getText().toString() == text4.getText().toString() && text4.getText().toString() == text7.getText().toString()) {
if(text1.getText().toString() == plr1) {
status.setText("Player 1 won!");
gameRunning = false;
}
else if(text1.getText().toString() == plr2){
status.setText("Player 2 won!");
gameRunning = false;
}
}
else if(text2.getText().toString() == text5.getText().toString() && text5.getText().toString() == text8.getText().toString()) {
if(text1.getText().toString() == plr1) {
status.setText("Player 1 won!");
gameRunning = false;
}
else if(text1.getText().toString() == plr2){
status.setText("Player 2 won!");
gameRunning = false;
}
}
else if(text3.getText().toString() == text6.getText().toString() && text6.getText().toString() == text9.getText().toString()) {
if(text1.getText().toString() == plr1) {
status.setText("Player 1 won!");
gameRunning = false;
}
else if(text1.getText().toString() == plr2){
status.setText("Player 2 won!");
gameRunning = false;
}
}
else if(text1.getText().toString() == text5.getText().toString() && text5.getText().toString() == text9.getText().toString()) {
if(text7.getText().toString() == plr1) {
status.setText("Player 1 won!");
gameRunning = false;
}
else if(text7.getText().toString() == plr2){
status.setText("Player 2 won!");
gameRunning = false;
}
}
else if(text3.getText().toString() == text5.getText().toString() && text5.getText().toString() == text7.getText().toString()) {
if(text1.getText().toString() == plr1) {
status.setText("Player 1 won!");
gameRunning = false;
}
else if(text1.getText().toString() == plr2){
status.setText("Player 2 won!");
gameRunning = false;
}
}


Problem logically should be at if statements, but for me they seem okay, maybe I'm missing something, because currently is midnight here.

Answer Source

Comparing strings you get from TextViews is a performance bottleneck (even if done only a few times) and, more generally, not good practise. As you showed, your TicTacToe grid could fit in a 3x3 array, that could be defined as such: byte[][] grid = new byte[3][3];. A byte is enough as a grid square can only have 3 states: empty (e.g.0), X (1), and O (2).

Checking for winners is then easier, cleaner and faster:

  1. Check for horizontal wins

    for (int i = 0; i < 3; i++) {
        if ((grid[0][i] == grid[1][i]) && (grid[1][i] == grid[2][i])) {
            ...
        }
    }
    
  2. Check for vertical wins (for each column...)

  3. Check for the 2 diagonal wins (which is loopable but not worth it for a fixed 3×3 board)

In each case of a win, just take any value from the winning line to determine who won.