0

I am working on code for a slot machine game and I am having trouble generating a random word from a list. This is what I have currently but I am unable to random generate a word from this list. I just need help determining the problem.

static void SlotMachine()
{
    Random rand = new Random();

    var list = new List<string>{ "Elephant", "Computer", "Football", "Resume", "Capstone", "Crimson" };
    var randomList = random.Next(list.Count);
    Console.Write("Enter amount of gil you want to risk: ");
    double gil = Convert.ToDouble( Console.ReadLine());
    double winningAmount = 0;

    for (int i = 1; i <= 3; i++)
    {
        string word = list[rand.Next(list.Count)];
        randomList.Add(word);
        Console.Write(word+" ");
    }
    Console.WriteLine();

    if (randomList[0] == randomList[1] && randomList[1] == randomList[2])
    {
        winningAmount = gil*3;
    }
    else if(randomList[0] == randomList[1] || randomList[0] == randomList[2] || randomList[2] == randomList[1])
    {
        winningAmount = gil * 2;
    }
    else
    {
        winningAmount = 0;
    }
    Console.WriteLine("Winning amount is {0} ",winningAmount);
 }

 static void Dice()
 {
     Random r = new Random();
     int val1 = r.Next(1,7);
     int val2 = r.Next(1,7);
     int val3 = r.Next(1,7);
     int val4 = r.Next(1,7);
     int val5 = r.Next(1,7);
     int sum = val1 + val2 + val3 + val4 + val5;
     int i = 1;
     
     for(i = 1; i<=4; ++i) 
     {
         Console.WriteLine("Guess a number between 5 and 30: ");
         int n = Convert.ToInt32(Console.ReadLine());
        
          if ( sum == n) 
          {
            Console.WriteLine("You have Guessed It Correctly and you win 50gil");
            break;
          }
          else if ( n > sum)
              Console.WriteLine("Too High");
          else
              Console.WriteLine("Too Low");
    }
    
    if(i==5)
        Console.WriteLine("Not able to Guess in 4 times, Thanks for playing");
  }
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • Clearly you have compilation errors here. Please include them. – ProgrammingLlama Mar 15 '21 at 05:42
  • Hint: `var randomList = random.Next(list.Count);` creates an `int` variable with a random value between 0 and `list.Count` inclusive. – ProgrammingLlama Mar 15 '21 at 05:43
  • Also, you'll probably be interested to read [Random number generator only generating one random number](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) as this might affect you in future (though probably not with this code). – ProgrammingLlama Mar 15 '21 at 05:44

0 Answers0