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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:28:16+00:00 2026-06-17T23:28:16+00:00

I have spent hours making a tic tac toe game just using mainly if

  • 0

I have spent hours making a tic tac toe game just using mainly if else statements, some of the code I just copy and pasted…

How would I just create one instance of the code, then refer to it later on? – when I need that code, instead of just copy and pasting massive lines of code each time. In this block of code, I am getting a random number between 1 to 10 this number will be stored in RI… then when it’s Ai’s turn the computer will randomly input an X in the button, then using this code it will check to determine if the computer has won. I hope that make sense =)

do
{
    storeRI = rc.Next(0, 10); //storing random number into storeRI so it can be used later on in life. 
    if (storeRI == 1 && btn1.Text == "")
    {
        btn1.Text = "X";
        Turn = 1;
        if (btn1.Text == "X" & btn2.Text == "X" & btn3.Text == "X")
        {
            btn1.BackColor = Color.Green;
            btn2.BackColor = Color.Green;
            btn3.BackColor = Color.Green;

            XScore += 1;
            lblPScoreX.Text = XScore.ToString();

            foreach (Button btn in buttonList)
                 btn.Enabled = false;
        }
        else if (btn1.Text == "X" & btn4.Text == "X" & btn7.Text == "X")
        {
             btn1.BackColor = Color.Green;
             btn4.BackColor = Color.Green;
             btn7.BackColor = Color.Green;

             XScore += 1;
             lblPScoreX.Text = XScore.ToString();

             foreach (Button btn in buttonList)
                btn.Enabled = false;
        }
    }
}

for example this code, is some of the code, that I have copied atleast 10 times, and it just makes me code look ugly and really hard to read.

  • 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-17T23:28:17+00:00Added an answer on June 17, 2026 at 11:28 pm

    for example this code, is some of the code, that I have copied atleast 10 times

    Any time you have copied code, you should try to move it into a method. You can then just call the method directly. In your case, a method could easily accept the three buttons to affect, and work on them directly.

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

Sidebar

Related Questions

I have spent hours trying to get my project working and I just can't
I have spent hours in this problem and my fellows couldn't help me out.
I'm sorry to ask this question but I have spent hours trying to understand
I hope this is a relatively easy problem although I have spent hours websearching
I have a really frustrating error that I've spent hours looking at and cannot
I have a problem with php header redirect. I already spent hours trying to
To begin, Ive spent several hours looking for an answer and have come CLOSE
I spent around two hours on that problem, and I have visited these stackoverflow
I have spent hours looking how to solve this. When I try to get
I am new to iPhone and objective c. I have spent hours and hours

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.