C#: Choose Your Own Adventure

An activity to introduce C# in 45-60m. C# is an object-oriented programming language, often used to make desktop applications. This lesson covers input/output from the console, variables, and conditional statements. The goal of the lesson is to build a simple Choose Your Own Adventure game using the C# console.

Setup

Follow the Setup guide to get ready to build an introductory program with Replit!

User Input

The Choose Your Own Adventure game should allow the user to decide how the story plays out. To do that, the program must ask a question, and remember the user's answer.

  1. Add another Console.WriteLine statement under the first one, and make it say "You wake up. What do you do?"
    • Make sure everything is capitalized correctly
    • Make sure there are parentheses (()) and quotes ("")
    • Make sure there is a semi-colon (;) at the end of the statement
  2. Under the Console.WriteLine statement, add another statement to read the user's answer:
     string answer = Console.ReadLine();
    

Now answer is a variable that holds the answer from the user. A variable is a name that points to a value in the code. A variable can hold anything, and the program can even change what it holds. For now, the answer variable allows the program to remember the user's answer to the question.

  1. Make another new line under the string answer statement, and add a Console.WriteLine
  2. Make the statement say "You said "
  3. After the "You said " ending quote, add + answer so that the answer is added to the end of the message:
     Console.WriteLine("You said " + answer);
    
  4. Run the program, and test that the user's answer to the question is said back!

Branching

Now, the program should change the story based on the user's answer. This is possible with if statements. These work just like in English; for example, "if it is raining, I will stay indoors." For the story, the program can create different outcomes in this way.

  1. Create a new line under the last Console.WriteLine statement
  2. Add an if statement to check whether the answer is equal to the text "Go back to sleep":
     if (answer == "Go back to sleep")
    
  3. Add curly brackets ({ and }) to open up the body of the if statement
    • All of the code in between those brackets will only run if the check is true!
  4. Create a new line after the opening bracket ({)
  5. On the new line, add a Console.WriteLine saying "You sleep happily."
  6. Run the code, and check that the next line appears when entering "Go back to sleep"

Code

if (answer == "Go back to sleep") {
    Console.WriteLine("You sleep happily.");
}

Another Answer

Now the program can respond to one answer, but what about another one? Use another if statement to check if the user entered a different answer.

  1. After the closing curly bracket (}) for the if, make a new line and add a new if statement
  2. In the parentheses, check if answer is equal to the text "Get up"
  3. Add opening and closing curly brackets after the parentheses
  4. Within the curly brackets, add a Console.WriteLine to say "You get out of bed."
  5. Run the code, and check that the proper line appears when entering "Get up"

Code

if (answer == "Get up") {
    Console.WriteLine("You get out of bed.");
}

Continuing the Story

After the user gets out of bed, the program can again ask how to proceed. Everything within the curly brackets of the second if will run only if the user said to get up. So, it is possible to ask another question within that branch!

  1. Under the "You get out of bed" line, add another Console.WriteLine asking the user what they would like to do next
  2. Use Console.ReadLine again to take in the user's answer, and store it in a variable named answer2
  3. If the user says to go to school, write a message saying "You learn a lot at school!"
  4. Otherwise, if the user says to go to the movies, write a message saying "You get in trouble :("
  5. Run the code, and test that the different branches work as expected!

Final Code

using System;

class MainClass {
    public static void Main (string[] args) {
        Console.WriteLine("Welcome to my game!");
        Console.WriteLine("You wake up. What do you do?");
        string answer = Console.ReadLine();

        if (answer == "Go back to sleep") {
            Console.WriteLine("You sleep happily.");
        }

        if (answer == "Get up") {
            Console.WriteLine("You get out of bed.");
            Console.WriteLine("What do you do next?");
            string answer2 = Console.ReadLine();

            if (answer2 == "Go to school") {
                Console.WriteLine("You learn a lot at school!");
            }

            if (answer2 == "Go to the movies") {
                Console.WriteLine("You get in trouble :(");
            }
        }
    }
}

Next Steps

Try to continue the story with some new branches! Write it out on paper or in Notepad first to get an idea of where the story should go. Then, try to update the code to make it into a playable game! Feel free to elaborate on the existing story, or change it completely. Have fun!

Kahoot

After the activity, there is a Kahoot covering the material: https://create.kahoot.it/details/choose-your-own-c-adventure/61875aea-36fd-4f7e-8847-f03615b1baaf

results matching ""

    No results matching ""