Steffan Caine
Steffan Caine

Reputation: 165

Issues with breaking out of a for loop

Essentially, when a button is pressed the randomMove() method is called, this is mean't to find a empty square in TicTacToe and then assign an "O" to it, the problem is that it then doesn't break out of the loop as at least two squares turn to "O".

I'm not sure where I've gone wrong but I don't doubt it's a simple solution.

 private void randomMove()
    {
        for (int i = 0; i < 3; i++)
        {
            for (int a = 0; a < 3; a++)
            {
                if (Board[a,i] == "")
                {
                    Board[a,i] = "O";
                    Temp = i + a;
                    compMove(Temp);
                    break;
                }  
            }
        }
    }

    private void compMove(int Temp)
    {
        switch (Temp)
        {
            case 0:
                btn1.Text = "O";
                break;
            case 1:
                btn2.Text = "O";
                break;
            case 2:
                btn3.Text = "O";
                break;
            case 3:
                btn4.Text = "O";
                break;
            case 4:
                btn5.Text = "O";
                break;
            case 5:
                btn6.Text = "O";
                break;
            case 6:
                btn7.Text = "O";
                break;
            case 7:
                btn8.Text = "O";
                break;
            case 8:
                btn9.Text = "O";
                break;
        }
        hasWon();
    }

Upvotes: 0

Views: 144

Answers (6)

Felix K.
Felix K.

Reputation: 6281

You can use local variables to break out of the second loop ( with a boolean ). Or you use goto MyLabel; and define the label with MyLabel:;. When only have a simple method, you could also use return.

    Boolean finish = false;
    for (int i = 0; i < 3; i++)
    {
        for (int a = 0; a < 3; a++)
        {
            if (Board[a,i] == "")
            {
                Board[a,i] = "O";
                Temp = i + a;
                compMove(Temp);
                finish = true;
                break;
            }  
        }
        if (finish) break;
    }


    for (int i = 0; i < 3; i++)
    {
        for (int a = 0; a < 3; a++)
        {
            if (Board[a,i] == "")
            {
                Board[a,i] = "O";
                Temp = i + a;
                compMove(Temp);
                goto Finish;
            }  
        }
    }
    Finish: ...

Upvotes: 2

jon
jon

Reputation: 6246

In addition to the other answers, I think your button text is not going to update as you'd hoped using your compMove() function.

i and a can never have a value higher than 2, as defined by your loops.

Therefore, the highest value your compMove() function will ever receive is 4.

Try changing:

Temp = i + a;
compMove(Temp);

To this:

Temp = (i*3) + a;
compMove(Temp);

Upvotes: 1

Tigran
Tigran

Reputation: 62248

Well, if I look on the code provided and, if I correctly understand what you want, the only thing you need here is a return, like this:

private void randomMove()
{
        for (int i = 0; i < 3; i++)
        {
            for (int a = 0; a < 3; a++)
            {
                if (Board[a,i] == "")
                {
                    Board[a,i] = "O";
                    Temp = i + a;
                    compMove(Temp);
                    return;
                }  
            }
        }
 }

And also it's better to change compMove method to something like this, imo (pseudocode)

   private void compMove(int Temp)
   {
      int buttonIndex = Temp + 1;
      Button btn = FindControl("btn" + buttonIndex) as Button;

      if(btn == null) return;

      btn.Text = "O"; 
      hasWon();
   }

Upvotes: 1

user915331
user915331

Reputation:

You forgot to break out from the bigger loop.. alter the code a little to look like this:

private void randomMove()
{
    bool flag = false;
    for (int i = 0; i < 3; i++)
    {
        for (int a = 0; a < 3; a++)
        {
            if (Board[a,i] == "")
            {
                Board[a,i] = "O";
                Temp = i + a;
                compMove(Temp);
                flag = true;
                break;
            }  
        }
        if (flag)
             break;
    }
}

Upvotes: 1

JMcCarty
JMcCarty

Reputation: 759

Your break statement will only escape the first for loop, you would need to use another break or some other mechanism.

Upvotes: 1

Oded
Oded

Reputation: 498934

You need to break out of both loops when an empty spot was located. Your code only breaks out of the nested one.

Here is one way to do so:

private void randomMove()
{
    bool foundEmpty = false;
    for (int i = 0; i < 3; i++)
    {
        for (int a = 0; a < 3; a++)
        {
            if (Board[a,i] == "")
            {
                Board[a,i] = "O";
                Temp = i + a;
                compMove(Temp);
                foundEmpty = true;
                break;
            }  
        }

        if (foundEmpty)
           break;
    }
}

Upvotes: 1

Related Questions