예제 #1
0
        private void GetInput()
        {
            var keyPressed = ui.GetKey();

            switch (keyPressed)
            {
                case ConsoleKey.LeftArrow:
                    Move(Direction.West);
                    break;
                case ConsoleKey.RightArrow:
                    Move(Direction.East);
                    break;
                case ConsoleKey.UpArrow:
                    Move(Direction.North);
                    break;
                case ConsoleKey.DownArrow:
                    Move(Direction.South);
                    break;
                //case ConsoleKey.P:
                //    PickUp();
                //    break;
                //case ConsoleKey.I:
                //    Inventory();
                //    break;
                case ConsoleKey.Q:
                    Environment.Exit(0);
                    break;
                default:
                    break;
            }

            var actionMeny = new Dictionary<ConsoleKey, Action>()
            {
                {ConsoleKey.P, PickUp },
                {ConsoleKey.I, Inventory },
                {ConsoleKey.D, Drop }
            };

            if (actionMeny.ContainsKey(keyPressed))
            {
                var method = actionMeny[keyPressed];
                method?.Invoke();
            }

        }
예제 #2
0
        public int AskForKey(string prompt)
        {
            bool success;
            int  keyPressed;

            do
            {
                Console.Write(prompt);
                var input = ui.GetKey();
                success = int.TryParse(input, out keyPressed);
                if (!string.IsNullOrEmpty(input) && success)
                {
                    success = true;
                }
            } while (!success);

            return(keyPressed);
        }