Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9324771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:40:55+00:00 2026-06-19T04:40:55+00:00

My first code executes and after I make my move, the computer always tries

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:40:57+00:00Added an answer on June 19, 2026 at 4:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following thread code which executes correct first time. After that from
I've developed some code that executes a quartz job.At first the code was outside
Here is my code, but it doesn't make replace! First I change AnyText to
I was advised to use this code to make my method execute after a
First, an example of something that works as expected: (all code was executed in
In my code a function execute when user mouseover on a div for first
First code : public static int pitagoras(int a, int b) { return (int) Math.sqrt(a*a
When I run the first code and press ctrl-c immediately there will be no
Why does the first code print DCBA and the second ABCD? Is 0x44434241 not
I am trying to write some of my first code in emacs lisp and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.