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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:45:44+00:00 2026-05-26T20:45:44+00:00

Sorry if this is a stupid noob question. I’m doing a very small project

  • 0

Sorry if this is a stupid noob question. I’m doing a very small project for my girlfriend – a list of countries and she has to enter their capitals (obscure countries, mind you) . Since I’m a total beginner, I had to resort to using two arrays, one for countries and the other for capitals, with matching indexes. That way it’s easy to check for the right answer and I don’t have to parse any text files or use any data-bases. I’m using random numbers to make it more interesting. To stop the program from generating the same countries over and over again, I’m using a List of integers that keeps tracks of what indexes have already been used and regenerates the number if the list contains the previous one. Pretty basic stuff. Surprisingly, it all works.

But I’m having a problem. How do I check that I’ve run out of countries, basically? 🙂 I can’t simply check the List size against my countries array, since List probably includes more values than the array, and if (taken.Equals(Countries.Length)) doesn’t seem to work. Or I can’t find the right place in the code to put this check.

Sorry if this is simple, but I can’t seem to find a proper solution.

EDIT
Wow, what an amazing community. During the short walk from Starbucks to my place I get dozens of quality answers which cover a huge array of design techniques. This is so great! Thank you everyone! Obviously, the question has been answered but I will post the code for you, if anyone has any additional comments.

// JUST A TEST FOR NOW, 13 COUNTRIES

string[] Countries = {"Belgium", "France", "The Netherlands", "Spain", "Monaco", "Belarus", "Germany",
                             "Portugal", "Ukraine", "Russia", "Sweden", "Denmark", "South Africa"};
        string[] Capitals = {"Brussels", "Paris", "Amsterdam", "Madrid", "Monaco", "Minsk", "Berlin",
                            "Lisbon", "Kiev", "Moscow", "Stockholm", "Copenhagen", "Pretoria"};
        Random number = new Random();
        List<int> taken = new List<int>();
        int index;
        int score = 0;

        private int Generate()
        {

            while (true) {
                index = number.Next(0, Countries.Length);
                if (taken.Contains(index)) continue;
                // THIS IS WHAT I WAS INITIALLY TRYING TO DO
                if (taken.Equals(Countries.Length)) { 
                    MessageBox.Show("Game over!");
                    return -1;


                }
                return index;
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            index = Generate();
            taken.Add(index);
            label1.Text = Countries[index];
            label3.Text = "0 out of " + Countries.Length.ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == Capitals[index].ToString()) {
                label2.Text = "You win!";
                index = Generate();
                taken.Add(index);
                label1.Text = Countries[index];
                textBox1.Clear();
                label3.Text = ++score + " out of " + Countries.Length.ToString();

            }
            else {
                label2.Text = "Wrong!";
                textBox1.Clear();
            }
        }
    }
}
  • 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-26T20:45:45+00:00Added an answer on May 26, 2026 at 8:45 pm

    Instead of using two arrays, or an array and a list, let’s introduce something of C# 4.0 that actually looks and is easy to use and seems to be made for this type of assignments.

    Follow this code with your eyes and specifically look how these “anonymous types” are used in the end. It makes life real easy.

    // initialize your array like so,
    // now you can access your items as countries[1].name and countries[1].city
    // and you will never have to worry about having too much cities or countries
    // PLUS: they're always together!
    var countries =  new [] {
        new { name = "The Netherlands", city = "Amsterdam"},
        new { name = "Andorra",         city = "Vaduz" }, 
        new { name = "Madagascar",      city = "Antananarivo"} 
    };
    
    // randomize by shuffling (see http://stackoverflow.com/questions/375351/most-efficient-way-to-randomly-sort-shuffle-a-list-of-integers-in-c-sharp/375446#375446)
    Random random = new Random();
    for (int i = 0; i < countries.Length; i += 1)
    {
        int swapIndex = random.Next(i, countries.Length);
        if (swapIndex != i)
        {
            var temp = countries[i];
            countries[i] = countries[swapIndex];
            countries[swapIndex] = temp;
        }
    }
    
    // go through all your items in the array using foreach
    // so you don't have to worry about having too much items
    foreach(var item in countries)
    {
         // show your girlfriend the country, something like
         string inputString = DisplayCountry(item.country);
         if(inputString == item.city)
         {
              ShowMessage("we are happy, you guessed right!");
         }
    }
    
    
    // at the end of the foreach-loop you've automatically run out of countries
    DisplayScore(to-your-girlfriend);
    

    Note: you can easily expand on this anonymous types by adding whether or not that particular country/city pair was guessed right and make a subsequent test with the ones she failed.

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

Sidebar

Related Questions

Sorry, this might be a basic/stupid/noob question - I am just trying to tweak
Sorry if this is a stupid noob question please be gentle with me I'm
I'm sorry if this is a stupid question, but I'm very new to Matlab,
Sorry if this is a stupid question... I've developed an application that creates absolute
Sorry if this sounds like a really stupid question, but I need to make
Sorry for stupid questions, I'm doing everything as described in this tutorial: http://www.functionx.com/visualc/howto/calldlgfromdlg.htm I
Sorry this is not a very well defined question, I am thinking about an
Sorry if this is a stupid question. I want to create an HTTP handler
Sorry for maybe this is stupid question, I just want download source code form
Firstly I asked sorry if this is a stupid question. but I have a

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.