0

I am developing game, Initially I was using boolean while decalring arrays, latter it revealed that instead of using boolean I should use int in order to store state of game, When I replaced boolean with int my if statement shows type mismatch exception and The operator && is undefined for the argument type(s) boolean, int. Here is my if statement code.

int [][] dots

    protected void onDraw(Canvas canvas) 
    {
        super.onDraw(canvas);
        canvas.drawPaint(pBack);

        for (int y = 0; y < numRows; y++)
        {
            canvas.drawLine(xStep, yCoords[y], numColumns * xStep, yCoords[y], pDot);

            for (int x = 0; x < numColumns; x++)
            {
                if (y == 0)
                {
                    canvas.drawLine(xCoords[x], yStep, xCoords[x], numRows * yStep, pDot);
                }

                if (dots[x][y])
                {
                    boolean left = x > 0 && dots[x - 1][y];
                    boolean up = y > 0 && dots[x][y - 1];

                    if (left)
                    {
                        canvas.drawLine(xCoords[x], yCoords[y], xCoords[x - 1], yCoords[y], pLine);
                    }

                    if (up)
                    {
                        canvas.drawLine(xCoords[x], yCoords[y], xCoords[x], yCoords[y - 1], pLine);
                    }

                    if (left && up && dots[x - 1][y - 1])
                    {
                        canvas.drawCircle(xCoords[x] - xStep / 2, yCoords[y] - yStep / 2, 10, pLine);
                    }
                }
            }
        }
       for (int y = 0; y < numRows; y++)
        {
            for (int x = 0; x < numColumns; x++)
            {
                canvas.drawCircle(xCoords[x], yCoords[y], 20, pDot);                    
                if (dots[x][y])
                {
                    canvas.drawCircle(xCoords[x], yCoords[y], 15, pLine);                       
                }                                       
            }
        }
        if (firstDotX != -1)
        {
            canvas.drawCircle(xCoords[firstDotX], yCoords[firstDotY], 25, pSelect);
        }
    }
Umar Farooq
  • 127
  • 1
  • 2
  • 19
  • Umm. `int` is not `boolean`. You cannot use an integer value like you use a boolean value. – Seelenvirtuose Apr 10 '14 at 06:16
  • I see no `int`s, is this your old or new code? – Tim Apr 10 '14 at 06:16
  • @TimCastelijns I would guess, that the dots variable (or is it a field?) was a `boolean[][]` before, and now it is an `int[][]`. And this - of course - does not work. The error he describes are most likely in the lines, where the boolean variables left and up are declared and initialized. – Seelenvirtuose Apr 10 '14 at 06:17
  • Of course, we can guess. But we don't know, that's why I asked :-) – Tim Apr 10 '14 at 06:19
  • Umar, you're getting the dots and the boxes confused. If you are going to have the user click two dots to select a side, then the `dots` array needs to remain `boolean`. You will need to create another 2-dim array, `int[][] boxes`, that you will use to track which player owns a box. Keep in mind, the `dots` array is really just storing UI info. `boxes` will track game state info. I.e., the game state really just relies on sides drawn and boxes owned. The dots are bells and whistles. – Mike M. Apr 10 '14 at 07:50
  • @MikeM. I want to make sides clickable instead of dots, When user click between two circles that line should become black. – Umar Farooq Apr 10 '14 at 08:01
  • @MikeM. as you know this is two player game so I want to keep track of moves. That which player has drawn line and which player has completed box. For example if first player is assigned a blue and second is assigned red, then in order to show who draw which line? it should display red and blue lines between circles. – Umar Farooq Apr 10 '14 at 08:07
  • 1
    Then I would suggest, for the moment, that you get rid of `dots` completely, and focus on the sides and boxes, and how you want to track their state. Also, you will need to change the `onTouchEvent()` method to find the closest side, instead of the closest dot, but it's an easy modification. If sides are to be "owned" by a player, then you will want to track those similarly to the boxes, i.e. with `int`s. – Mike M. Apr 10 '14 at 08:14
  • @MikeM. What I am going to do is, I will keep dots as boolean? , and keep track of onTouchEvent() on lines. Is it the way? – Umar Farooq Apr 10 '14 at 08:42

3 Answers3

0

That is because you cannot use the AND && and OR || operators with integers, so you may want to re-define the condition:

if (left && up && dots[x - 1][y - 1])
                  ------------------
                  this is an integer

I can't give you a "real " fix, because it depends on what you are trying to do. You can try this, but may not work as you expect:

if (left && up)
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

Yes, when left and up are used as int variable then following if condition will give type mismatch exception

if (left && up && dots[x - 1][y - 1])

As the result of left && up will be an integer and then you are performing logical AND between an int and boolean variable, so it will give type mismatch exception.

You should use it as following way -

if (((left && up).equals(intValue) && dots[x - 1][y - 1])

Where intValue is valid out value in your case and now (left && up).equals(intValue) will give a boolean value which can easily use with other boolean value dots[x - 1][y - 1]

See logical operations on int variables -

2 | 1 = 3 and 4 | 1 = 5. 
Bhanu
  • 663
  • 5
  • 13
0

You are trying to use an int type in a conditional statement that will evaluate boolean expressions, leading to a type mismatch. Unlike other languages, in Java 0 does not correspond to false too (and variables of boolean type can only be true or false, not 0 or 1). You will have to set up an expression in the statement that will give out a boolean result.

For example, you can do:

if(dots[x][y] == 0){....}

instead of:

if(dots[x][y]){....}

Now, if you are using a specific number in your dots array that is the unwanted situation to check with, you check with that number instead of 0.

The same rules occur if there are multiple expressions combined with && and/or || operators, in your conditional statements.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
  • I have seen this but I was unable to implement it. http://stackoverflow.com/questions/3793650/convert-boolean-to-int-in-java – Umar Farooq Apr 10 '14 at 06:48
  • You must not convert all booleans to ints. The point is doing the opposite, as conditional statements (if, while, etc) use booleans. The answer I gave was how to use an int to get a boolean result. The opposite will be useless here. – Nick Louloudakis Apr 10 '14 at 06:50