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

  • Home
  • SEARCH
  • 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 8397095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:42:12+00:00 2026-06-09T20:42:12+00:00

I’m trying to replicate the Mastermind game within c# and have hit a hurdle,

  • 0

I’m trying to replicate the Mastermind game within c# and have hit a hurdle, so to speak. The problem I’m facing is at the stage where player 2 guesses which 3 checkboxes are correct from the 6 available. (8 rows for 8 attempts/lives at guessing). The code I have works when player 2 guesses the correct checkboxes, however when the incorrect checkboxes are selected and the “guess” button is clicked nothing happens. I have a second if statement to check this but obviously something must be wrong. The code for the button click event is:

 private void Guess_button_Click(object sender, EventArgs e)
 {
     int boxesChecked = 0;   // Default value

     CheckBox[] checkBoxArray = new CheckBox[] 
         { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6 };

     for (int i = 0; i < checkBoxArray.Length; i++)
     {
         if (checkBoxArray[i].Checked)
             boxesChecked++;
      }

      if (boxesChecked > 3)
          MessageBox.Show("You have checked " + boxesChecked.ToString() + 
              " checkboxes. Only 3 are allowed.");
      else if (boxesChecked < 3)
          MessageBox.Show("You have checked " + boxesChecked.ToString() + 
              " checkboxes. Please choose 3.");

      if (checkBox1.Checked == cb1)
          if (checkBox2.Checked == cb2)
              if (checkBox3.Checked == cb3)
                  if (checkBox4.Checked == cb4)
                      if (checkBox5.Checked == cb5)
                          if (checkBox6.Checked == cb6)
                          {
                              MessageBox.Show("Congratulations, You Win!", 
                                  "Game Won"); 

                              if (MessageBox.Show("Would you like to play again?", 
                                  "Play Again?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                              {
                                  p1input restart = new p1input();
                                  this.Close();   // Close current window
                                  restart.Show(); // Open restart (instance of p1input)
                              }
                              else
                              {
                                  Environment.Exit(0);    // Terminate Application
                              }

    if (checkBox1.Checked != cb1)
        if (checkBox2.Checked != cb2)
            if (checkBox3.Checked != cb3)
                if (checkBox4.Checked != cb4)
                    if (checkBox5.Checked != cb5)
                        if (checkBox6.Checked != cb6)
                        {
                            MessageBox.Show("Unlucky, Guess Again!");
                            checkBox1.Visible = false;
                            checkBox2.Visible = false;
                            checkBox3.Visible = false;
                            checkBox4.Visible = false;
                            checkBox5.Visible = false;
                            checkBox6.Visible = false;
                        }
    }                              
}
  • 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-09T20:42:14+00:00Added an answer on June 9, 2026 at 8:42 pm

    Ok, let’s go. There a few things to review at your code:

    1. How many checkbox are checked?

    Let’s use a little lamba to make that for a little bit prettier:

    boxesChecked = checkBoxArray.Where<CheckBox>(x => x.Checked).Count();
    

    2. If the user doesn’t have checked 3 checkboxes, let’s show the message and leave the method!

    It’s a little bit simplified too, you may wish to change it:

            if (boxesChecked != 3)
            {
                MessageBox.Show(string.Format("You have checked {0} checkboxes. Please choose 3.", boxesChecked));
                return;
            }
    

    3. Verify the result

    Let’s change those if a little bit. Notice the main else condition (player lost!):

            if (checkBox1.Checked == cb1
                && checkBox2.Checked == cb2
                && checkBox3.Checked == cb3
                && checkBox4.Checked == cb4
                && checkBox5.Checked == cb5
                && checkBox6.Checked == cb6)
            {
                MessageBox.Show("Congratulations, You Win!", "Game Won"); // Display MessageBox
    
                if (MessageBox.Show("Would you like to play again?", "Play Again?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    p1input restart = new p1input();
                    this.Close();   // Close current window
                    restart.Show(); // Open restart (instance of p1input)
                }
                else
                {
                    Environment.Exit(0);    // Terminate Application
                }
            }
            else
            {
                MessageBox.Show("Unlucky, Guess Again!");
                checkBox1.Visible = false;
                checkBox2.Visible = false;
                checkBox3.Visible = false;
                checkBox4.Visible = false;
                checkBox5.Visible = false;
                checkBox6.Visible = false;
            }
    

    Please note that I’m not saying that this is the best design for a game, I’m just pointing out a few things to change on your code.


    UPDATE

    Based on spender’s comment, let’s review your method. Please, check it out:

        private void Guess_button_Click(object sender, EventArgs e)
        {
            int boxesChecked = 0;   // Default value
    
            List<CheckBox> AllTheCheckBoxes = new List<CheckBox> { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6 };
    
            boxesChecked = AllTheCheckBoxes.Where<CheckBox>(x => x.Checked).Count();
    
            if (boxesChecked != 3)
            {
                MessageBox.Show(string.Format("You have checked {0} checkboxes. Please choose 3.", boxesChecked));
                return;
            }
    
            if (AllTheCheckBoxes.Any<CheckBox>(x => x.Checked != Convert.ToBoolean(x.Tag)))
            {
                MessageBox.Show("Unlucky, Guess Again!");
    
                AllTheCheckBoxes.ForEach(x => x.Visible = false);
    
                return;
            }
    
            MessageBox.Show("Congratulations, You Win!", "Game Won"); // Display MessageBox
    
            if (MessageBox.Show("Would you like to play again?", "Play Again?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                p1input restart = new p1input();
                this.Close();   // Close current window
                restart.Show(); // Open restart (instance of p1input)
            }
            else
            {
                Environment.Exit(0);    // Terminate Application
            }
        }
    

    Notice that I’m using the Tag property. It’s an arbitrary string, that developer may use for any purpose. Here I’m expecting that the correct value (true or false) is stored at this property.


    UPDATE 2

    Regarding OP comment about finding all the checkboxes (looks like its 48 total).
    You can use the following statement (understand it and improve it to your needs).

    List<CheckBox> AllTheCheckBoxes = this.Controls.AsQueryable().OfType<CheckBox>().Where(x => x.Tag != null).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.