Now that we have covered what arrays are, and how to use them, let's integrate them into our Adventure Game. We'll use our Choice() method to handle three different scenarios. The content arrays will store pieces of text, and we'll show different ones depending on the choices the player makes.
Arrays in the Adventure Game
Content Pattern
You should have your three scenarios written out. Each scenario can be broken down into the same pattern if you followed the optional template:
- Choice introduction and options
- [Player makes a choice]
- Depending on choice one of two paths is shown
- Story continues (for both paths)
- Player has something different happen depending on choice made and path, perhaps finding an item (like a coin)
- Followup text that will transition to the next part of the game
We'll repeat this pattern three times, once for each scenario.
There are two areas that will show content based on player choice. We will save the option the player chooses in a variable, so that we can use a conditional statement to show the appropriate text when needed.
We could diagram the pattern like this:
Content Arrays
Translating the pattern as an array, it could look like this:
static string[] PartOne = { "Description of story ... and the choice A or B", "... what happens if A is chosen...", "... what happens if B is chosen...", "....more story....", "... more about what happens if A is chosen...", "... more about what happens if B is chosen...", "Transition to next part of story...." };
Reading through the array and matching it to the flowchart, we see this formula:
- The first part of the scenario, stored at index 0, will be shown and the player will see a choice between two options.
- Depending on which choice the player makes, only one of the next two index numbers (1 and 2) will be shown.
- Index 3 is always shown.
- Only one of index numbers 4 and 5 will be shown, again depending on player choice.
- Index 6 is always shown.
So we know that no matter what, the player will see elements at index 0, index 3, and index 6.
If the player has chosen A they will also see index 1 and index 4.
If they've chosen B, they'll see 2 and 5.
Create Arrays
At the top of your Game class, by the CharacterName declaration, build three arrays to store your scenarios. Test your application to make sure there are no errors.
You've created arrays!
Conditional Statements Pattern
Translating the flowchart to code, we can create a structure like this:
string input = ""; //write the beginning of the scenario //and ask for a choice between a or b Console.WriteLine(PartOne[0]); Console.Write("Enter your choice: "); //read in the choice and save it input = Console.ReadLine(); //make whatever was typed in lowercase // we could also use ToUpper() and then // capitalize the A and B in the if statements // either way it allows the player to type in lower or capital letters input = input.ToLower(); //if the choice was a, print out the second element in the array [1] if (input == "a") { Console.WriteLine(PartPartOne[1]); } //if the player types b (or anything else) we'll assume they want the second choice else { Console.WriteLine(PartPartOne[2]); } //write out the next part of the story for everyone Console.WriteLine(PartPartOne[3]); //if the choice was a, print out the second element in the array [4] if (input == "a") { Console.WriteLine(PartPartOne[4]); } else { Console.WriteLine(PartPartOne[5]); } //write out the transition to the next part of the story for everyone Console.WriteLine(PartPartOne[6]);
This code will be the same for all three scenarios, and is the main code that will drive the game forward.
for
We are going to surround the Choice() method statements with a for loop. This will keep our game moving forward until we run out of scenarios. We know that there are three scenarios, so we could type the number 3 in our for loop header:
for (int scenario = 1; scenario <= 3; scenario++) { }
However, what if we later want to add more scenarios and make the game play longer? We would need to find this loop in our code and manually update it. Since our project is so small, this isn't too much trouble. But if we keep expanding our project, or work on another project that is much larger that has several references to a number that might change later, we would want to have a better solution than having "hard coded" data.
Add another variable that will track the number of scenarios. At the top of your Game class, by the CharacterName declaration, add this line:
static int Scenarios = 3;
Then add the loop structure:
for (int scenario = 1; scenario <= Scenarios; scenario++) { }
Multidimensional Arrays
An alternative option is a multidimensional array.
The Length property of an array could be used instead of the Scenarios variable in the for loop header.
Multidimensional arrays are out of the scope of this series, but it is an option that you can explore later if you are interested in them.
switch
A switch will run statements if the value in the parentheses matches the value after "case". The keyword "break" will break the program flow out of the switch statement. The basic form we will use is:
switch (scenario) { case 1: //if scenario equals 1, statements here run break; case 2: //if scenario equals 2, statements here run break; case 3: //if scenario equals 3, statements here run break; default: //if scenario does not match any above break; }
In the example above we are using an integer (a whole number) to track what scenario we are currently in. We are initializing our counter variable "scenario" with 1 in our for loop. The first time the switch statement runs, scenario will equal 1 and the statements after "case 1:" will run.
The for loop will increment the counter, and the next time the switch statement runs scenario will be 2.
More About switch
In some languages you can leave out a break in a switch statement and the flow of the program will fall down to the next case.
However, in C# if you don't end a switch statement you will get an error. The most common way of satisfying the end requirement is using the break keyword.
Read more: switch (C# Reference)
Add a switch inside your for loop. Inside the switch add your scenario content.
Test your game and make sure everything is working. If you have errors, try to fix them on your own before you look at the example code. Troubleshooting code can be frustrating sometimes, but experimenting and fixing bugs helps you learn the language.
Example Code: Choice Method
Adding the for loop, switch statement, and our code for writing out content depending on what scenario the player is in, and what choices they've made, we have something that looks like the code below inside our Choice method.
for (int scenario = 1; scenario <= 3; scenario++) { string input = ""; switch (scenario) { case 1: //Part One //same pattern for each of the sections. 1) print the first part of the section Console.WriteLine(PartOne[0]); //2)read in player's choice (a or b) Console.ForegroundColor = ConsoleColor.Green; Console.Write("Enter your choice: "); input = Console.ReadLine(); input = input.ToLower(); Console.ResetColor(); //3) if a print the next part of the array, otherwise skip next and print 3rd if (input == "a") { Console.WriteLine(PartOne[1]); } else { Console.WriteLine(PartOne[2]); } //4) print next part of the section Console.WriteLine(PartOne[3]); //5) again if a print next, otherwise skip ahead if (input == "a") { Console.WriteLine(PartOne[4]); } else { Console.WriteLine(PartOne[5]); } //6) print last piece of the section Console.WriteLine(PartOne[6]); break; case 2: //Part Two Console.WriteLine(PartTwo[0]); Console.ForegroundColor = ConsoleColor.Green; Console.Write("Enter your choice: "); input = Console.ReadLine(); input = input.ToLower(); Console.ResetColor(); if (input == "a") { Console.WriteLine(PartTwo[1]); } else { Console.WriteLine(PartTwo[2]); } Console.WriteLine(PartTwo[3]); if (input == "a") { Console.WriteLine(PartTwo[4]); } else { Console.WriteLine(PartTwo[5]); } Console.WriteLine(PartTwo[6]); break; case 3: //Part Three Console.WriteLine(PartThree[0]); Console.ForegroundColor = ConsoleColor.Green; Console.Write("Enter your choice: "); input = Console.ReadLine(); input = input.ToLower(); Console.ResetColor(); if (input == "a") { Console.WriteLine(PartThree[1]); } else { Console.WriteLine(PartThree[2]); } Console.WriteLine(PartThree[3]); if (input == "a") { Console.WriteLine(PartThree[4]); } else { Console.WriteLine(PartThree[5]); } Console.WriteLine(PartThree[6]); break; default: //default if section number does not match one of the above break; } //let player advance when ready, then clear the screen Console.WriteLine("Press enter to continue..."); Console.ReadKey(); Console.Clear(); }
If you haven't backed up your project in awhile, do it now.
You've created code that displays content from an array conditionally with for, switch, and if/else statements!
Quick Quiz
In C#, the first element in an array by default is at what index number?
If we want to increment a variable (for example, a counter) what data type would be best?
Example Code Progress
If you'd like to see an example code structure, expand the toggle box below.
Full Example Code
Your code should have unique content based on the story you've written for your game. Be sure that you aren't using any of the example text! Celebrate your creativity and make game content you think is fun and interesting.
To Do: Practice, Play Test, and Utility Class Creation
Assignments
- Create another version of your Madlib that uses arrays to store variables: Simple C# Madlib (Part Three)
- Play test your game.
- Create utility class.
Play Test
Watch someone play your game. See if there are any points where they are confused or unsure how to proceed. A great rule for play testing is for you as the developer to remain silent as much as possible. Don't guide them through the game - you will get more information about how to improve it if you stay quiet and observe.
If your tester asks what they should do next, ask them what they think the options are. This can give you valuable insight on what they are thinking as they are playing. Something may be obvious to you as the creator because you already know how the game works. It might not be as obvious to your player. If they are truly stuck, help them out to move forward and be sure to take notes on what isn't clear for them.
Utility Class
Identify supportive methods that can help you in more than one project. Create a class for the utilities and add it to the current version of your adventure game (example utility class).
Code Files
If you have errors, or just want to check how your code compares in terms of progress, the code examples for this chapter are linked below.
Next Step
We've almost reached the end of this adventure series. In the last two sections we'll use lists to track items that have been found by the player, discuss objects (instances of a class), and look at an alternative way to work with content in our game.