-1

I want to create a program which would show the flag in a picture box and a user would have to guess what flag it is. I have a problem that flags are repeating and I totally don't know how to fix it.

Here is a bit of my code:

private void button1_Click(object sender, EventArgs e)
{
    Random random = new Random();
    randomized = random.Next(0, 9);
    //0 = Poland
    //1 = France
    //2 = Sweden
    //3 = Germany
    //4 = Portugal
    //5 = Spain
    //6 = Finland
    //7 = Norway
    //8 = Russia
    //9 = Ukraine
    
    if (randomized == 0)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Poland.png");
    }

    if (randomized == 1)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/France.png");
    }

    if (randomized == 2)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Sweden.jpg");
    }

    if (randomized == 3)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Germany.png");
    }

    if (randomized == 4)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Portugal.png");
    }

    if (randomized == 5)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Spain.png");
    }

    if (randomized == 6)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Finland.png");
    }

    if (randomized == 7)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Norway.png");
    }

    if (randomized == 8)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Russia.png");
    }

    if (randomized == 9)
    {
        ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Ukraine.png");
    }
}
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • Maybe accepted answer of https://stackoverflow.com/questions/2019417/how-to-access-random-item-in-list could help you. – M. Mennan Kara Oct 31 '21 at 10:31
  • Have you tried making `Random random = new Random();` a "global" variable? If the button is clicked in quick successions then there is a good chance the random numbers will be identical. Making `random` global should fix this. – JohnG Oct 31 '21 at 10:32
  • have you tried putting generated number in a list? – fmansour Oct 31 '21 at 10:37
  • Do you mean if say Germany was ever displayed to the user, then subsequent clicks should not consider Germany when picking the next country? – Jeppe Stig Nielsen Oct 31 '21 at 10:39
  • Check this out: https://stackoverflow.com/questions/7251714/c-sharp-random-numbers-arent-being-random – Yom B Oct 31 '21 at 10:41
  • 1
    Try the following: create a variable List in your form and fill it with all the filenames of the flags you have. Move your variable `Random random` near your list, out from `button1_Click` and initialize it only once. Then in `button1_Click` generate a random number from zero to the count of the list - 1. Pick that filename from your list and remove it from the list afterwards. If the list doesn't have any more elements, you are done, disable the `button1` and show a message saying no more countries to guess. – Steeeve Oct 31 '21 at 10:43
  • Just use two `HashSet` collections to store seen and unseen flags. When user clicks button, invoking `button1_Click`, select a random index from the unseen flags between 0 and the length of the unseen flags collection. Then remove that flag from the unseen collection and add it to the seen collection. To reset, simply add all flags from the seen collection to the unseen collection and clear the seen collection. This is not very efficient, but efficiency is not really a issue here. – Thomas Angeland Oct 31 '21 at 11:59

1 Answers1

2

If you want to show a different image every time, you may need to keep track of which images you've already shown.

private readonly Random _random = new();
private const string PathPrefix = "C:/Users/Kacper/Desktop/FlagiDoGeo/";
private static List<string> _flags = Reinitialize();
       
private void button1_Click(object sender, EventArgs e)
{
    var randomized = _random.Next(0, _flags.Count);
    var image = _flags[randomized];

    // delete the image that will be displayed.
    _flags.Remove(image);

    // if all images are displayed, the game restarts from the beginning.
    if (_flags.Count == 0)
    {
        _flags = Reinitialize();
    }
            
    ptbFlag.Image = PathPrefix + image;
}

private static List<string> Reinitialize()
{
    return new List<string>
        {
            "Poland.png",
            "France.png",
            "Sweden.jpg",
            "Germany.png",
            // ...
        };
}
momo
  • 3,313
  • 2
  • 19
  • 37