Hey, you've reached the final project,
and as you've seen at the beginning of today's lessons,
I showed you what we're going to be making
which is a sort of choose your own adventure game.
I don't know if you've ever read those,
Choose Your Own Adventure books
where you flip to a different page
if you make a particular choice
and then the story kind of evolves as you make your choices.
Now we're going to make a very simple version of that
using what we've learned about conditionals,
if, else, elif statements
as well as everything we've done previously.
So you've already seen what the final outcome
should look like and if you want ot review it again,
just head over to treasure-island-end.appbrewery.repl.run,
and here you'll see essentially the game in action.
So it starts off asking you, "You're at a crossroad,
do you want to go left or right?"
And then you have to type your choice.
If you selected left,
then it's going to take you to a lake
and it asks you whether if you want towait for a boat
or swim across.
So let's go ahead and "wait for a boat".
And then the final step is it asks you,
which door would you like to go through
red, yellow, or blue?
Now depending on your choice,
you'll either end up with game over
or you'll end up with winning the game.
So the basic setup for this game looks something like this.
And I've included a link to this flowchart
in the starting Repl.
So replit/appbrewery/treasure-island-start,
and of course you'll find all the links
in the course resources as always.
But if you scroll down, there's a link right here
that takes you to this flowchart.
Now, the first question you're going to ask the user
is to make a choice between left and right.
Now you can use my wording if you want.
"You're at a crossroad.
Where do you want to go?
Type "left" or type "right"."
But I think the fun part of this challenge
is really creating your own story, right?
But somehow get them to choose between left and right.
If they choose right or any other condition for that matter,
tell them that they've lost, Game Over
for some sort of reason.
In my case, if they had chosen right,
the reason is because they fell into a hole, Game Over.
But I think you are far more imaginative than I am.
So go ahead and use all of those skills
and make your game really fun.
Now, if they've chosen left on the other hand,
then we're going take them to another question
that asks them whether if they want to swim
or whether if they want to wait for a boat.
And if they swam, then it's going to be some form of Game Over.
There's crocodiles, there's whatever it may be,
but they can't continue.
But if they chose wait, then we take them to the third
and final question which asks them to choose
between three doors.
And if they choose any door
other than the yellow door, then they would lose,
but if they chose the yellow door, then they somehow win.
Now notice how each of these just says Game over,
Game over, left or right,
but you know, the idea is really to use your creativity
and make this game really your own.
Now one thing to remember though
is that when the user types in an answer,
let's say left or right,
they might type it like this with an uppercase R
or they might type it like this all lowercase.
Have a think about how you might make sure
that no matter which version they typed,
you are still going to be able to catch
their command essentially.
So have a think about
what you've learned in the previous lessons
and take a look at this flowchart
then head over to the Starting Project
and you can get started writing your code.
Now if you head over here
and what you see is sort of like a mumbo jumbo
bunch of characters,
then the reason is probably because there's not enough space
for this ASCII art of a Treasure Chest to display.
So you can either do, as I did,
reduce the size of the console
or alternatively and probably easier
is to change the layout to stacked,
make it go at the bottom, collapse this left hand pane,
and then you've got as much space as you have to work with.
Now ASCII art is really cool,
because it basically just uses
a bunch of characters that you know and love
like the comma and the equal sign
and all of these things that together make up a picture.
And you can find your own ASCII art
if you just go to ascii.co.uk/art
and you'll see a whole bunch of different topics
and they've grouped a lot of things together
into individual pages.
So if you wanted a rhino ASCII art,
then you can find a whole bunch of rhinos
that people have drawn.
But the easiest way is of course using Command + F
if you're on a Mac
or Ctrl + F if you're on a Windows machine,
and then just search for the thing that you want.
So for example, I went and searched for treasure,
and down here I found this brilliant Treasure Box
in ASCII art, which is what I'm using
to print inside this print statement.
Now, notice how there's three single quotes (' ' ')
that are at the start
and there's three single quotes (' ' ') at the end.
Basically what the single quotes allow you to do
is to create multiple lines of a string essentially.
So notice how if I go ahead and delete
all of these single quotes
and I only had a set of double quotes,
then it actually doesn't quite work.
It gets quite confused because it thinks
that this is where the line ends,
but in fact, I wanted to print all of this out.
So to do that, instead of using a double quote,
I use three single quotes.
And at the very bottom,
I tell it well this is the end of my multi block string
by using three single quotes as well.
So have a go at running that
and you should be able to see that in your console.
You get this little Treasure Island ASCII art being printed
along with the two sentences that begin the game.
S have a look at the flow chart,
have a play around with the ASCII art,
and also have a look at the final version of the app
so that you can go ahead
and create your own version to complete this challenge.
Pause the video now and give that a go.
All right, so let's see
if we can replicate the functionality
or the logic that's shown in this diagram
into our Treasure Island game.
So the first thing it has to do is we have to ask the user
whether if they want to turn left or right.
And after we ask the user this question,
we're going to want to capture their input, right?
So let's go ahead
and create an input() function and ask our question.
Now notice how when I use my double quotes around my string,
the double quotes that are inside the double quotes
are interpreted as code because it actually thinks that
this is the end of the first string,
this is the second string, and this is the final string.
This is how the computer is going see it.
Now previously you saw that we could do the trick
where we change this to single quotes instead.
But if like me, you have this case
where you have the "You're" with the apostrophe,
then it's going to think
that this is actually the end of the first string
this is the second string and this is the third string.
So this doesn't actually really help us.
Now all we can do when we want to tell our computer
that what I'm writing is actually not code
just see it as text.
You can get it to ignore a symbol afterwards
by using the backslash (\).
So this way it basically escapes this string
and it will see it as text.
And now we have a complete string like this
and this is just going to be interpreted as text
because we told it to with the backslash.
Now of course there's other ways
that you could have gone around this.
You could have just simply said
"You are at a crossroad..." rather than using the apostrophe,
but more often than not,
you'll need to use a symbol that you'll need to escape
and you can do that with the backslash.
So that's just a quick tip.
Now at this point,
the user is going to type a message left or right.
So let's go ahead and save that to a variable.
Let's call it choice1 equals their input.
Now remember how I said they could be typing the answer
as "Right" or as "right".
So how can we make sure that when we are doing our checks
using the if statement we ignore the casing?
Well, one way of doing this is we can simply
use the "lower() function" to change their input
no matter how they wrote it, Right, right, or RIGHT,
it should all be converted into lowercase to this version.
That way we have one thing
that we can consistently check for in our if statement,
which is coming up next.
So we can say if choice1 ==, remember
the double equals is checking
whether if left hand side and right hand side is equal
and the single equal sign is assigning the right hand side
to this particular variable name.
Now if choice1 equals the string right,
then it's pretty much Game Over for our player.
So let's go ahead and add our colon
and print something like Game Over.
Of course we can elaborate this a little bit more
as to why was it Game Over.
"Well, because you fell into a hole."
This is like the ET game from the '80s.
Lots of holes.
So, "You fell into a hole. Game Over."
But what if they had chosen left instead?
Well, that takes them on to the next question.
So that's the option that will allow them to continue
along the game.
Now in this case, we could use an else statement and say,
well if they didn't choose right,
then they probably chose left, right?
But they could have also chosen like an option
that was completely not listed.
They could have just written something like this
and it would still trigger the else statement.
So what we should actually do is to switch it around.
So namely we should say, if choice1 == "left":
well in that case, then they continue you in the game.
But otherwise, namely if they chose "right"
or anything else for that matter,
then it's pretty much Game Over.
So this format makes a lot more sense
if you want to continue
along the left side of the branch, this way.
Now another thing that you might have realized
because we learned about combining different conditions
is instead of using the .lower()
to change the input to lowercase,
you could have also said if choice == "left"
or if choice1 == "Left": spelt like this,
that will both work.
But I think in terms of succinctness and less code writing,
I think this makes a lot more sense to me.
So I'm going to continue with this version.
But if you did it the other way,
that's perfectly valid as well.
Now if their choice1 was left,
then they get taken to the next question.
Do you want to swim or do you want to wait?
So let's go ahead and continue the game
and create another input.
In this input we're going to ask them a question
and say, "You've come to a lake.
There is an island in the middle of the lake.
Type "wait" to wait for a boat.
Type "swim" to swim across.
Cool, so we've actually got the same problem
as we had before, namely that this is being interpreted
as code rather than as a continuous single string.
So do you remember how we fixed this previously?
And if you got stuck on this,
I recommend having a go at this yourself
before I show you the answer.
All right, so previously we said we were going to switch it
to single quotes instead
so that these double quotes become interpreted
as just normal strings.
But of course we've got this "You've,"
we could of course change it to "you have" instead of you've,
but if we want to escape the string, we just add a \
and now everything is colored in this sort of pinkish color
to indicate it's all interpreted as a string,
which is exactly what we did previously.
Now in terms of this input,
they're going to type "wait" or they're going to type "swim".
So let's go ahead and lower the casing for it as well,
and then we're going to save it inside a choice.
We're going to say choice2.
Now be really careful and mindful of your indentation,
because if you wrote it like this,
then it's going to be creating some errors for you
because this choice2 input line
has to run when this choice1 is equal to left,
otherwise we're going to get an indentation error.
So let's go ahead and indent it here
to show that this part is inside this if statement
and will only be executed if this is true.
And then we get to check our choice2.
So, if choice2 == "wait": ,
then it means that the game will continue,
but else, so if they typed swim
or anything else for that matter,
then the game is going to end.
So we're going to print
and tell them that something terrible happened,
"You got attacked by an angry trout.
Game Over."
Now we can continue along this path.
So we're down to here now.
And the final question we're going to ask the user
is which door do they want to choose?
So here I'm going to use an input()
to send them the message
that, "You arrive at the island unharmed.
There is a house with three doors.
One red, one yellow, one blue.
Which color do you choose?"
Now I'm going to save their choice
inside a variable called choice3,
lowercase the answer that they provided.
And finally I'm going to use if, elif, and else
to check which one they chose.
So if choice3 == "red":
elif choice3 == yellow,
elif choice3 is equal to blue.
And then I've got an else.
So here are four possible things that could happen.
If they chose red, then I'm going tell them
that it's Game Over,
but it's going to be some sort of creative
version of Game Over.
So maybe something like, "It's a room full of fire.
Game Over."
If they chose yellow on the other hand,
well that's going to be the room where the treasure exists.
So I'm going to print, "You found the treasure!
You Win!"
If they chose blue, they also end up with Game Over
and I have to give them a different reason.
Now if you had decided
that if they chose anything but yellow,
the way that they're going to game over is the same,
say if, you know, if you chose red,
"It's a room full of fire.
Game Over."
If you chose blue, "It's a room full of fire.
Game Over."
Anything else, it's also going to be fire.
Game over.
Well then you could just use if and else,
but if you wanted each choice or each door
to have a different outcome for the user,
say, "The red door has fire. The blue door has beasts."
And if they chose the wrong door,
then they're going to get something like
"You chose a door that doesn't exist.
Game Over."
So can you see that
depending on whether if you want to give the user
a different print statement based on their choice,
then you would use if, elif, else,
but if you actually
just wanted to give them the same feedback,
which is Game Over, then you could just use if and else.
Now let's run our game and just make sure that it works.
So I'm going to go left because I know it continues,
and then I'm going to wait for the boat,
and that allows me to get to the final condition.
And if I choose yellow, I know that I'll win.
Now if you want to change the formatting,
because having the input at the end of the sentence
doesn't look as good as letting the user type it
on a new line like what we've got here,
then you can simply, if you remember, just add the new line
which is \n at the end of your inputs, like so.
So how did you get on with this challenge?
And if you didn't manage to do it right
or if you've done it right, go back and try and modify it.
See if you can add some more conditions
or see if you can make the Game Over messages
a little bit more interesting than what I've written.
See if you can modify and make it really your own.
Have fun with that.
And in the next lesson,
I've got more exciting things coming up for you.
So hopefully you've scheduled some time for tomorrow
to complete the next day's lessons.
But for now, it's goodnight for me.