Dracose
Dracose

Reputation: 67

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);
            } 
          } 
        }
    }

Upvotes: 0

Views: 1874

Answers (3)

ScottK
ScottK

Reputation: 1556

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

Upvotes: 2

Florian S.
Florian S.

Reputation: 446

Some general things:

First of all you should not do

loop = ++loop;

If you want to increment loop just code

++loop;

Second instead of doing all this if else (loop == ...) stuff you can do

TicTac[loop][0] = uno;
TicTac[loop][1] = dos;
TicTac[loop][2] = tres;
++loop;
ticTacLine = scanner.nextLine();

only once.

And last if you know the number of loops it is much easier and better to read when you use a for loop instead of a while loop.

Now to the program itself.

The

ticTacLine = scanner.nextLine();

is not required because you are doing

String ticTacLine = scanner.nextLine();

at each begin of the loop. Currently you are reading the input twice with every loop.

And the printing should be outside of the loop. Then you also don't need to check for the loop value but just call it once.

What you read from the inout is characters, but what you put into the array is the int value of the character. If you want to store characters you have to use a character array.

Upvotes: 1

Lucas Engleder
Lucas Engleder

Reputation: 671

First of all i want to give you an important piecoe of advice when it comes to structuring with conditional statements. Try to minimize your code ina n if or else statement

Secondly heres the error

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

You see you use the | operator for a boolean statement. You apparently confused the bitwise operator | and the logical operator || (or)

It's very important to know the the difference. You see the bitwise operator basically takes two binary numbers like

1001 | 1010 = 1011 

because its like the logical operator but for 1 and zero. Attention: Theres also the bitwise AND operator that looks like & but its not the logical &&

Upvotes: 0

Related Questions