5
\$\begingroup\$

I'm currently trying to improve my coding skills, so I'm trying to code a Tic Tac Toe game. At one point a "win check" is needed.

I'm using this code to check:

public void CheckWin(States state)
{   
    if(fieldA1.GetState == state && fieldA2.GetState == state && fieldA3.GetState == state)
        ReportWin(currentPlayer);
    else if(fieldB1.GetState == state && fieldB2.GetState == state && fieldB3.GetState == state)
        ReportWin(currentPlayer);
    else if (fieldC1.GetState == state && fieldC2.GetState == state && fieldC3.GetState == state)
        ReportWin(currentPlayer);
    else if (fieldA1.GetState == state && fieldB1.GetState == state && fieldC1.GetState == state)
        ReportWin(currentPlayer);
    else if (fieldA2.GetState == state && fieldB2.GetState == state && fieldC2.GetState == state)
        ReportWin(currentPlayer);
    else if (fieldA2.GetState == state && fieldB2.GetState == state && fieldC2.GetState == state)
        ReportWin(currentPlayer);
    else if (fieldA3.GetState == state && fieldB3.GetState == state && fieldC3.GetState == state)
        ReportWin(currentPlayer);
    else if (fieldA1.GetState == state && fieldB2.GetState == state && fieldC3.GetState == state)
        ReportWin(currentPlayer);
    else if (fieldA3.GetState == state && fieldB2.GetState == state && fieldC1.GetState == state)
        ReportWin(currentPlayer);
    else
        PlayerSwitch();
}

This code looks very unclean to me. Is there a better way to handle these if instructions?

\$\endgroup\$

2 Answers 2

8
\$\begingroup\$

There should only be eight ways to win; you have nine ReportWin()s. This one got listed twice:

fieldA2.GetState == state && fieldB2.GetState == state && fieldC2.GetState == state

Considering that you've named your squares as independent variables, there's not much you can do to generalize CheckWin(). Start by turning the board into an array or two-dimensional array, then read the advice in Tic-Tac-Toe design . Personally, I like the suggestion to list all eight winning configurations, as it results in the least code.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Yes, you're right! Thank you for bringing me in the right direction, this helped me alot! \$\endgroup\$
    – Kimmax
    Dec 21, 2013 at 21:57
1
\$\begingroup\$

I am too lazy to program it, but try programming tic tac toe instead of being in the "bruteforce" way, try making the winner the one who has at least 3 picks and the sum of his picks is 15(bad wording, I know). I'm going to save you on my blabbering and look at this:

http://en.wikipedia.org/wiki/Magic_square

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Uhm I never heard of the real way to play this, I only knew the basic "children game". Looks like there is much more behind this game, thank you for showing me this! \$\endgroup\$
    – Kimmax
    Dec 22, 2013 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.