đ Project Challenge: Building a Text Adventure
Text Adventure Game
Bridging Variables and Control Flow to OOP
Welcome to a special project challenge! So far, weâve explored the fundamentals of C# programming, focusing on how to store data using Variables and how to guide our programâs execution using Control Flow statements (if
, else
, switch
, while
, for
).
This challenge is designed to help you solidify these concepts by applying them to a slightly larger, more interactive application. Weâll be building the basic structure for a Simple Text Adventure Game. Think of classic games like Zork or Colossal Cave Adventure, but much simpler!
Why this project?
- Reinforces Core Concepts: Youâll heavily use variables to track the playerâs state (like location, maybe health or score later) and control flow to handle player commands and game logic.
- Interactive Application: It moves beyond simple calculations to create something the user can interact with directly.
- Bridge to OOP: As we build this using only variables and control flow, you might start to see areas where organizing the code could be improved. For example, how do we represent a room with its description and exits? How do we manage the playerâs inventory? These questions naturally lead into the concepts of Classes and Objects, which weâll cover in upcoming lessons. This project sets the stage for understanding why OOP is so useful.
The Goal:
Our goal isnât to build a complete, complex game yet. Instead, weâll create the essential foundation: a simple world with a few locations, the ability for the player to move between them using text commands (like ânorthâ, âsouthâ), and basic feedback.
Guidance Style:
This challenge emphasizes your problem-solving skills. Instead of providing the exact C# code, I will guide you step-by-step with:
- Explanations: Describing what needs to be done.
- Hints: Pointing you towards the right C# features (variables, specific loops, conditional statements).
- Pseudocode: Outlining the logic in plain English or structured comments.
Itâs up to you to translate these hints and pseudocode into working C# code. Donât be afraid to experiment and refer back to the previous lessons on Variables and Control Flow!
Letâs begin building our adventure!
Project Steps: Building the Foundation
Letâs break down the creation of our text adventure foundation into manageable steps. Remember to translate the hints and pseudocode into actual C# code in your Program.cs
file.
Step 1: Setting Up the Game World (Variables)
First, we need to represent the gameâs state. For now, this is mainly the playerâs current location and maybe a way to know if the game is running.
- Hint: Use variables to store this information. What data types are suitable for representing a location (maybe just a number or a name for now?), and for tracking if the game should continue running?
// Declare a variable to store the player's current location.
// Let's use numbers: 1 for "Forest Clearing", 2 for "Cave Entrance", 3 for "Inside Cave"
// Initialize the player's starting location (e.g., Forest Clearing).
// Declare a variable to control the main game loop.
// This variable should indicate whether the game is still running.
// Initialize it to indicate the game should start running.
Step 2: The Main Game Loop
A text adventure game runs continuously until the player decides to quit. We need a loop that keeps the game going.
- Hint: Which type of loop (
while
ordo-while
) makes sense here? The loop should continue as long as the game is considered ârunningâ (based on the variable from Step 1).
// Start the main game loop
// Loop WHILE the game is still running
// {
// Inside the loop, we will:
// 1. Display the current location's description.
// 2. Get the player's command.
// 3. Process the command.
// }
// After the loop finishes (game is no longer running)
// Display a "Goodbye!" message.
Step 3: Displaying Location Information (Control Flow)
Inside the loop, the first thing we should do is tell the player where they are. The description will depend on the playerâs current location variable.
- Hint: Use conditional statements (
if-else if-else
orswitch
) to check the value of the playerâs location variable and print the corresponding description.
// Inside the main game loop...
// Check the player's current location variable
// IF location is 1 (Forest Clearing)
// Print "You are in a forest clearing. Paths lead north and south."
// ELSE IF location is 2 (Cave Entrance)
// Print "You stand at the dark entrance to a cave. You can go south or east."
// ELSE IF location is 3 (Inside Cave)
// Print "It's dark inside the cave. You can only go west."
// ELSE
// Print "Error: Unknown location!" // Good practice for unexpected situations
Step 4: Getting Player Input
After showing the location, ask the player what they want to do.
- Hint: Use
Console.Write()
to prompt the user andConsole.ReadLine()
to read their command into astring
variable.
// Inside the main game loop, after displaying location...
// Prompt the user for input (e.g., "What do you do? ")
// Read the user's command and store it in a string variable.
// Consider converting the input to lowercase to make command checking easier (e.g., "NORTH" becomes "north").
Step 5: Processing Player Commands (Control Flow)
This is the core logic! Based on the playerâs command and their current location, we need to decide what happens.
- Hint: This will involve nested conditional statements. First, check the command. Then, within the command check, you might need to check the current location to see if that command is valid there.
// Inside the main game loop, after getting input...
// Process the command (using the lowercase version)
// IF the command is "quit"
// Set the game running variable to indicate the game should stop.
// Print a message like "Quitting game..."
// ELSE IF the command is "north"
// // Check if moving north is possible from the current location
// IF current location is 1 (Forest Clearing)
// Update the player's location variable to 2 (Cave Entrance).
// Print "You walk north."
// ELSE
// Print "You can't go that way."
// ELSE IF the command is "south"
// // Check if moving south is possible
// IF current location is 1 (Forest Clearing)
// Print "You stumble into thick woods. Can't go south."
// ELSE IF current location is 2 (Cave Entrance)
// Update the player's location variable to 1 (Forest Clearing).
// Print "You walk south."
// ELSE
// Print "You can't go that way."
// ELSE IF the command is "east"
// // Check if moving east is possible
// IF current location is 2 (Cave Entrance)
// Update the player's location variable to 3 (Inside Cave).
// Print "You cautiously enter the cave."
// ELSE
// Print "You can't go that way."
// ELSE IF the command is "west"
// // Check if moving west is possible
// IF current location is 3 (Inside Cave)
// Update the player's location variable to 2 (Cave Entrance).
// Print "You exit the cave."
// ELSE
// Print "You can't go that way."
// ELSE (for any other command)
// Print "Unknown command."
// Add a blank line for spacing before the next loop iteration
// Print an empty line using Console.WriteLine()
Step 6: Putting It All Together and Testing
Assemble the code from the steps above inside your Program.cs
Main
method. Ensure the location display, input prompt, and command processing are all inside the main game loop.
- Testing: Run your program (
dotnet run
). Try moving between the locations. Can you go north from the clearing? South from the cave entrance? East into the cave? West out of the cave? What happens if you try an invalid direction? Does âquitâ work? Fix any issues you find!
Further Challenges (Using Variables & Control Flow)
Congratulations on building the foundation of your text adventure! Now that you have the basic movement and interaction loop working, here are some ways you can expand the game using only the concepts weâve covered so far (Variables and Control Flow):
- More Locations:
- Add 2-3 new locations (e.g., a hidden path, a riverbank, an old shack).
- Hint: Youâll need to assign new numbers to these locations, update the location display logic (
Step 3
), and add moreelse if
conditions to the command processing (Step 5
) to handle movement to/from these new areas.
- Look Command:
- Implement a âlookâ command that provides a slightly more detailed description of the current location or mentions something interesting.
- Hint: Add another
else if
block inStep 5
to check for the âlookâ command. Inside this block, useif-else if
based on thecurrentLocation
variable to print a specific detail for that location.
- Simple Items:
- Introduce one or two items the player can find (e.g., a key, a lantern).
- Hint: Use
bool
variables (e.g.,hasKey
,hasLantern
) initialized tofalse
. When the player enters a specific location and maybe uses the âlookâ command, set the corresponding variable totrue
and print a message that they found the item.
- Simple Puzzle:
- Make one location accessible only if the player has a specific item.
- Hint: In
Step 5
, when processing the command to move to the restricted location, add an innerif
statement that checks the required itemâs boolean variable (e.g.,if (hasKey)
). If they have the item, allow movement; otherwise, print a message like âThe door is locked.â or âItâs too dark to proceed without a light.â
- Score Keeper:
- Add a score to the game.
- Hint: Declare an
int score = 0;
variable at the beginning. Increment the score (score += 10;
orscore++;
) when the player performs certain actions, like entering a new location for the first time (requires anotherbool
variable per location to track if visited) or finding an item.
- Help Command:
- Add a âhelpâ command that lists the available commands (north, south, east, west, look, quit, help).
- Hint: Add an
else if
block for the âhelpâ command inStep 5
and simply print the list of commands.
These challenges will make your if-else if
structures more complex and require careful management of your variables, reinforcing the concepts youâve learned while making your game more engaging!