Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a simple shotgun game where the user vs the CPU and the both pick shot, shield or reload but In my GetOptionFromUser method I am not sure how to return value from the enumeration method depending on what the user chose.

Any Guidance would be appreciated Here is my methods
C#
enum ShotgunOption
         {
             Shoot = 1,
             Reload = 2,
             Shield = 3,
          
             
         }
        static void DisplayMenu()
        {
             
            Console.WriteLine("Please pick an item:");
            Console.WriteLine("S - Shoot");
            Console.WriteLine("P - Shield");
            Console.WriteLine("R - Reload");         
            Console.WriteLine("X - Exit");
            
        }

       
            static ShotgunOption GetOptionFromUser()
    {
               
        char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            if (menuItem == 'S')
            {
                return ShotgunOption.Shoot;
            }
            else if (menuItem == 'P')
            {
                return ShotgunOption.Shield;
            }
            else if (menuItem == 'R')
            {
                return ShotgunOption.Reload;
            }
            while (menuItem != 'F' && menuItem != 'C' &&
                menuItem != 'I' && menuItem != 'X' && menuItem != 'R' && menuItem != 'D')
            {
                Console.WriteLine("Error - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            }
        }

        
    
        
        static void DisplayResults(ShotgunOption UserOption,ShotgunOption CPUOption, int UserScore, int UserBullets, int CPUBullets)
    {
        Console.Clear();
        Console.WriteLine("Giving up?");
        Console.WriteLine("You Chose {0}, The Computer Chose{1} Your Score is {3} . You had {4} Bullet(s). The CPU had {5} bullets(s).", UserOption, CPUOption, UserScore, UserBullets, CPUBullets);
        Console.WriteLine("Thanks for playing!");
        Console.ReadKey();

    }
Posted

Your while conditions don't match menu options...other then that it seems you're returning options just fine. Remove first set of if conditions, replace while loop with do..while and put those ifs inside:

C#
static ShotgunOption GetOptionFromUser()
  {

      char menuItem;
          
          
          do 
    {
        DisplayMenu();
          menuItem = char.ToUpper(char.Parse(Console.ReadLine()));

              if (menuItem == 'S')
          {
              return ShotgunOption.Shoot;
          }
          else if (menuItem == 'P')
          {
              return ShotgunOption.Shield;
          }
          else if (menuItem == 'R')
          {
              return ShotgunOption.Reload;
          }
else {
Console.WriteLine("Error - Invalid menu item");
}
while (menuItem != 'F' && menuItem != 'C' &&
              menuItem != 'I' && menuItem != 'X' && menuItem != 'R' && menuItem != 'D')

          }
      }


I didn't touch the conditions because they do not relate to the menu so maybe you have some more application logic hidden...play with it for a bit.

Good luck.
 
Share this answer
 
You major problem is not what you described, but the way you write your code. You violate the SPOT principle, which makes your code unsupportable and development error-prone. Not only you hard-code immediate constants (as far as your enumeration problem is concerned, these are strings/characters), but you repeat the same "magic strings" in more than one place of the code.
Please see: http://en.wikipedia.org/wiki/Single_Point_of_Truth[^].

Big bunch of problems related to enumerations are solved in two my articles. It includes enumeration looping through the enumeration type members, using enumeration types as array indices and definition and use of human-readable enumeration member names:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

You can also learn other useful techniques related to enumerations.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900