Using Random With Enumeration

148 views Asked by At

Hi I am making a simple shotgun game where the user vs the computer and pick shoot, shield or reload But when I try to set the enumeration to random it gives me the error

Cannot implicitly convert type int to ShotgunGame.Program.ShotgunOption. An explicit conversion exists (are you missing a cast?),

I am not sure how to fix this.

Any Guidance would be appreciated

//Declare Variables
        Console.Title = "Welcome To The Shotgune Game";
        int CPUBullets = 3, userBullets = 3;
        ShotgunOption UserOption;
        int computerChoice, userScore = 0;
        bool QUIT = false;
        double gameCount = 0.0;
        Random computer = new Random();

        Console.Clear();
        Console.WriteLine("SHOOT RELOAD SHIELD");
         do
        {
            do
            {
            //Console.Write("Please enter choice, or enter QUIT to quit: ");
            UserOption = GetOptionFromUser();

            if (UserOption.ToUpper() == "QUIT")
            {
                break;
            }
            ShotgunOption CPUOption = computer.Next(1, 3); // 1 is Shot, 2 is Reload, 3 is Shield



              switch (UserOption.ToUpper())
            {
                case "SHOOT":
                    if (computerChoice == 1)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shoot. It was a tie!", userChoice);
                        ; userBullets --;CPUBullets --; ++gameCount;
                    }
                    else if (computerChoice == 2)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Reload. You win!", userChoice);
                        ++userScore; ++gameCount;
                    }
                    else if (computerChoice == 3)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
                        ++gameCount;
                    }
                    break;
                case "RELAOD":
                    if (computerChoice == 1)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", userChoice);
                         ++userScore; ++gameCount;
                    }
                    else if (computerChoice == 2)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Reload. You Both Gain A bullet", userChoice);
                        userBullets++; CPUBullets++; ++gameCount;
                    }
                    else if (computerChoice == 3)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);

                    }
                    break;
                case "SHIELD":
                    if (computerChoice == 1)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", userChoice);
                        ++gameCount;
                    }
                    else if (computerChoice == 2)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Reload. You win!", userChoice);
                        ++userScore; ++gameCount;
                    }
                    else if (computerChoice == 3)
                    {
                        Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
                        ++gameCount;
                    }
                    break;                  

            }
          }
          while (UserOption != ShotgunOption.Shield || CPUOption != 4);
        } while (QUIT == false || gameCount == 3);
2

There are 2 answers

0
Luke On BEST ANSWER

Shotgune game

As the error message suggests, you're trying to assign an int to a variable that is expecting an enum value. You can cast the int to an enum, though:

(EnumName)integerValue

So in this case:

ShotgunOption CPUOption = (ShotgunOption)computer.Next(1, 3);
0
Saveendra Ekanayake On
Array values = Enum.GetValues(typeof(ShotgunOption));
Random computer = new Random();
ShotgunOption CPUOption = (ShotgunOption)values.GetValue(computer.Next(values.Length));

Try this...