0

Hello I am trying to make a tic tac toe program, and I am having trouble with having the turns ending, and a winner being declared, so could someone please take a look at my method and see what might be the problem. So far diagonals work, but i don't think rows, and columns do thanks.

public String checkWin()

{
    String results = "";
    for (int r =0; r <3;r++)
    {
        if(board[0][0]== "1" &(board[1][1]== "1") &(board[2][2]== "1"))
        {
            results = "Player 1 wins";
        }
        else if(board[0][0]== "2" &(board[1][1]== "2" )&(board[2][2]== "2"))
        {
            results = "Player 2 wins";
        }
        else if(board[0][2]== "1" &(board[1][1]== "1") &(board[2][0]== "1"))
        {
            results = "Player 1 wins";
        }   
        else if(board[0][2]== "2" &(board[1][1]== "2") &(board[2][0]== "2"))
        {
            results = "Player 2 wins";
        }
    // else if(board[0][0] == "1" & board [0][1] == "1" & board [0][2] =="1"
    // {
    //  results = "Player 1 wins"
    //  }
        else if (board [r][0] == "1"& board [r][1]=="1" & board[r][2]=="1")
        {
            results = "Player 1 wins";
        }
        else if(board [r][0] == "2"& board [r][1]=="1" & board[r][2]=="2")
        {
            results = "Player 2 wins";
        }
        else if (board [0][r] == "1"& board [1][r]=="1" & board[2][r]=="1")
        {
            results = "Player 1 wins";
        }
        else if (board [0][r] == "2"& board [1][r]=="2" & board[2][r]=="2")
        {
            results = "Player 2 wins";
        }
        else
        {
            results = "Tie";

        }
        return results;
    }
}
Naman
  • 27,789
  • 26
  • 218
  • 353
Javanoob
  • 1
  • 3
  • Is there anything you've done to try to solve the problem? What? Also, for boolean `and`, you have to use `&&`. `&` means binary `and`. And you can't use `==` to check for equality with `String`s, assuming you're using Java. Use `String.equals()` instead. – ddsnowboard Jan 21 '16 at 00:45
  • ok thanks so change & to && and == to .equals() ? – Javanoob Jan 21 '16 at 00:55
  • Yes, so it would be `board[0][0].equals("1")` and so on. I'm not sure that those are your only problems, but I am certain that nothing else will work until you fix those things. – ddsnowboard Jan 21 '16 at 00:56
  • then why does the diagonal work? – Javanoob Jan 21 '16 at 00:57
  • See [this](http://stackoverflow.com/a/1724263/2570117) post. Single `&` can be used with boolean expressions, but it does something slightly different than normal and will confuse people (like me, for example). The `==` thing works sometimes because Java is going to try to be nice to you and make it work, but sometimes it won't be able to help you and everything will come crashing down. See [this](http://stackoverflow.com/a/513839/2570117) post. – ddsnowboard Jan 21 '16 at 01:03
  • ok thanks so i should change it to .equals() – Javanoob Jan 21 '16 at 01:33

0 Answers0