C# Text Adventure: Lesson 10

Filtering User-Input for "Yes" or "No"

In this lesson, we'll develop a method in ConsoleInputOutput.cs to present a question (that can be answered with "yes" or "no") and obtain a definitive "yes" or "no" answer from the user.

Why? Because many times in a text adventure game we may want to prompt the user for a yes/no response. Get confirmation of user's intention to quit the game. A dragon appears atop the hill. Do you want to run? You are carrying a lot in your pack. Would you like to sell some of it before you leave Gerzack's Emporium?

Now, perhaps you could come up with your own method to accomplish this. And perhaps it would be better than mine. If that's the case, go for it! But below I present to you method areYouSure():

    public static int areYouSure()
    {
        /*  This method loops until user either enters 'Y' for "yes" or 'N' for "No".
            It returns 1 if 'Y' was entered, 0 if 'N'. */

        Console.TreatControlCAsInput = true; //prevents ending in user presses Ctrl-C

        bool invalidInput = true;
        int result = -1;
        string response = string.Empty;

        while (invalidInput)
        {
            conShow("\\n");
            conShow("\\pAre you sure you want to quit and exit the application? (", "\\wh");
            conShow("Y", "\\ge");
            conShow(")es or (", "\\wh");
            conShow("N", "\\ge");
            conShow(")o ", "\\wh");
            conShow("", "\\ye");

            ConsoleKeyInfo cki = Console.ReadKey();
            response = cki.Key.ToString().ToUpper();

            if (response == "Y" || response == "N")
            {
                invalidInput = false;
                result = 1;
                if (response == "N") { result = 0; }
            }
        }

        conShow("", "\\ye");
        ConsoleColors.conWhiteOnBlack();
        conShow("\\n");
        return result;
    }

Copy/paste it into ConsoleInputOutput.cs and save the source code file. Then, switch to your command prompt and execute a dir *.dll command. Although the timestamps on *your* DLLs will likely differ from mine, they should be approximately the same size. Of course, if you expanded the selfTest() method in *ConsoleColors.cs*, it's DLL may be larger than the one shown in the following screen shot:

Whatever your timestamps for those two libraries, they'll bear later timestamps (and, if you saved the new method in ConsoleInputOutput.cs, its corresponding DLL will grow in size) after you enter the following at the command prompt:

rebuild

You know what happens now, right? Yes, we're going to build a test app to see if our newly added areYouSure() method works as expected. So, create a new source code file named testInputOutput3.cs.

Enter the following source code listing in a new Notepad++ file and save it as indicated above:


public sealed class testInputOutput3
{
    public static void Main()
    {
        ConsoleIO.conShow("", "\\wh");
        ConsoleIO.conShow("\\n");
        ConsoleIO.conShow("\\pYou stand with your hand hovering over the\\n");
        ConsoleIO.conShow("\\plaunch button for the nuclear ICBMs. Press \"Launch\"?\\n");

        int result = ConsoleIO.areYouSure();

        if (result == 1)
        {
            ConsoleIO.conShow("\\pBang! Hello World World III!\\d", "\\ma");
        }
        else
        {
            //zero was returned; only 1 or zero CAN be...
            ConsoleIO.conShow("\\pMmm, yeah. Discretion is the better part of valor.\\d", "\\ma");
        }
        ConsoleIO.conShow("", "\\wh");
    }

}

Just for shits and giggles, rebuild your DLLs using the following at the command prompt:

rebuild

Then, compile the test file by entering the following at the command prompt:

csc /r:ConsoleInputOutput.dll testInputOutput3.cs

Below, I show the two possible outputs of the test program, depending upon whether you answer Yes or No to the question the program poses:

Note that the areYouSure() method even poses the "Are you sure (Y)es/(N)o" question. It loops until a definitive Yes (user entered 'Y') or No (user entered 'N') is obtained. Then it returns integer 1 if "yes" or 0 if "no". A useful method!

You can click here if you need/want to download the files associated with this lesson.

Return to Lesson 9 | Proceed to Lesson 11

Return to Index


Author: kyrathasoft

The only programmer in Glens Fork, Ky.