Search code examples
javaarraysmultidimensional-arraytic-tac-toe

Creating a tic tac toe board through the use of a two dimensional array


In this code, I try to create a two dimensional array that represents a tic tac toe board (with user input) but no matter what I enter in "TicTacLine", the program always brings up "Have you never played Tic Tac Toe before ? It's okay if you haven't, but just FYI, it plays with x's and o's.", which is the message I wrote if the characters in uno, dos and tres weren't either x's or o's.

public class TicTacToe {

    public static void main(String[] args) {
        int TicTac[][]= new int[3][3];

        System.out.println("Enter the Tic Tac Toe board you want to see, one line at a time.");
        Scanner scanner = new Scanner(System.in);
        String TicTacLine = scanner.nextLine();     
        int loop = 0;

        if (TicTacLine.length()<3 | TicTacLine.length()>3) {  // I try to define the array by a series of inputs that go in the while loop.
            System.out.println("Tic-tac-toe plays in a 3×3 grid. This means if you want to input a line, you would want to input 3 characters, no more, no less.");
        } else { 
            while (loop != 3) { // we count the loops so that there's only 3 different lines
                char uno = TicTacLine.charAt(0);
                char dos = TicTacLine.charAt(1);
                char tres = TicTacLine.charAt(2);
                if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o') {
                    System.out.println("Have you never played Tic Tac Toe before ? It's okay if you haven't, but just FYI, it plays with x's and o's.");
                    break;
                } else { 
                    if (loop == 0) {
                        TicTac[0][0] = uno;
                        TicTac[0][1] = dos;
                        TicTac[0][2] = tres; 
                        loop = ++loop;
                        TicTacLine = scanner.nextLine();
                    } if (loop == 1) {
                        TicTac[1][0] = uno;
                        TicTac[1][1] = dos;
                        TicTac[1][2] = tres;
                        loop = ++loop;
                        TicTacLine = scanner.nextLine();
                    } if (loop == 2) {
                        TicTac[2][0] = uno;
                        TicTac[2][1] = dos;
                        TicTac[2][2] = tres;
                        loop = ++loop;
                        TicTacLine = scanner.nextLine();
                        }
                    }
                }
            }   
        if (loop == 3) {
        for(int[] row : TicTac) {
            PrintingRow(row);
            } 
          } 
        }
    }

Solution

  • Your boolean expression within the IF statement will always be true:

    if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o')
    

    Why? Because you are ORing the result of all six subexpressions together. Since it is the case that at least three of these will always be true, and possibly all six are true, then the whole expression will always be true.

    Look at the first two boolean expressions that you are bitwise ORing together. This returns true always because either uno != 'x' is true, or uno != 'o' is true, or both, so the expression will always be true, so you will always execute the

    System.out.println("Have you never played Tic Tac Toe before ? It's okay if you haven't, but just FYI, it plays with x's and o's.");
    break;
    

    You need to rewrite this using logical ORs and ANDs as follows:

    if ((uno != 'x' && uno != 'o') || (dos != 'x' && dos != 'o') || (tres != 'x' && tres != 'o')) {
    

    This says, evaluate to true if uno is not 'x' and uno is not 'o', or evaluate to true if dos is not 'x' and dos is not 'o', or evaluate to true if tres is not 'x' and tres is not 'o'

    For more information on Java Operators, Oracle has good documentation here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html