Choose Your Adventure Game - Part 3
For the next part of the game, change what happens in the program based on the user's input. This code-along covers if
/else
statements.
Starting Point
For this code-along, start from the ending point of part two. If needed, fork this Repl project to begin.
Going to the Barn
If the user decides to go to the barn, they should find a potion. They should also gain one health point.
- At the bottom of the main block, make a new line
- Create an
if
structure usingif () { }
- For the condition, check if the user typed in "barn"
- For the body, display a message saying the player found a potion
- Also in the body, set
health
to be itself plus one
Run the program, and verify that it is possible to visit the barn. The message should only appear if the player types in "barn" when prompted! The code should look something like this:
if (go == "barn")
{
Console.WriteLine("You go into the barn and find a potion. +1health");
health = health + 1;
}
Going to the Tree
Next, if the user decides to go to the tree, they should find some bees. They should also lose one health point.
- Under the
if
block}
, make a new line - Create an
else if
structure usingelse if () { }
- For the condition, check if the user typed in "tree"
- For the body, display a message saying the player found some bees
- Also in the body, set
health
to itself minus one
Run the program, and verify that it is possible to visit the tree. The new message should only appear if the player types in "tree" when prompted! The new code should look something like this:
else if (go == "tree")
{
Console.WriteLine("You check out the tree and find some bees. -1health");
health = health - 1;
}
Going down the Path
The last option is this: going down the path. In this case, the player should face the monster and lose five health points.
- Under the
else if
block}
, make a new line - Create an
else if
structure usingelse if () { }
- For the condition, check if the user typed in "path"
- For the body, display a message saying the player faced the monster
- Also in the body, set
health
to itself minus five
Run the program, and verify that it is possible to visit the path. The new message should only appear if the player types in "path" when prompted! The new code should look something like this:
else if (go == "path")
{
Console.WriteLine("You go down the path and face the monster.");
health = health - 5;
}
Going Somewhere Invalid
Now the player can successfully visit all of the places, but they might type in something else. Handle this case by telling them they entered an invalid option.
- Under the
else if
block}
, make a new line - Create an
else
structure usingelse { }
- Note that there is no condition for an
else
- Note that there is no condition for an
- For the body, display a message saying invalid option
Run the program, and verify that the message appears when it should. The new message should only appear if the player types in something other than "barn", "tree", or "path" when prompted! The new code should look something like this:
else
{
Console.WriteLine("Invalid option.");
}
End Result
Lastly, the player's final health will determine whether or not they survive. Print out a message with this information at the end of the program.
- Make a new line at the bottom of the main block
- Create an
if
/else
structure usingif () { } else { }
- For the
if
condition, check if thehealth
variable is less than1
- In the body of the
if
, print out "You have died." - In the body of the
else
, print out "You make it out of the woods."
Run the program, and verify that the proper message appears based on the user choice! If the user goes down the path, they should die. Otherwise, they should live. The new code should look something like this:
if (health < 1)
{
Console.WriteLine("You have died.");
}
else
{
Console.WriteLine("You make it out of the woods.")
}
Final Program
By the end of this code-along, the main.cs file should look something like this:
using System;
class Program {
public static void Main (string[] args) {
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Clear();
Console.WriteLine("You awaken in a strange place.");
Console.WriteLine("You see a barn, a big tree, and a path through the woods.\n");
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
int health = 5;
Console.WriteLine("\nHello " + name);
Console.WriteLine("Your health is " + health);
Console.WriteLine("\nWhere do you go? (barn, tree, path)");
string go = Console.ReadLine();
Console.WriteLine("\nYou go to the " + go);
if (go == "barn")
{
Console.WriteLine("You go into the barn and find a potion. +1health");
health = health + 1;
}
else if (go == "tree")
{
Console.WriteLine("You check out the tree and find some bees. -1health");
health = health - 1;
}
else if (go == "path")
{
Console.WriteLine("You go down the path and face the monster.");
health = health - 5;
}
else
{
Console.WriteLine("Invalid option.");
}
if (health < 1)
{
Console.WriteLine("You have died.");
}
else
{
Console.WriteLine("You make it out of the woods.")
}
}
}