π» Unit 2 Project: Choose-Your-Own Adventure Game
Overview
π₯ PROJECT PROGRAM SETUP INSTRUCTIONS
- Go to the public template repository for our class: BWL-CS Java Template
- Click the button above the list of files then select
Create a new repository - Specify the repository name:
CS2-Adventure-Game - Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositoriessection of GitHub! - Now on your repository, click and select the
Codespacestab - Click
Create Codespace on mainand wait for the environment to load, then youβre ready to code! - π Write code in this Codespace during class.
One of the first games coded for early computers in the 1970s was called Colossal Cave Adventure. It was a text-based interactive fiction game where you had to make your way through an elaborate cave. The program only understood one word or phrase commands like north, south, enter, take, etc.
You can try playing adventure recreated online following some of the commands in this walkthrough. Part of the challenge is finding the commands that the code will understand.
In a game like Adventure, if, else if, and else statements can be used to respond to commands from the user like n, e, s, w.

Instructions
PART A: Planning
- π§ Come up with a unique location for your adventure.
Your adventure could be set anywhereβ¦ a place you are familiar with (like BWL or Manhattan), a place youβd like to visit, or even a fantasy/fictional world.
- π PLAN & DRAW OUT YOUR MAP on paper before coding!!! Alternatively, make a FLOWCHART.
PART B: Starter Code
- Replace the content in your
Main.javafile with the following starter code:import java.util.Scanner; public class Main { public static void main(String []args) { System.out.println("Starting a new adventure...\n"); String command = ""; // stores the user's choices } // END OF MAIN METHOD /** Method to prompt & process input for the next command * @param prompt to display to user, asks for specific input * @return user's input command as a lowercase String */ public static String getCommand(String prompt) { System.out.println("\nβΆοΈβΆοΈβΆοΈ " + prompt); Scanner scan = new Scanner(System.in); String command = scan.nextLine().toLowerCase(); scan.close(); return command; } // END OF METHOD } // END OF CLASS - Incorporate this starter scenario inside your
mainmethod, after the existing statements:// STEP #1: Describe the scenario scene System.out.println("You are at a crossroads."); // STEP #2: Get user command (prompt & take input) command = getCommand("Do you take the path on the LEFT (l) or RIGHT (r)?"); // STEP #3: Process user command (choose a path) if (command.equals("l")) { System.out.println("You turned left and continued walking."); } else if (command.equals("r")) { System.out.println("You turned right and continued walking."); } else { System.out.println("Bad input. Press RUN to restart!"); }
PART C: Write your own Branches
π Add more branches to expand the gameplay by nesting conditional blocks.
- Review π 2.4: Multiway Selection notes on Nested if Statements.
- Follow your map/flowchart carefully as you write code for the branches you designed.
EXAMPLE:
if (command.equals("l"))
{
System.out.println("You turned left and continued walking.");
// NEXT BRANCH...
command = getCommand("Do you pick the pretty flower πΊ on the path? (y/n)");
if (command.equals("y"))
{
System.out.println("The flower explodes! π£π₯");
}
else if (command.equals("n"))
{
System.out.println("You keep walking down the path.");
}
}
π‘ TIPS:
- Donβt forget to get the userβs input (the command) before opening the next conditional block!
command = getCommand("prompt"); - You do not need to keep the command options as
landrevery time. You can even have more than two choices ( liken,e,s,w) by providing moreelse ifstatements for that block.- Just make sure you tell the user what the options are in the prompt!
Extensions
Turn your text-based adventure into a visual one with a GUI (Graphical User Interface)! See my Java Swing demo: GitHub Swing GUI