5
\$\begingroup\$

I wanted a routine that would allow me to use the NumPad keys for directional commands. 1-9 for each compass point, - for up and + for down. If one of these keys are not pressed, it would act as a regular readline(). I threw in a few simple lines to act as timers, obviously this was just to complete it.

This is my solution. It works fairly well, but thinking there is a better way.

 string orders;
        bool lives = true;
        int counter = 0;
        char[] delimeterChars = { ' ', ',', '.', ':', '\t' };
        Console.Write("What shall I do? ");

        do
        {
            string verb = "";
            string noun = "game";
            orders = Ask();
            Console.WriteLine("You pressed " + orders);
            counter = 0;
            string[] words = orders.Split(delimeterChars);

            verb = words[0].ToLower();
            if (words.Length > 1) { noun = words[1].ToLower(); }
            Console.WriteLine("verb is " + verb);
            Console.WriteLine("noun is " + noun);

            if (verb == "q" || verb == "quit") { noun = "game"; lives = false; }


        } while (lives == true);


        if (lives == false)
        {
            Console.WriteLine("Game over");
        }
    }



    public static string Ask()
    {
        do
        {
            ThirstTimer();
            HungerTimer();
            ParalysisTimer();
            PoisonTimer();
            AttackTimer();
            SpellTimer();
        } while (Console.KeyAvailable == false);

        var cki = Console.ReadKey(true);
        Console.Write(cki.Key.ToString());
        string VeRb = cki.Key.ToString();
        switch (VeRb)
        {
            case "NumPad1":
                return "go southwest";
            case "NumPad2":
                return "go south";
            case "NumPad3":
                return "go southeast";
            case "NumPad4":
                return "go west";
            case "NumPad6":
                return "go east";
            case "NumPad7":
                return "go northwest";
            case "NumPad8":
                return "go north";
            case "NumPad9":
                return "go northeast";
            case "Add":
                return "go down";
            case "Subtract":
                return "go up";

      // If numpad is not used, get letter pressed and add to input.

            default:
                string Verbage = Console.ReadLine();
                string VERB = cki.Key.ToString() + Verbage;
                return VERB;

        }
    }
    public static void ThirstTimer()
    { //Console.WriteLine("You are thirsty."); } // Timers include current Time, target Time, and 30 second display timer
    }
    public static void HungerTimer()
    { //Console.WriteLine("You are hungry."); }// Timers include current Time, target Time, and 30 second display timer
    }
    public static void ParalysisTimer()
    { //Console.WriteLine("You are paralysed."); }// Timers include current Time, target Time, a display everytime you attempt to move while active.
    }
    public static void PoisonTimer()
    { //Console.WriteLine("You have been poisoned."); }// Timers include current Time, target Time, and 30 second display timer
    }
    public static void AttackTimer()
    { //Console.WriteLine("Checking Attack delay."); }// Timers include current Time, target Time, and 30 second display timer
    }
    public static void SpellTimer()
    { //Console.WriteLine("Checking Spell Delay."); }// Timers include current Time, target Time, and display timer based on mana usage
    }
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

A couple of things to mention:

Whenever you're receiving input from the user, you should write your code assuming the user will screw up and type the wrong thing. I don't see any checks to handle that.

When your switch block is only converting one value to another, you should be looking for a collection to help with that. In this case, in the Ask method, I would suggest an enum mapped to the character code values that you'll need:

enum Directions
{
    none = 0,
    southwest = 97,
    south,
    southeast,
    west,
    east = 102,
    northwest,
    north,
    northeast,
    up = 107,
    down = 109
}

This shortens the code in the Ask method considerably:

public static string Ask()
{
    do
    {
        ThirstTimer();
        HungerTimer();
        ParalysisTimer();
        PoisonTimer();
        AttackTimer();
        SpellTimer();
    } while (Console.KeyAvailable == false);
    var cki = Console.ReadKey(true);
    string keyString = cki.Key.ToString();
    Console.Write(keyString);
    Directions direction = Directions.none;
    if(Enum.TryParse<Directions>(((int)cki.Key).ToString(), out direction))
    {
        return $"go {direction}";
    }
    else
    {
        string Verbage = Console.ReadLine();
        return keyString + Verbage;
    }

}
\$\endgroup\$
3
  • \$\begingroup\$ Thank you tinstaafl, That's nice and simple... I will certainly do that. So much C# to learn, so little time. Now that that is settled, just for conversation and learning, are there any other methods you would consider? \$\endgroup\$
    – Just Earl
    Nov 20, 2019 at 21:16
  • \$\begingroup\$ At this time I don't see much else to change. Once you have it more fleshed out, the community will be happy to examine it more. \$\endgroup\$
    – user33306
    Nov 21, 2019 at 2:10
  • \$\begingroup\$ Awesome.. Thanks again \$\endgroup\$
    – Just Earl
    Nov 21, 2019 at 2:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.