dazoar
dazoar

Reputation: 53

Reading char value from Console

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?

Upvotes: 2

Views: 2592

Answers (4)

Nate Barbettini
Nate Barbettini

Reputation: 53720

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);
    }
}

Upvotes: 2

Super Jade
Super Jade

Reputation: 6384

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

direction = Console.ReadKey();

Upvotes: 0

wingerse
wingerse

Reputation: 3796

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.

Upvotes: 1

Matthew Spencer
Matthew Spencer

Reputation: 2270

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

Upvotes: 0

Related Questions