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 7169567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:01:18+00:00 2026-05-28T15:01:18+00:00

Essentially, when a button is pressed the randomMove() method is called, this is mean’t

  • 0

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();
    }
  • 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-05-28T15:01:19+00:00Added an answer on May 28, 2026 at 3:01 pm

    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();
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Essentially I have a method of a class called killProgram, which is intended to
I've heard that pressing the back button will essentially cause the current Activity to
I've added a button that I want to essentially toggle a label from Hello
I'm working on an Android widget which essentially places a button on the homescreen.
I'll start by saying I am very much a beginner programmer, this is essentially
I have a button class that I wrote in CSS. It essentially displays block,
This is essentially my table inside of a loop: <table id=tblMyTable> <tr> <td><%= attachment.Name
I have a button and, when pressed, it downloads several images from the web
So essentially I want the UILabel to increment every time a button is clicked.
Inside a datagrid, I essentially want to create a download button itemrenderer to the

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.