All right guys. So before we get started with our project,
it's really important that we're all on the same page and we understand how the
code is going to work because we know how the game works.
So I recommend that if you've never heard of this game hangman,
then before you continue to just head over to the Wikipedia article on hangman,
and it might be even easier if you select your own language
because I know that it has a different name in different languages. Now,
if you've never played this game,
I always recommend just go and give it a go online.
So a link to this address, and if you click on single player untimed,
you can see that you have a word that you have to guess,
and then you start picking some letters. And if it's right,
then it goes in and if it's wrong,
then the hangman gallows start being drawn and our little man is at risk of
being hung. Once you understand how the game works,
it's time for us to examine what the logic for our program is going to look
like. And to do that, we're going to use flow chart programming.
So it's very simple.
It just means that we can draw out a flow chart to represent the logic of our
game. At this point,
I want you to pause the video and have a quick think about how you would program
hangman.
Try and come up with a flow chart that represents the logic of this game.
Break it down into little steps. What needs to happen first,
what needs to happen when the user makes a guess, and what should happen when the
guest is incorrect, when should the game end? Pause the video
now, take a pencil and a piece of paper, or use draw.io to create your flowchart.
Right?
So how did that go? Did you break down the problem?
It's really important to say that there's no single right answer here.
And there's loads of ways that you could have done this,
but let me show you my approach,
which is how we'll write our program over the next couple of lessons.
So this is what it looks like from start to end.
And if we zoom in a bit, let's go through this flow chart
step-by-step so we're all on the same page. At the start of the game
the first thing we do is to generate a random word. Let's say, in this case
it was the word mouse.
Now the next step is to generate as many blanks as the letters in the word.
So we have these blanks to show the user
how many letters are in this word that they need to guess. They don't get to see
this, but they get to see this. And the next step they get to
guess a letter.
So let's say the user in this case guessed the letter O. Well,
the next thing we have to check is, is the guessed letter actually in the word?
Well, in this case, o it is indeed in mouse.
It's the one at the second position.
And so we go down this path and we answer yes to that question.
So now we to replace that blank with the letter O.
Now the next stage is we have to check are the blanks filled, are all of these
blanks removed because in that case,
it means the user is guessed all of the letters correctly? Well at the moment,
it's definitely a no, cause we've only guessed one letter.
So then we go all the way back to the beginning and we ask the user to guess
another letter. Now at some stage of the game,
all the letters will be guessed. And then we can answer yes to this question,
which means it's game over. The user has won because they've guessed all the
letters correctly. Now,
if we go back to the part where we ask the user to guess a letter,
and they guessed a letter, say the letter z
which doesn't actually exist in this word mouse, while then in this case,
we go down this right path to answer no
and the user loses a life,
which means we start drawing out a hangman figure. Now,
depending on whether if they have run out of lives,
if the hangman man has been drawn completely, then they would go down this path.
But at the moment they've only lost one life and this figure is not complete
so the answer is no.
And that means we go back to the beginning and we get them to guess the next
letter. Now,
if at the stage where they've exhausted all of their lives and the hangman
figure is complete, well, then it's the end of the game
and they've basically just lost without having guessed
all of the letters in the word. So this is the logic,
and you might need to come back to this video a few times
if you get stuck along the way just to review how the program flows.
But it might also be worth creating your own flow chart or simply downloading
this completed flowchart I have here in the course resources.
It's a really useful tool to come back to and refer to as you're writing the
code,
because this will give you an overview of what you're trying to get your code to
do.
And hopefully this is going to make it easier for you to complete the upcoming
challenges in this project. Once you are ready, head over to the next lesson,
and let's get started.