1

The 2d boolean is a static variable if that means anything. I thought that I could refer to just the name of the array, but I get an error for doing that.

public class Game {

static boolean[][] board = new boolean[3][3];
}

public class Computer
{
  if (Game.board = true)
    {
        //code
    }
}
iii
  • 606
  • 1
  • 6
  • 21
  • 2
    you want to check if all elements in your array are true? – Chris Dargis Jan 29 '13 at 05:03
  • 1
    Be careful - aside from the issue at hand, you're using the assignment operator `=` in your `if` condition. Use `==` to test equality between primitives. Also, for `boolean`s, you can just write `if (someBoolean)` for example. – Paul Bellora Jan 29 '13 at 05:05
  • @DougRamsey Yes, that's what I wanted it to do. PaulBellora I've tried that already, and I still got an error. – iii Jan 29 '13 at 05:08

7 Answers7

1

Sure, Game.board is how you refer to the boolean[][] two-dimensional array. But what makes you think you can compare a 3x3 array of booleans to true directly?

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1

if(Game.board = true) will throw a compiler error because board is a two-dimensional array and thus cannot be assigned a single boolean value.

Also, when comparing make sure you use the comparison operator ==

Perhaps, you want to do something like:

if(Game.board[i][j] == true) where i and j are indexing the array:

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        if(board[i][j] == true)
        {
          // Code
        }
    }
}

You have board declared as static, any particular reason?

Since board[i][j] returns a boolean value, there really isn't a need for the == true:

if(board[i][j])
Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
  • Well, I tried to declare board as an instance variable, but it wouldn't let me, so I just used static instead. – iii Jan 29 '13 at 05:19
  • The code in if block will be executed as soon as it finds true, I thought OP wants to check if all the elements are true – Abdullah Shaikh Jan 29 '13 at 05:22
  • 1
    @igknighton: It is important to know the difference between static and non-static, and when to use which. http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil – Chris Dargis Jan 29 '13 at 05:32
  • 1
    @AbdullahShaikh: The OP can easily modify this code to check for all elements. I am simply offering a helping hand. – Chris Dargis Jan 29 '13 at 05:34
1
for (int i = 0; i < 3; i++)
   for (int j = 0; j < 3; j++)
      if(board[i][j] == true){

         //Code

      }
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
1

Perhaps you are looking for something like this?

import java.util.Arrays;

class Test
{
    public static void main(String[] args)
    {
        boolean[][] board = {{}, {}, {}};
        // all values in board are set to false by default

        boolean[][] board2 = new boolean[0][3];
        boolean[][] board3 = {{true},{true},{true, false, false}};
        boolean[][] board4 = {{}, {true}, {true, true}, {}};

        printInfo(board, 1);
        printInfo(board2, 2);
        printInfo(board3, 3);
        printInfo(board4, 4);


    }

    public static boolean check(boolean[][] board)
    {
        if(board.length == 0) return false;

        int colLength = 0;
        int elementsInBoard = 0;    

        for (int i = 0; i < board.length; i++)
        {
            colLength = board[i].length;
            elementsInBoard += colLength;
            for(int j = 0; j < colLength; j++)
            {
                if (board[i][j] == false)
                {
                    return false;
                }
            }
        }

        if (elementsInBoard == 0)
        {
            return false;
        }else
        {
            return true;
        }
    }

    public static void printInfo(boolean[][] board, int id)
    {
        System.out.println("Board : " + id);
        System.out.println(Arrays.deepToString(board)); 
        System.out.println(check(board));       
    }
}

Output:

Board : 1
[[], [], []]
false
Board : 2
[]
false
Board : 3
[[true], [true], [true, false, false]]
false
Board : 4
[[], [true], [true, true], []]
true
Akavall
  • 82,592
  • 51
  • 207
  • 251
  • 3
    What if boolean[][] board = new boolean[0][3]; – Achintya Jha Jan 29 '13 at 05:24
  • @AchintyaJha, you are right of course, it was just my lazy implementation assuming that the board was square or rectangle. – Akavall Jan 29 '13 at 05:52
  • 1
    @AchintyaJha, You removed your last comment, and I thought that your point was totally valid. Did I miss anything? – Akavall Jan 29 '13 at 05:56
  • I removed my last comment because I just wrote it and after that I start thinking it will work or not. Anyway I think getting number of columns in each row if given 2d array is not square or rectangle should work fine. – Achintya Jha Jan 29 '13 at 06:09
  • @AchintyaJha, yes that works fine. But I also can't check to see if all cell are empty the way I used to `board.length * board[0].length`. I think I addressed this too. – Akavall Jan 29 '13 at 06:46
0

You declared your variable board as a 2D array of boolean and you also initialize it. static boolean[][] board = new boolean[3][3];

Now here, Game.board = true, you are trying to assign your 2D boolean array variable to a boolean value that is incorrect.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

A) Code like this is a bug:

if (someBoolean = true)

You have just silently set the boolean variable instead of testing it. You want:

if (someBoolean == true)

or better yet:

if (someBoolean)


B) A boolean[][] (an array of array of boolean) is not a boolean. Try this:

if (Game.board[x][y])
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

A couple issues with your code. First, you can't directly compare a boolean[][] with true. You'll need to use a loop. Second, you have to place code statements inside a method. You can't use things like if statements without a method. Here's an example of what you want:

public class Game {
    private boolean[] board = new boolean[3][3];

    public static void main(String[] args) {
        boolean allTrue = true;
        for (int i = 0; i < board.length; i++) {
            boolean[] row = board[i];
            for (int j = 0; j < row.length; j++) {
                allTrue &= row[j];
            }
        }
        System.out.println(allTrue);
    }
}

This ignores some short circuit logic you could do, but hopefully you get the point.

One last thing, a single = is assignment, not comparison. Use == for comparison, such as myInt == 1.

Brian
  • 17,079
  • 6
  • 43
  • 66