Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
Yes, you're right! Thank you for bringing me in the right direction, this helped me alot! –  Kimmax Dec 21 '13 at 21:57
add comment

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

share|improve this answer
    
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! –  Kimmax Dec 22 '13 at 1:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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