2

I am making a game and need to ask the user which direction they want to move and store it as a char (L, R, U, D).

The char will be passed to this method:

       static void Movement(int n, int rolled, char direction)
        {
        Console.WriteLine("Making a move for " + players[n].Name);


        if (direction == 'u' || direction == 'U')
        {



            if (players[n].Y - rolled < 0)
            {
                players[n].Y = players[n].Y + 8 - rolled;
            }
            else
                players[n].Y = players[n].Y - rolled;
        }
        else if (direction == 'd' || direction == 'D')
        {



            if (players[n].Y + rolled > 7)
            {
                players[n].Y = players[n].Y - 8 + rolled;
            }
            else
                players[n].Y = players[n].Y + rolled;
        }
        else if (direction == 'l' || direction == 'L')
        {



            if (players[n].X - rolled < 0)
            {
                players[n].X = players[n].X + 8 - rolled;
            }
            else
                players[n].X = players[n].X - rolled;
        }
        else if (direction == 'r' || direction == 'R')
        {



            if (players[n].X + rolled > 7)
            {
                players[n].X = players[n].X - 8 + rolled;
            }
            else
                players[n].X = players[n].X + rolled;
        }

            Console.WriteLine(" Please pick a direction: (U,D,L,R");

                char direction = Console.ReadLine();//this gives me an error

Console.ReadLine() gives me an error, because it returns a string. How do I read a value in as a char and store it in direction?

4 Answers 4

2

You're looking for Console.ReadKey.

char direction = Console.ReadKey().KeyChar;

Or, if you don't want the key to be displayed, you can use the intercept parameter:

char direction = Console.ReadKey(true).KeyChar;

Unlike ReadLine, which returns a string, ReadKey returns a ConsoleKeyInfo. If you just need the character, you can get it from KeyChar as I did above, or you can get the key code (or other things like modifiers).


Also, you could make your code a little more readable by using ToUpper() and a switch statement:

static void Movement(int n, int rolled, char direction)
{
    direction = char.ToUpper(direction);

    Console.WriteLine("Making a move for " + players[n].Name);

    switch (direction)
    {
        case 'U':
            if (players[n].Y - rolled < 0)
            {
                players[n].Y = players[n].Y + 8 - rolled;
            }
            else
                players[n].Y = players[n].Y - rolled;
            break;

        case 'D':
            if (players[n].Y + rolled > 7)
            {
                players[n].Y = players[n].Y - 8 + rolled;
            }
            else
                players[n].Y = players[n].Y + rolled;
            break;

        case 'L':
            if (players[n].X - rolled < 0)
            {
                players[n].X = players[n].X + 8 - rolled;
            }
            else
                players[n].X = players[n].X - rolled;
            break;

        case 'R':
            if (players[n].X + rolled > 7)
            {
                players[n].X = players[n].X - 8 + rolled;
            }
            else
                players[n].X = players[n].X + rolled;
            break;

        default:
            throw new ArgumentException("Unknown direction " + direction);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use Console.ReadKey() instead of Console.ReadLine(). The ReadLine method will read the entire line as a string, but ReadKey method will read the first char. But it returns a ConsoleKeyInfo object. Use ConsoleKeyInfo.KeyChar to get the char.
But I suggest you to use Enums instead of chars because if you decided to change your direction representations, using enums would be easier.

Comments

0

Have you considered the Console.ReadKey Method? This can read in a character which could be processed from the Console.

Comments

0

Use Console.ReadKey() to store a character value. For example,

direction = Console.ReadKey();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.