0

In Tic Tac Toe, Program works for horizontal, and vertical, but it does not work for diagonal. I think that private bool checkWin part is wrong.

private bool checkWin()
{
    for (int row=0; row<3; row++)
    {
        if (values[row,0] != ' ' && values[row,0]==values[row,1]&&values[row,0]==values[row, 2])
        {
            lockButton(false);
            return true;
        }
    }
    for (int col = 0; col < 3; col++)
    {
        if (values[0, col] != ' ' && values[0, col] == values[1, col] && values[0, col] == values[2, col])
        {
            lockButton(false);//asdfasdfsdafadsfasdfasdfasdf
            return true;
        }
    }
    return false;
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Because you aren't checking the diagonals. You have a loop to check the 3 rows, then a loop to check the 3 columns but nothing for the diagonals. Since there are only 2 you can probably just slap them in hardcoded. – Jack T. Spades May 06 '21 at 09:11
  • 3
    Does this answer your question? [Algorithm for Determining Tic Tac Toe Game Over](https://stackoverflow.com/questions/1056316/algorithm-for-determining-tic-tac-toe-game-over) – Self May 06 '21 at 09:25

1 Answers1

1

Maybe you can try to add something like this betwen the second for and the return false:

if (values[0, 0] != ' ' && values[0, 0] == values[1, 1] && values[0, 0] == values[2, 2])
{
    lockButton(false);
    return true;
}else if(values[0, 2] != ' ' && values[0, 2] == values[1, 1] && values[0, 2] == values[2, 0])
{
    lockButton(false);
    return true;
}

The fist one is for diagonal for 0,0 to 2,2 and the second one for 0,2 to 2,0