0
\$\begingroup\$

I have written a code in C# that checks whether the user have won the TicTacToe game. I feel that maybe there is a more elegant way to check it, but I couldn't find one. Would like your help.


public static bool CheckWinnings(string[,] gameboardsquares)
{
    bool iscontinue = true;

    //rows;
    if (gameboardsquares[0,0] == gameboardsquares[0,1] && gameboardsquares[0,1] == gameboardsquares[0,2] && gameboardsquares[0,0] != " ")
    {
        iscontinue = false;
    }
    else if (gameboardsquares[1,0] == gameboardsquares[1,1] && gameboardsquares[1,1] == gameboardsquares[1,2] && gameboardsquares[1,0] != " ")
    {
        iscontinue = false;
    }
    else if (gameboardsquares[2,0] == gameboardsquares[2,1] && gameboardsquares[2,1] == gameboardsquares[2,2] && gameboardsquares[2,0] != " ")
    {
        iscontinue = false;
    }

    //columns;
    else if (gameboardsquares[0,0] == gameboardsquares[1,0] && gameboardsquares[1,0] == gameboardsquares[2,0] && gameboardsquares[0,0] != " ")
    {
        iscontinue = false;
    }
    else if (gameboardsquares[0,1] == gameboardsquares[1,1] && gameboardsquares[1,1] == gameboardsquares[2,1] && gameboardsquares[0,1] != " ")
    {
        iscontinue = false;
    }
    else if (gameboardsquares[0,2] == gameboardsquares[1,2] && gameboardsquares[1,2] == gameboardsquares[1,2] && gameboardsquares[0,2] != " ")
    {
        iscontinue = false;
    }

    //diagonals;
    else if (gameboardsquares[0,0] == gameboardsquares[1,1] && gameboardsquares[1,1] == gameboardsquares[2,2] && gameboardsquares[0,0] != " ")
    {
        iscontinue = false;
    }
    else if (gameboardsquares[0,2] == gameboardsquares[1,1] && gameboardsquares[1,1] == gameboardsquares[2,0] && gameboardsquares[0,2] != " ")
    {
        iscontinue = false;
    }

    return iscontinue;
}
\$\endgroup\$
  • \$\begingroup\$ (nested loops & index arithmetic - please to not present cut&dried solutions in answers without spoil retarder (>!)) \$\endgroup\$ – greybeard May 4 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.