'I'm making a guessing game but the random word is always the same

I have 100 random words in a notepad file & choosing a random word but it's always choosing the same word which is the last one in the file every word is a line.

Here is the code for taking a random word & showing it as:

private string[] tab;
private string current = "";
private string copycurrent = "";

private void randomword()
{
    string line = "";

    try
    {
        using (StreamReader sr = new StreamReader("Words.txt"))
        {
            string[] tab = null;

            while ((line = sr.ReadLine()) != null)
            {
                tab = line.Split("\n");
            }

            Random r = new Random();

            int j = r.Next(tab.Length);
            current = tab[j];
            copycurrent = "";

            for (int i = 0; i < current.Length; i++)
            {
                copycurrent += "_";
            }

            label1.Text = "";

            for (int i = 0; i < copycurrent.Length; i++)
            {
                label1.Text += copycurrent.Substring(i, 1);
                label1.Text += " ";
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

Please - can anyone help? I would really appreciate it! Thanks in advance.

I don't know why it doesn't work like I've been quite wondering for some time. BTW I'm making a hangman game if anyone want the project hmu ig: rami_chalouhi. its like an actual game with login and loading etc..



Solution 1:[1]

You keep overwriting 'tab'. you only have the last line

            string[] tab = null;

            while ((line = sr.ReadLine()) != null)
            {
                tab = line.Split("\n");

            }

Better would be a list of words

       var words = new List<string>();


            while ((line = sr.ReadLine()) != null)
            {
                var tab = line.Split("\n");
                words.AddRange(tab);

            }

in fact that split doesnt make any sense, ReadLine will already be splitting on new line (my first scan I assumed you were splitting on a needed delimter like space or ',') So all that code can be replaced by

  var words = File.ReadAllLines("Words.txt");

see https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-6.0


The rest of that code is pretty odd too

            current = tab[j];
            copycurrent = "";
            for (int i = 0; i < current.Length; i++)
            {
                copycurrent += "_";
            }
            label1.Text = "";

            for (int i = 0; i < copycurrent.Length; i++)
            {
                label1.Text += copycurrent.Substring(i, 1);
                label1.Text += " ";
            }

you want

  • a string of "_" thats the same length as the selected word

  • to fill label1.Text with that _ string plus an extra space

             current = tab[j];
             var copycurrent = new string('_', current.Length);
             label1.Text = copycurrent + " ";
    

is a lot simpler and clearer

So that whole function becomes

    string current;
    string copycurrent;
    try
    {
          var words = File.ReadAllLines("Words.txt");
          
          Random r = new Random();
          int j = r.Next(words.Length);
          current = words[j];

          copycurrent = new string('_', current.Length);
          label1.Text = copycurrent + " ";

        
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);

    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1