0

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 :)

5
  • 4
    Break your big problem into smaller problems, solve each of the little problems and you will get to your final solution - this will apply to almost all programming obstacles you encounter Commented Dec 4, 2015 at 8:41
  • 1
    TryParse returns a bool that you're not checking - and, if it fails, it set the parameter value to one that falls within your valid range. Commented Dec 4, 2015 at 8:43
  • Thanks very much for the edit, it works and looks much better now however it still moves onto the next person after an empty input or after inputting coordinates which have a cheese on them already. I hope you can fix that for me as I've lost all faith in my limited abilities . Commented Dec 4, 2015 at 8:46
  • You could use an 2D array of booleans. And check if a cheese is in on it. If not place the cheese and if so handle it (throw error). Commented Dec 4, 2015 at 8:56
  • Don't give up! @jeroenh's answer should fix your first q, to prevent from going to the next player, (If I understand your logic) - add: i--; continue; inside if(!TryToPlaceCheese(CheeseX, CheeseY)) Commented Dec 4, 2015 at 9:04

2 Answers 2

1

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.