Blaise
Blaise

Reputation: 1

Stuck on C# input validation

I'm doing my first console application at school which is a Cheese Racer Game. Sixteen pieces of cheese are distributed on the board by up to 4 players. Two pieces of cheese cannot be placed on this same square, which is decided by another method which returns a bool. Here is what I have so far.

do
{
    for (int i = 0; i < NoOfPlayers; i++)
    {
        string YourTurnCheese = "Your turn, " + players[i].Name + "!";
        Console.SetCursorPosition((Console.WindowWidth - YourTurnCheese.Length) / 2, Console.CursorTop);
        Console.WriteLine(YourTurnCheese);
        do
        {
            string XCoordinate = "X Coordinate: ";
            Console.SetCursorPosition((Console.WindowWidth - XCoordinate.Length) / 2, Console.CursorTop);
            Console.Write(XCoordinate);
            int.TryParse(Console.ReadLine(), out CheeseX);
            if (CheeseX > 7 || CheeseX < 0)

                Console.WriteLine("Please enter a number from 0 to 7");
            else
                break;
        } while (true);

        do
        {
            string YCoordinate = "Y Coordinate: ";
            Console.SetCursorPosition((Console.WindowWidth - YCoordinate.Length) / 2, Console.CursorTop);
            Console.Write(YCoordinate);
            int.TryParse(Console.ReadLine(), out CheeseY);
            if (CheeseY > 7 || CheeseY < 0)
                Console.WriteLine("Please enter a number from 0 to 7");
            else
                break;
        } while (true);

        if (!TryToPlaceCheese(CheeseX, CheeseY))
        {
            string CheeseisHere = "There is already a piece of cheese at this location.";
            Console.SetCursorPosition((Console.WindowWidth - CheeseisHere.Length) / 2, Console.CursorTop);
            Console.WriteLine(CheeseisHere);

        }
        else
        {
            Board[CheeseX, CheeseY] = SquareState.gotCheese;
            TotalCheesePlaced = (TotalCheesePlaced + 1);
        }
    }
} while (TotalCheesePlaced < 16);

I'm not sure how to make it reject empty input into the console. Also, when there is already a cheese piece on the location given, it states that, but moves onto the next player instead of looping back. Can I get some help with fixing that code please? I'm still pretty new to all of this so please be gentle :)

Upvotes: 0

Views: 152

Answers (2)

Andrea Dusza
Andrea Dusza

Reputation: 2110

This way, it does not move onto the next person when the field is occupied.

do
{
    for (int i = 0; i < NoOfPlayers; i++)
    {
        do
        {
           ...
            do
            {
                ...
                if (CheeseX > 7 || CheeseX < 0)

                    Console.WriteLine("Please enter a number from 0 to 7");
                else
                    break;
            } while (true);

            do
            {
                ...
                if (CheeseY > 7 || CheeseY < 0)
                    Console.WriteLine("Please enter a number from 0 to 7");
                else
                    break;
            } while (true);

            if (!TryToPlaceCheese(CheeseX, CheeseY))
            {
                string CheeseisHere = "There is already a piece of cheese at this location.";
                Console.SetCursorPosition((Console.WindowWidth - CheeseisHere.Length) / 2, Console.CursorTop);
                Console.WriteLine(CheeseisHere);

            }
            else
            {
                Board[CheeseX, CheeseY] = SquareState.gotCheese;
                TotalCheesePlaced = (TotalCheesePlaced + 1);
            }
        } while (!TryToPlaceCheese(CheeseX, CheeseY));
    }
} while (TotalCheesePlaced < 16);

Upvotes: 0

jeroenh
jeroenh

Reputation: 26782

Your problem is here:

int.TryParse(Console.ReadLine(), out CheeseX);

First, split this as follows:

string input = Console.ReadLine();
int.TryParse(input, out CheeseX);

Now, you are parsing the input but not doing anything when the input is invalid. The TryParse method returns true if it succesfully converted the input to an integer, and false otherwise. So you could do something like this:

    if (!int.TryParse(input, out CheeseX))
    {
         Console.WriteLine("invalid input, try again");
         break;
    }

Upvotes: 1

Related Questions