-3

I am currently making an Hangman game, and I have everything set except having it so for each turn the player is guessing on a random word, currently they can guess on all the words in my dictionary/wordbank, and also to tell the player how many letters the word has, Currently you can write random words in hope that you get the correct one. The only idea I have is o to use Random, but further then that and I'm lost.

Cheers.

class Program
{
   static List<string> ordbank = new List<string>() 
   {
      "Leksak", "Djur", "Organismer", "Mat", "Länder"
   };

    static bool runMenu = true; //kör menyn 
    static bool runGame = false; //kör spelet
    static int numberOfTries = 2; //antal försök personen har på sig 
    static int wrongGuesses = 0; // hur många gånger har personen gissat fel 
    static int numWordsToPutIn = 1; //Lägga till ett extra ord till listan, skulle vilja göra så man kan lägga till fler än 1 åt gången. 


    static void Main(string[] args)
    {
        Console.WriteLine("Hänga gubbe!\n1) Lägg till ord\n2) Lista till alla ord\n3) Spela\n4) Avsluta");
        do
        {
            Console.Write("Menu: ");
            string menuInput = Console.ReadLine();
            switch (menuInput.ToLower())
            {
                case "1":
                    Console.WriteLine("Du ska lägga till " + numWordsToPutIn + " ord nu.");
                    for (int i = 1; i <= numWordsToPutIn; i++)
                    {
                        Console.WriteLine("Lägg till ord " + i + ": ");
                        string wordInput = Console.ReadLine();
                        ordbank.Add(wordInput);

                    }
                    ordbank.Sort();
                    break;  //Ifall man vill lägga till nytt ord till listan.

                case "2":
                    Console.WriteLine("Nu skrivs alla orden ut: ");
                    if (ordbank.Count > 0)
                    {

                        foreach (string ord in ordbank)
                        {
                            Console.WriteLine(ord);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Listan är tom. Fyll den först."); //Behövs denna nu när jag ändrade så ord redan finns?
                    }
                    break; //Skriver ut orden de lagt in.

                case "3":
                    if (ordbank.Count == 0)
                    {
                        Console.WriteLine("Fyll ordlistan med " + numWordsToPutIn + " ord innan du börjar spelet");
                        break;
                    }

                    Console.WriteLine("Hur många fel får man ha: " + numberOfTries + " ");
                    Console.WriteLine("Då kör vi, gissa vilka ord som finns med");
                    runGame = true;
                    wrongGuesses = 0;

                    do
                    {
                        Console.Write("Gissa ord: ");
                        string guessedWord = Console.ReadLine();
                        if (ordbank.Contains(guessedWord))
                        {
                            ordbank.Remove(guessedWord);


                            //kolla om personen har vunnit 
                            if (ordbank.Count == 0)
                            {
                                Console.WriteLine("Grattis du vann");
                                runGame = false;
                            }
                            else
                            {
                                Console.WriteLine("Wohoo det ordet fanns med, fortsätt!");
                            }

                        }
                        else
                        {
                            wrongGuesses++;
                            //kolla om personen har förlorat 
                            if (wrongGuesses == numberOfTries)
                            {
                                runGame = false;
                                Console.WriteLine("Du förlorade.");
                                ordbank.Clear();
                            }
                            else
                            {
                                Console.WriteLine("Du gissade fel, du har " + (numberOfTries - wrongGuesses) + " försök kvar");
                            }
                        }

                    } while (runGame);
                    break;

                case "4":
                    Console.WriteLine("Spelet avslutas nu...");
                    runMenu = false;
                    break;
                default:
                    Console.WriteLine("Snälla välj ett tal mellan 1 - 4.");
                    break;        
            }
        } while (runMenu == true);

      }
   }
}  
MethodMan
  • 18,625
  • 6
  • 34
  • 52
ztumpan
  • 1
  • 4
  • 2
    Select a random number `r` between 0 and ordbank.Length - 1. Output `ordbank[r].Length`. – Eric J. Feb 02 '15 at 17:46
  • I'm sorry, I don't quite understand what you mean by that. – ztumpan Feb 02 '15 at 17:48
  • 1
    We probably should suggest new SO feature - match assignments to each other so people can pair up in person rather than asking the same question multiple times... Or at least have link to the course information so one willing to provide solution for given level of expertise can look through previous lectures and know what type of solution is expected... :) – Alexei Levenkov Feb 02 '15 at 17:52

2 Answers2

3

You can use the Random class to generate pseudo-random numbers. Its Next method can be used to retrieve a random number between 0 (including) and a given max value (excluding).

If you use the word list's element count as the max value, you're computing a random index which is between 0 and (list length - 1). You can then retrieve your random word at that index:

var random = new Random();

// Compute a valid list index
int randomIndex = random.Next(ordbank.Count);

// Fetch the word at that index
string randomWord = ordbank[randomIndex];

Make sure to only create one instance of Random and to re-use it. Otherwise, you might notice that the same numbers could be generated over and over again if Random.Next is called in a loop.

Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
0

This is obviously a course assignment of some sort, so i don't want to do it for you but the below code should give you a hint to the solution. Add all of the below to your code and use appropriately.

int selectedWordIndex = 0;
Random rand = new Random();
void pickNewRandomWord(){
    selectedWordIndex = rand.Next(ordbank.Count);
}
string getSelectedWord(){
    return ordbank[selectedWordIndex];
}
Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53
  • You got an off-by-one error here, the `-1` shouldn't be there. Also, since `List` implements `ICollection`, you can use the `Count` property directly — no need for the `Count()` extension method. – Marius Schulz Feb 02 '15 at 20:37
  • on count you are right. but on the cout of a list of 10 elements return 10 and random whould run between 0-10 and thats 11 elements 1 element too much so thus the -1, giving 0-9 the valid index range. (i might be wrong on random though (does it never return the max, in this case 10?) – Thomas Andreè Wang Feb 04 '15 at 12:10
  • 1
    Yes, that's exactly the case. The maximum value [is exclusive](https://msdn.microsoft.com/en-us/library/zd1bc8e5(v=vs.110).aspx) for `Random.Next(int)`, which means passing the `Count` property itself (not decremented) is just right — the highest possible return value will be `Count - 1`, and that's the highest allowed list index. – Marius Schulz Feb 04 '15 at 12:57
  • Ahh on then, thanks for the pointer, il note that behind my ear (as it absolutely will be nice to know). – Thomas Andreè Wang Feb 04 '15 at 12:59