Dwelling Place
Dwelling Place

Reputation: 81

C# - Why is the execution of these different codes giving me the same result?

My first code executes and after I make my move, the computer always tries to get the bottom right-hand spot of a Tic Tac Toe board:

private void ComputersTurn()
    {
        Control.ControlCollection coll = this.Controls;
        foreach (Control c in coll)//for each button in form
        {
            if ((c != null) && (c is Button))//if c is a button and c has a value
            {
                if ((c.Name != "btnNewGame") && (c.Name != "btnExit")) // if the button isnt btn new game or exit
                {

                    if (c.Enabled == true) //if a button has an X
                    {
                        c.Text = "O"; //place an O
                        c.Enabled = false; //in a empty button
                        CheckComputerWinner(); //check if it wins
                        return; //return result
                    }//end of if
               }//end of if 2
            }//end of if 1
        }//end of foreach
    }//end of ComputersTurn

The second code, which I got help with...does the same exact thing:

private void ComputersTurn()
    {
        Control.ControlCollection coll = this.Controls;
        foreach (Control c in coll)//for each button in form
        {
            if ((c != null) && (c is Button))//if c is a button and c has a value
            {
                if ((c.Name != "btnNewGame") && (c.Name != "btnExit")) // if the button isnt btn new game or exit
                {
                    gamefield = new Button[] { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9 };
                    int freeCount = gamefield.Count(b => b.Text != "X");

                    int offset = RandomGenerator.GenRand(0 - 8, freeCount - 1);
                    Button target = gamefield.Where(b => b.Text != "X").Skip(offset).FirstOrDefault(); ;
                    if (target != null)//if target has an X
                    {
                        // check it
                        if (c.Enabled == true)
                        {
                            c.Text = "O"; //O will be inside the button
                            c.Enabled = false; //button can no long be used
                            CheckComputerWinner(); //check if it finishes task
                            return;
                        }
                    }
               }
            }
        }
    }//end of ComputersTurn

Random Generator

public static class RandomGenerator
    {
        private static readonly Random _random = new Random();

        public static int GenRand(int x, int y)
        {
            return _random.Next(x, y);
        }
    }

I don't understand why. The second one is aimed for the computer to be random, first one is set up to be predictable. Why are they both doing the same thing?

Upvotes: 1

Views: 99

Answers (1)

Rob Epstein
Rob Epstein

Reputation: 1500

The second solution never uses the target value. It uses the current looped value c. Change all the check it logic to use target instead of c. You can also eliminate the outer loop and two outer if statements all together.

Upvotes: 1

Related Questions