0

I've been stuck on this for a while now and i cant seem to get it to work. I want my program to be able to select a random word from the "Words" array (array contents previously added by the user) and allow the user to then enter another word and see if it matches the randomly selected word selected by the program from the array. If the words match a message will be out put but if not a message will also be output but the system should indicate to the user if any letters that they had entered were in the random String. I know this is quite a bit but I've been stuck for ages haha, thanks! Here is part of the code i have been using, a bit simplified.

private void btnGuess_Click(object sender, EventArgs e) {

        string guess = txtGuess.Text;

        string[] words = new string[6];
        lstWords.Items.Add(txtEnterWord.Text);

        Random rand = new Random();

        for (int i = 0; i < words.Length; i++)
        {
            words[i] = rand.ToString();
        }
        if (String.Equals(guess, rand))
        {
            MessageBox.Show("Congratulations you have won! Your words are a match");
        }
        else
        {
            MessageBox.Show("Sorry but your words are not a match, try again");
        }
    }
JordonG
  • 3
  • 2

3 Answers3

0

you have to

  • initialize your words array with actual words

  • have a method that picks up a random word from words array

like follows:

   private void btnGuess_Click(object sender, EventArgs e)
    {
        string guess = txtGuess.Text;

        string[] words = new string[6] { "word1", "word2", "word3", "word4", "word5", "word6" }; //<--| initialize words array
        lstWords.Items.Add(txtEnterWord.Text); //<--| what is that for?

        string randWord = GetRandomWord(words); //<--| get rabdom word from words array

        if (String.Equals(guess, randWord))
        {
            MessageBox.Show("Congratulations you have won! Your words are a match");
        }
        else
        {
            MessageBox.Show("Sorry but your words are not a match, try again");
        }

    }

    static private string GetRandomWord(string[] words)
    {
        Random rnd = new Random();
        return words[rnd.Next(0, words.Length)].ToString();
    }
user3598756
  • 28,893
  • 4
  • 18
  • 28
  • Here it is probably fine, but in general it's a bad idea to create a new `Random` instance for each random number you need. – Jens Nov 05 '16 at 15:19
  • Hi, Thank you for your response, but the way i need it is so that each time the 6 words are entered by the user. The 6 words will be different each time, that's why i haven't set each word with in the array as a constant. – JordonG Nov 05 '16 at 15:28
  • @JordonG, my code mainly shows how _"...to be able to select a random word from the "Words" array "_ . I initialized `words` with constants merely for testing purposes. But you can initialize it as you need – user3598756 Nov 05 '16 at 15:34
  • @Jens, yup. I went with the creation of a `Random` instance inside `GetRandomWord()` method since its use, as per OP's question, would allow it. But generally speaking I'd instantiate one `Random` instance outside the method and have it passed as an argument – user3598756 Nov 05 '16 at 15:38
  • Thanks for your help again guys, but Ive had a long morning and starting to get stressed out haha brain is mush. When you say initialize it as I need to I deleted the "word1, word 2, etc" bit. the part where you asked what this "lstWords.Items.Add(txtEnterWord.Text);" part was for. that is so that the words entered to the "txtEnterWord" text box will be added to the words array. When i run the program enter the same six words and when I enter the same word again to check if it matches the random word selected i get "Null Reference exception Unhandled". – JordonG Nov 05 '16 at 16:00
  • `lstWords.Items.Add(txtEnterWord.Text);` takes the content of `txtEnterWord` textbox and adds it to `lstWords` listbox: which way should this generate a 6 elements array? – user3598756 Nov 05 '16 at 16:49
0
 Random rand = new Random()

    for (int i = 0; i < words.Length; i++)
    {
        words[i] = rand.ToString();
    }

In this loop you're assigning the output of rand.ToString() to every element in your array. If you looked at the array after the loop every element would then be "System.Random" because calling the ToString method on an object of type Random returns the type of the object, as a string.

When you create a new Random object you're creating an object. An object that can return you random numbers. You're NOT creating a random number.

You're wanting to pick one of the strings out of the array, this is how you can get it out.

string thirdWordFromMyArrayOfWords = words[3];

To get a random number using Random that will be within the range of your words element:

int randomNumberWithinTheRangeOfMyArray = rand.Next(0,words.Length-1)

You need to subtract one because the array has 6 elements (words.Length = 6) but it starts counting at zero 0,1,2,3,4,5 so if you tried to reference words[6] you'll throw an exception.

int randomNumberWithinTheRangeOfMyArray = rand.Next(0,words.Length-1);
string randomWordFromArray = words[randomNumberWithinTheRangeOfMyArray];
 if (String.Equals(guess, randomWordFromArray))

and it can be further condensed

if (String.Equals(guess, words[rand.Next(0,words.Length-1)]))

Something else to consider is that since your calling for a new random number every time the button is clicked you'll get a new random number and therefore a new random word out of the array.

Kreitlow
  • 51
  • 1
  • 4
  • Thanks for your response! I'm going to try it this way which means ill have to take a completely different approach for the whole program. Ill let you know how i get on! – JordonG Nov 05 '16 at 16:27
0
using System;
using System.Collections.Generic;
using System.Linq;

namespace RandomWords
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] words = new[] { "csharp", "Stack", "overflow", "microsoft", "word5", "Coding"};
            Random rnum = new Random();
            string input = null;
            while ( input != "end") {
                int intnum = rnum.Next(0, words.Length);
                Console.WriteLine("Guess a word or enter end to exit");
                input = Console.ReadLine();
              List<string> l =  words.Where(x => x == input).ToList<string>();
              if (l.Count != 0) { Console.WriteLine("Congratulations you have won! Your words are a match"); } else { Console.WriteLine("Sorry but your words are not a match, try again"); }
            }

        }
    }
}
Harminder
  • 141
  • 1
  • 8