Question Are their any ways I can improve this short program?

boywithastick

New member
Joined
May 5, 2018
Messages
1
Programming Experience
Beginner
Hey guys this is my first post, I just wrote a little program which uses some user input to position the "player" in a certain location, the program then generates a position for a "killer", finally determining which position (left, middle, right) you chose, and what random position the killer chose it will print out the result such as "YOU DIED" if you were both in the same position, or if you picked something different to the killer it prints "YOU DODGED THE KILLER YOU LIVE!".

I was just wondering of ways I could improve/condense this code down, maybe an alternative to using switch statements.

Any help would be appreciate, I am 4 days into learning c# and I am just looking to improve as a programmer.

Thank you very much!

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApp3
{
    class Program
    {
        
        static void Main(string[] args)
        {
            Random random = new Random();
            var killerChosenLoc = "";
            var playerChosenLoc = "";


            string[] positions = new string[3] { "left", "middle", "right" };


            Console.Write("Choose your hiding spot (0 = left, 1 = middle, 2 = right): ");
            var playerLoc = Convert.ToInt32((Console.ReadLine()));


            var killerLoc = random.Next(0, 3);


            switch (playerLoc)
            {
                case 0:
                    playerChosenLoc = positions[0].ToUpper();
                    break;
                case 1:
                    playerChosenLoc = positions[1].ToUpper();
                    break;
                case 2:
                    playerChosenLoc = positions[2].ToUpper();
                    break;
                default: 
                    break;
            }


            switch (killerLoc)
            {
                case 0:
                        killerChosenLoc = "LEFT";
                        break;
                case 1:
                        killerChosenLoc = "MIDDLE";
                        break;
                case 2:
                        killerChosenLoc = "RIGHT";
                        break;
            }


            Console.WriteLine(playerLoc);
            Console.WriteLine(killerLoc);




            Console.WriteLine("You are at: " + playerChosenLoc +
            "\nThe killer is at: " + killerChosenLoc);


            Program finalResult = new Program();
            string result = finalResult.CompareResults(playerLoc, killerLoc);


            Console.WriteLine(result);


        }


        //Add both the playerloc (1,2,3) && enemyloc (1,2,3) and depending 
        //on the result it will print out the correct message.
        public string CompareResults(int player, int killer)
        {
            var result = "";


            if (player == killer)
            {
                result = "YOU ARE DEAD!";
            }
            else
            {
                result = "YOU DODGED THE KILLER! YOU LIVE!";
            }
            return result;
        }
    }
}
 
Last edited:
How about positions[playerLoc] ?
 
I would suggest using an enumeration for the positions, rather than using strings and ints of your own at different times. You also ought to add some validation of the user input. It also doesn't make sense that you creating an instance of the Program class there. You should have declared that CompareResults method 'static' and then just called it directly.
 

Latest posts

Back
Top Bottom