0

I have this slight problem with my Tic Tac Toe program. I have a nested counter checking for vertical and horizontal wins with the X piece (I haven't done it for O yet). For some reason, it never ends, allowing me to place more and more pieces.

For some context, here are some snippets of code.

public static void game (String winner, Board pointer)
{ 
    boolean win = false;
    int turnCount = 1;
    winner = "Tie";
    while (win == false || turnCount < 9)
    {
        int row = Integer.parseInt(JOptionPane.showInputDialog(pointer.getName1() + ", it is your turn. " + "\n" + "What row would you like your 'x' to be in?"));
        int col = Integer.parseInt(JOptionPane.showInputDialog("What column would you like it to be in?"));
        pointer.play[row] [col] = pointer.x;
        SpotCheck1(winner, pointer, row, col);
        System.out.println(BoardStat(pointer));
        winCheck(win, pointer, winner);
        int row2 = Integer.parseInt(JOptionPane.showInputDialog(pointer.getName2() + ", it is your turn. " + "\n" + "What row would you like your 'o' to be in?")); 
        int col2 = Integer.parseInt(JOptionPane.showInputDialog("What column would you like it to be in?"));
        SpotCheck2(winner, pointer, row2, col2);
        pointer.play[row2] [col2] = pointer.o;
        System.out.println(BoardStat(pointer));
        winCheck(win, pointer, winner);
        turnCount++;
    }
    //return winner;
}

public static void winCheck (boolean win, Board pointer, String winner)
{
    for (int counter = 1; counter <= 3; counter++)
    {
        if ( pointer.play [counter] [1].compareTo(pointer.x) > 0 && pointer.play [counter] [2].compareTo(pointer.x) > 0 && pointer.play [counter] [3].compareTo(pointer.x) > 0)
        {
            win = true;
            winner = pointer.getName1();
            win(winner, pointer);
        }          
        else if (pointer.play [1] [counter].compareTo(pointer.x) > 0 && pointer.play [2] [counter].compareTo(pointer.x) > 0 && pointer.play [3] [counter].compareTo(pointer.x) > 0)
        {
            win = true;
            winner = pointer.getName1();
            win(winner, pointer);
        }
    }
    //return win;

}

SpotChecks 1 and 2 are the ways to check if a player has already placed a piece. The problem, I think, is in the WinCheck method. If anybody can help me out, that'd be great! First post, apologies if I did something wrong.

ConnorD
  • 1
  • 1
  • As a tip: move away from ```static``` and move the state you're keeping (outside your while loop) to instance fields. – Jorn Vernee May 26 '16 at 12:54

1 Answers1

1

You're modifying the parameter win in the method winCheck and you are expecting that this changed the local variable win in method game. But that's not the case - Java passes method parameters by value, so the win in winCheck is a copy and any changes do not reflect back. You can make the return type of method winCheck a boolean.

public static boolean winCheck (boolean win, Board pointer, String winner)
{
    // Other code is unchanged

    return win;
}

And in your game method you change any invocation of the method winCheck to:

win = winCheck(...);
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79