Search code examples
javatic-tac-toe

tic tac toe horizontal/vertical and diagonal check not wokring - java


I am writing a tic tac toe game and everything seems to work except the checks (horizontal/vertical and diagonal). I am using an array (int [3][3]) to make the board.

These are my checks:

private boolean wonStraightLines( int player)
   {
       boolean answer = false;

       if (
            ((board[0][0] & board[0][1] & board[0][2]) == (player * 3)) || 
            ((board[1][0] & board[1][1] & board[1][2]) == (player * 3)) ||  
            ((board[2][0] & board[2][1] & board[2][2]) == (player * 3)) ||
            ((board[0][0] & board[1][0] & board[2][0]) == (player * 3)) ||
            ((board[0][1] & board[1][1] & board[2][1]) == (player * 3)) ||
            ((board[0][2] & board[1][2] & board[2][2]) == (player * 3))
          ) 
       {
           answer = true;   
       }
       else {
           answer = false;
       }
       return answer;
   }

and for the diagonal:

private boolean wonDiagonal( int player)
   {
      boolean answer = false;

      if (
              ((board[0][0] & board[1][1] & board[2][2]) == (player * 3)) || ((board[0][2] & board[1][1] & board[2][0]) == (player * 3))
         ) 
      {
          answer = true;
      }
      else {
          answer = false;
      }
      return answer;
   }

when I run the program whenever X or O get 3 from any direction the game keeps running rather than throwing out a "you win" message. Any help would be appreciated. Not sure if any other parts of the code are needed or not.

EDIT: I also tried using the + instead of the & between the board array values but did not work as well.


Solution

  • I'm assuming there are 3 unique values for the states of each element such as for example:

    board[row][col] = 0 // no entry
    board[row][col] = 1 // player 1
    board[row][col] = 2 // player 2
    

    Without being clever a horizontal check for player (using row-major structure) would look like:

    (board[0][0] == player && board[0][1] == player && board[0][2] == player)
    

    and repeat for each row.

    And similar for vertical, repeating for each column:

    (board[0][0] == player && board[1][0] == player && board[2][0] == player)
    

    And one diagonal:

    (board[0][0] == player && board[1][1] == player && board[2][2] == player)
    

    If you were determined to use an arithmetic operation then you'd have to change the "player" values to avoid overlap, as in player 1 == 1 and player 2 == 4, such as:

    board[row][col] = 0 // no entry
    board[row][col] = 1 // player 1
    board[row][col] = 4 // player 2
    

    Then you could do something like for a horizontal row:

    ((board[0][0] + board[0][1] + board[0][2]) == (player * 3))
    

    Note in Java 8 and later adding an int array (a row or column in your case) can be slightly more succinct:

    // sum the first row
    (IntStream.of(board[0]).sum() == (player * 3))
    

    Now if you really wanted to use the bit-wise "and" operation then this is your check assuming the initial element values (0,1,2) as stated at top:

    // check if the player's "bit" is set in all elements of first row.
    (board[0][0] & board[0][1] & board[0][2]) == player