CODE CREATIVE
CHOOSE YOUR OWN ADVENTURE
Take control of the action
"A Story of the 3 Little Piggies"
If you aren’t familiar with “Choose your own Adventure” storybooks, they present the reader with the first page of the story. At the end of the page, there is a message that asks the reader what they want the main character to do. Each choice refers the reader to a certain page in the book to continue the story. In this lab, we will construct our own “Choose your own Adventure” stories!

Getting Started
Our program is going to start with a title along with the first paragraph of the story. To begin the program, we will make a call to the ‘start’ function:

	function start(){
		println("A Story about 3 Pigs: A Choose your own Adventure!”);
	}
	


In the block of code above, the compiler will begin execution of the program at the start() function. Go ahead and add the code above to your program. After running the program, we can see that the title of the story is printed to the user.

Now Try
  1. Change the title of the story to one that you want to write about.
Now that we have a title printed out, we need to write the first paragraph of our story. Take a look at the following code:

	function start():
		println(“A Story about 3 Pigs: A Choose your own Adventure!”);
		println("\nIn the woods, there lived 3 little piggies: Pinky, Oink and Bacon. Pinky was a lazy pig and always liked to swing in his hammock while chewing on a long piece of straw. Oink was a outdoorsman and his favorite thing to do was go on long hunts for rare species of mushrooms. Bacon was an inquisitive little pig and couldn't get his head out of a book.\n"))
	
You can see that there is now another print command that prints out the first paragraph of our story. What might surprise you is the symbol ‘\n’. This symbol forces the program to start a newline. Without these symbols, the story would be on one long line. Try taking the ‘\n’ symbols out and run your code. Do you see the reason I placed them where I did?

After adding the above code to your program and running it, you should see that we now have a title and the first paragraph of our story printed to the user.

Now Try
  1. Write the first paragraph of the story to one that fits your title. Remember to run your code to make sure there are no errors and everything is formatted well. You might have an error in your code or need to change the ‘\n’ so everything prints correctly.


Decisions, Decisions, Decisions
What we need now is for the program to ask the user to make a choice that will affect the direction of the story. We can do this by using an ‘input’ command. Take a look at the following code:
	println(“\n\nWhich piggy would you like to follow?”);
	println(“1. Pinky  	2. Oink  	3. Bacon”);
	
	choice = readLine(“Make your choice: “);
	
You can see that the following code asks the user to make a choice and receives the input from the user. Let’s add this to our code. Be sure to place it in the start() function after the first paragraph of your story.

Now Try
  • Add your own question to the user. In our example, there were 3 different choices the user could make, feel free to give the user 2, 3 or even 4 choices!
  • Feel free to change the look of the menu:
    	println(“\n\n1. I want to follow Pinky.”);
    	println(“2. I want to see what Oink does.”);
    	println(“3. I’m sure Bacon will do something interesting.”);
    	
    	choice = readLine(“Make your choice: ")
    	
    Defining our Functions
    You ask the user to make a choice, but now you have to change the story based on the user’s choice. Let’s create a set of functions that will output different versions of the story depending on what option the user chose. We can start by creating a function definition for Pinky. Remember to place the function definition below the start() function:
    	function pinky():
    	
    We have started to write our function definition, but we still need a block of code that will run when the ‘pinky’ function is called. Let’s add that now:

    	function pinky():
    		println(“\nAfter spending one too many afternoons lazily swinging in the hammock, Pinky's parents kicked him out of the house and he off he went with all he owned in a red sack hanging on the end of a stick. Slinging his stick over his shoulder, Pinky began whistling his favorite tune. Soon Pinky ran into his friends Oink and Bacon. They were both sweating in the hot sun building themselves a shelter...\n")
    	


    Try running our code and you can see that if you choose ‘1’, you will get the next part of the story from Pinky’s perspective printed out.

    Now Try
  • Now, take some time to write in the next part of your story when the user chooses option ‘1’.

  • Deciding When to Call our Function
    Great job, we have our function definition written, but we need to call it in our ‘main’ function. We do this by adding a function call, but remember that we only want to call the pinky() function when the user chooses to follow “Pinky the Pig”. We can get our program to make such a decision by using a conditional statement. Try the following unary conditional statement in your start() function after the user enters their choice:
    	#User chooses Pinky
    	if(choice == '1'):
    		pinky();
    	
    Looking at this unary conditional statement, we can see that this line of code calls the pinky() function when the user chooses ‘1’, but what about when the user chooses ‘2’? Let’s add to the unary conditional statement to make it a binary selection.

    What we really want is for the branch to be a chained conditional statement so we can account for if the user choose 1, 2 or 3. Let’s do that now and add to our binary conditional statement. If you have forgotten, the following is the general form of a chained conditional statement:

    	if( -Boolean expression- ):
    		# Block of code 1
    	else if( -Boolean expression- ):
    		# Block of code 2
    	else:
    		# Block of code 3
    	
    	
    Looking at the code above, we can see that the chained conditional statement will execute block 1 when the first Boolean expression is true. If the first Boolean statement is false, the conditional statement will check the second Boolean expression. If it is true, the second block of code will be executed. If the second Boolean statement is false, then the 3rd block of code will always be executed. Remember that we can have as many ‘else if’ statements as we want in a chained conditional.

    By now your ‘main’ function should have a chained conditional statement that calls 3 different functions based on the user’s input.

    Now Try
  • Write the function definitions for the function calls you have just written in your 2nd and 3rd block of code, if you wrote one.


  • Next Stage of the Story
    We should have a program that allows the user to choose the second stage of the story. Your program should print out that second stage, but we still need to give the user choices on what the next step will be and receive their input. Do that now. After completing that, our program should look like the following:

    	function pinky():
    		println(“\nAfter spending one too many afternoons lazily swinging in the hammock, Pinky's parents kicked him out of the house and he off he went with all he owned in a red sack hanging on the end of a stick. Slinging his stick over his shoulder, Pinky began whistling his favorite tune. Soon Pinky ran into his friends Oink and Bacon. They were both sweating in the hot sun building themselves a shelter…\n”);
    		println(“What would you like to say to your friends?”);
    		println(“1. Oink! Oink!  	2. Got any food?  	3. Can I stay with one of you guys?”);
    	
    		question = readLine(“Choose 1, 2 or 3: “);
    	

    Completing Your Adventure
    Great, job! Now you need to get your program to make another decision based on the user’s input. Here I would include another chained conditional statement inside the function ‘pinky’ based on the user’s input. We need to continue this entire process for each stage of our story.

    Project
    Now Try
    1. Create a story that has at least 4 stages, at least 4 sentences per stage and at least 2 conditional statements per stage.


    Extra Credit
    Now Try
    1. Create a game that needs to be won.
    2. Add a graphical interface using topics from "Games and Animations" chapter.
    Student Examples
    Below are a list of student projects that have been voted into the top three of previous classes. Click a few below and try them out!

    Julia & Kaitlyn: Fast Food
    Irene: Survival Mission
    Kathy: Ball Bounce
    Anton & Jerry: Project
    Theo: Norse
    Steven & Brian: Wealth Test
    Brian: Tron
    Brian: Minesweeper
    Brian: Flappy Bird
    Brian: Number Puzzle