0

I have a multi class program in which I'm trying to make winning conditions for. However when I attempt to compare the 2D array that contains an object, with another object of the same kind.

Here is a sample, I've tested the bare bones and the method does work, it's the if statements that do not, any help is very much appreciated.

(Yes I converted them to strings with the quotations, however the statement doesn't work either way)

public boolean winner()//Checks to see if someone has won.
{
    XO x = new XO("X");//creates the objects to mimic the players
    XO o = new XO("O");
    if((board [0][0]+"").equals(o+"") && (board [0][1]+"").equals(o+"") && (board [0][2]+"").equals(o+""))//top horizontal row
    {
        return true;
    }
    else if((board [0][1]+"").equals(o+"") && (board [1][1]+"").equals(o+"") && (board [2][1]+"").equals(o+""))//mid horizontal row
    {
        return true;
    }
    else if((board [0][2]+"").equals(o+"") && (board [1][2]+"").equals(o+"") && (board [2][2]+"").equals(o+""))//bottom horizontal row
    {
        return true;
    }
    else if((board [0][0]+"").equals(o+"") && (board [0][1]+"").equals(o+"") && (board [0][2]+"").equals(o+""))//left vertical row
    {
        return true;
    }
    else if((board [1][0]+"").equals(o+"") && (board [1][1]+"").equals(o+"") && (board [1][2]+"").equals(o+""))//mid vertical row
    {
        return true;
    }
    else if((board [2][0]+"").equals(o+"") && (board [2][1]+"").equals(o+"") && (board [2][2]+"").equals(o+""))//right vertical row
    {
        return true;
    }
    else if((board [0][0]+"").equals(o+"") && (board [1][1]+"").equals(o+"") && (board [2][2]+"").equals(o+""))//declining diagonal row
    {
        return true;
    }
    else if((board [2][0]+"").equals(o+"") && (board [1][1]+"").equals(o+"") && (board [0][2]+"").equals(o+""))//rising diagonal row
    {
        return true;//                                                                    respectively the same for the lines below
    }
    //Above O wins, Below X wins
    else if((board [0][0]+"").equals(x+"") && (board [0][1]+"").equals(x+"") && (board [0][2]+"").equals(x+""))//top horizontal row
    {
        return true;
    }
    else if((board [0][1]+"").equals(x+"") && (board [1][1]+"").equals(x+"") && (board [2][1]+"").equals(x+""))//mid horizontal row
    {
        return true;
    }
    else if((board [0][2]+"").equals(x+"") && (board [1][2]+"").equals(x+"") && (board [2][2]+"").equals(x+""))//bottom horizontal row
    {
        return true;
    }
    else if((board [0][0]+"").equals(x+"") && (board [0][1]+"").equals(x+"") && (board [0][2]+"").equals(x+""))//left vertical row
    {
        return true;
    }
    else if((board [1][0]+"").equals(x+"") && (board [1][1]+"").equals(x+"") && (board [1][2]+"").equals(x+""))//mid vertical row
    {
        return true;
    }
    else if((board [2][0]+"").equals(x+"") && (board [2][1]+"").equals(x+"") && (board [2][2]+"").equals(x+""))//right vertical row
    {
        return true;
    }
    else if((board [0][0]+"").equals(x+"") && (board [1][1]+"").equals(x+"") && (board [2][2]+"").equals(x+""))//declining diagonal row
    {
        return true;
    }
    else if((board [2][0]+"").equals(x+"") && (board [1][1]+"").equals(x+"") && (board [0][2]+"").equals(x+""))//rising diagonal row
    {
        return true;//                                                                    respectively the same for the lines below
    }
    else//if none of these are fulfilled
        return false;}
  • 1
    You need to compare Strings with `.equals()`. – LordAnomander Feb 22 '16 at 13:47
  • Btw, what's the datatype of `board`? Why don't you just compare the element value and the two objects (ideally the element values would just be references/copies of the x and o objects anyways)? – Thomas Feb 22 '16 at 13:49
  • Datatype of "board" is an XO object, which returns a string. – BuySomeChips Feb 22 '16 at 13:51
  • Another tip: I'd try to improve the checks for winning conditions if this is a learning excercise. In practice using that many if-conditions is error-prone and overkill for any reasonable complexity (think of a 4x4 or 5x5 board where it'll get way too much to check, not considering larger boards). – Thomas Feb 22 '16 at 13:52
  • In that case, don't compare the strings (you don't do it right anyways) but implement `hashCode()` and `equals()` and then use sth. like `board[0][0].equals(x)` etc. – Thomas Feb 22 '16 at 13:53
  • Ahh, I have it, I'll update in the edit. Thanks. I just was so fed up with attempting to do all of it I forgot that I had to use `.equals()` anyway. Thanks all – BuySomeChips Feb 22 '16 at 14:05

0 Answers0