Hello -

The code that I have below works .. but I am sure that there must be a more efficient way of looping through to check for a winner. Any advice much appreciated ... thanks:).

if   ((nandcButton[0,0].Text == "0") && (nandcButton[0,1].Text == "0") && (nandcButton[0,2].Text == "0") || (nandcButton[1,0].Text == "0") && (nandcButton[1,1].Text == "0") && (nandcButton[1, 2].Text == "0") || (nandcButton[2, 0].Text == "0") && (nandcButton[2, 1].Text == "0") && (nandcButton[2, 2].Text == "0")
               || (nandcButton[0,0].Text == "0") && (nandcButton[1,0].Text == "0") && (nandcButton[2,0].Text == "0") || (nandcButton[0,1].Text == "0") && (nandcButton[1,1].Text == "0") && (nandcButton[2, 1].Text == "0") || (nandcButton[0, 2].Text == "0") && (nandcButton[1, 2].Text == "0") && (nandcButton[2, 2].Text == "0")
               || (nandcButton[0,0].Text == "0") && (nandcButton[1,1].Text == "0") && (nandcButton[2,2].Text == "0") || (nandcButton[2,0].Text == "0") && (nandcButton[1,1].Text == "0") && (nandcButton[0, 2].Text == "0"))
            //try
            {
                Form mynewForm = new winnerForm(p1NameTextBox.Text, p2NameTextBox.Text);//show if NAUGHTS wins - pass winner and score
                mynewForm.Show();
                
                pOneScore = pOneScore + 1;
                
                string play1name = (p1NameTextBox.Text);

                   }

Recommended Answers

All 2 Replies

Hello,
I think this may work.
you can use 2 nested for loops and a flag, something lik that.

bool flag=false
for(int i;i<length;i++)
{
     for(int j;j<_length;j++)
     {
        if(nandcButton[1,0].Text == "0")
          flag=true;
     }
}
commented: Thanks for the help +1

I dont find anything that can be connected with if,else loops in your code, ok it could be, but it would be too complicated.
In my opinion fawi`s approach is the right one, I will only modify it a bit, to suit your needs:

bool flag = false;
            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    if (nandcButton[i, j] == "0")
                        flag = true;
                }
            }
            if (flag == true)
            {
                //the requirment has been fulfiled!
            }

Hope this helps,
Mitja

commented: Thanks for the help +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.