-2

I'm writing a C# text based game to learn how to code.

I don't know how to explain this, but I wan't my game to be able to recognize if you input a wrong parameter and asks you the question given to you again.

E.g

  • Code Question: Do you wan't to open that door?
  • Your Answer: asdhasd
  • Code Answer: I can't understand that.
  • Code Question: Do you wan't to open that door?
Jorge Gill
  • 175
  • 1
  • 2
  • 10
  • There's no question in your question. Please read [ask] for critical advice on how to express your question in a clear, answerable way. – Peter Duniho Feb 07 '18 at 00:13
  • This question is way too broad and is going to be closed. Advice: Don't use a text-based game to learn how to code in C#. There are entire languages for text-based games. For a reason. – zzxyz Feb 07 '18 at 00:13
  • There is no apostrophe in the word "want". – Rufus L Feb 07 '18 at 00:41
  • Ask the question (and get the response) in a `do { } while ();` loop whose exit condition is a correct answer. – Rufus L Feb 07 '18 at 00:42

2 Answers2

3

Normally for this kind of task I write a helper method that takes in a string "prompt" (which is the question being asked to the user) and one or more valid responses. Then the method repeatedly asks the question in a do/while loop whose condition is that the response is one of the valid answers.

Note that it's usually a good idea to give the user some choices for valid input (y/n) so they have some idea why you keep asking them the same question over and over again if they enter something else. Though they may be pleasantly surprised if you accept other answers, like "sure" or "nope".

In the example below, this method returns a bool, and takes in two lists of valid answers: valid true answers, and valid false answers. This way it only returns true or false, but accepts a wide variety of input from the user:

public static bool GetBoolFromUser(string prompt, List<string> validTrueResponses, 
    List<string> validFalseResponses, StringComparison comparisonType)
{
    string response;

    // Combine all valid responses for the sake of the loop
    var allValidResponses = 
        validTrueResponses?.Union(validFalseResponses) ?? validFalseResponses;

    do
    {
        Console.Write(prompt);
        response = Console.ReadLine();
    } while (allValidResponses != null &&
             !allValidResponses.Any(r => r.Equals(response, comparisonType)));

    // Now return true or false depending on which list the response was found in
    return validTrueResponses?.Any(r => r.Equals(response, comparisonType)) ?? false;
}

Then, in our main code we can create two lists of valid responses, pass them along with a prompt to the method above, and we know it will return us a true or false result that we can use to make a decision:

private static void Main()
{
    // Add any responses you want to allow to these lists
    var trueResponses = new List<string>
    {
        "y", "yes", "ok", "sure", "indeed", "yep", "please", "true"
    };

    var falseResponses = new List<string>
    {
        "n", "no", "nope", "nah", "never", "not on your life", "false"
    };

    bool openDoor = GetBoolFromUser("Do you want to open the door? (y/n): ",
        trueResponses, falseResponses, StringComparison.OrdinalIgnoreCase);

    if (openDoor)
    {
        Console.WriteLine("Ok, opening the door now!");
    }
    else
    {
        Console.WriteLine("Ok, leaving the door closed!");
    }

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

Output

Here's what it looks like if you run it and give some bad responses...

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

If you're writing a text adventure for a C# console app, you can find a good description on one possible way of structuring a text adventure game in Usborne's 1980s computer book called "Write Your Own Adveture Programs" which has since been released for free at https://usborne.com/browse-books/features/computer-and-coding-books/

The programming language it uses is old BASIC, so I wouldn't recommend using the code directly (although .NET Console Apps have Console.ReadLine() and Console.WriteLine() which work almost the same as BASICs INPUT and PRINT), but more than that the book talks about how text adventures of the time were structured and the planning that goes into it.

For your specific situation, the old text adventures defined a list of possible verbs (actions) and a list of possible nouns (objects). The user would type either a single verb, or a verb and a noun. The code would match these to the lists, and jump to a specific function (or subroutine) based on the entered verb.

The game loop then prompted the user for an action, accepted some input, processed it in this way, and went back to prompting.

For example:

What should I do now?

north

... would match the "north" verb in the list of verbs, so the program would run the routine to move the player one room north (which would then make sure the player could actually go north from their current location).

What should I do now?

open door

... would split the string on a space, match the "open" verb from the verb list, and the "door" noun from the noun list. The open function would then check where the player was, if there was a "door" there, if the door was already open, etc. then update a "door state" to open, and return a message.

What should I do now?

asdf asdf

... wouldn't match any verb in the list, so the program would respond with "I don't understand how to 'asdf'" or "I don't know what a 'asdf' is."

Of course, there are many approaches to solving this problem and you may already have a design in place that is different from this.

Hope this helps

Community
  • 1
  • 1
Trevor
  • 1,251
  • 1
  • 9
  • 11