In this chapter, we are going to make a Hangman game. This game is more complicated than our previous game, but it is also much more fun. Because the game is advanced, we should first
In case you've never played Hangman before, let's first learn the rules for Hangman.
In case you don't know, Hangman is a game for two people that's usually played using paper and
Here is an example of what the player might see when they run the Hangman program we will write later. The text that the player enters in shown in bold.
H A N G M A N
+---+
| |
|
|
|
|
=========
Missed letters:
_ _ _
Guess a letter.
a
+---+
| |
|
|
|
|
=========
Missed letters:
_ a _
Guess a letter.
o
+---+
| |
O |
|
|
|
=========
Missed letters: o
_ a _
Guess a letter.
r
+---+
| |
O |
| |
|
|
=========
Missed letters: or
_ a _
Guess a letter.
t
+---+
| |
O |
| |
|
|
=========
Missed letters: or
_ a t
Guess a letter.
a
You have already guessed that letter. Choose again. Guess a letter.
c
Yes! The secret word is "cat"! You have won! Do you want to play again? (yes or no)
no
Half of the lines of code in the Hangman program aren't really code at all, but are multiline strings that use
This game is a bit more complicated than the ones we've seen so far, so let's take a
(рис 8.1) The complete flow chart for what happens in the Hangman game.
Of course, we don't have to make a flow chart. We could just start writing code. But often, once we start programming, we will think of things that need to be added or changed that we hadn't considered before. We may end up having to change or delete a lot of code that we had already written, which would be a waste of
The following flow chart is provided as an example of what flow charts look like and how to make them. For now, since you're just using the source code from this book, you don't need to draw a flow chart before writing code. The program is already written, so you don't have to plan anything out. But when you make your own games, a flow chart can be very handy.
Keep in mind, your flow charts don't always have to look exactly like this one. As long as you understand the flow chart you made, it will be helpful when you start coding. We'll begin with a flow chart that only has a "Start" and an "End" box, as shown in Figure 8.2:
(рис 8.2) Begin your flow chart with a Start and End box.
Now let's think about what happens when we play Hangman. First, one player (the computer in this case) thinks of a secret word. Then the second player (the person running the program) will
(рис 8.3) Draw out the first two steps of Hangman as boxes with descriptions.
But the game doesn't end after the player guesses one letter. It needs to check to see if that letter is in the secret word or not.
There are two possibilities: the letter will either be in the word or it won't be. This means we need to add two new boxes to our
(рис 8.4) There are two different things that could happen after the player guesses, so have two arrows going to separate boxes.
If the letter is in the secret word, we need to check to see if the player has
We can add boxes for those cases too. We don't need an arrow from the "Letter is in secret word" box to the "Player has run out of body parts and loses" box, because it's impossible to lose as long as you are only guessing correct letters. Also, it's impossible to win as long as you are guessing only
(рис 8.5) After the branch, the steps continue on their separate paths.
Once the player has won or lost, we'll ask them if they want to play again with a new secret word. If the player doesn't want to play again, the program will end. If the program doesn't end, we think of a new secret word, as shown in Figure 8.6:
(рис 8.6) The game ends if the player doesn't want to play again, or the game goes back to the beginning.
This flow chart might look like it is finished, but there's something we're forgetting: the player doesn't
(рис 8.7) The game does not always end after a guess. The new arrows (outlined) show that the player can guess
again.
We are forgetting something else, as well. What if the player guesses a letter that they've
(рис 8.8) Adding a step in case the player guesses a letter they already guessed.
We also need some way to show the player how they're doing. In order to do this, we'll show them the hangman board, as well as the secret word (with blanks for the letters they haven't
We'll need to update this information every time the player guesses a letter. We can add a "Show the board and blanks to the player." box to the flow chart between the "Come up with a secret word" box and the "Ask player to
(рис 8.9) Adding "Show the board and blanks to the player." to give the player feedback.
That looks good! This flow chart completely maps out everything that can possibly happen in Hangman, and in what order. Of course this flow chart is just an example-you won't really need to use it, because you're just using the source code that's given here. But when you design your own games, a flow chart can help you remember everything you need to code.
It may seem like a lot of work to sketch out a flow chart about the program first. After all, people want to play games, not look at flowcharts! But it is much easier to make changes and notice problems by thinking about how the program works before writing the code for it.
If you jump in to write the code first, you may discover problems that require you to change the code you've already written. Every time you change your code, you are taking a chance that you create bugs by changing too little or too much. It is much better to know what you want to build before you build it.
In this chapter, we are going to make a Hangman game. This game is more complicated than our previous game, but it is also much more fun. Because the game is advanced, we should first
In case you've never played Hangman before, let's first learn the rules for Hangman.
In case you don't know, Hangman is a game for two people that's usually played using paper and
Here is an example of what the player might see when they run the Hangman program we will write later. The text that the player enters in shown in bold.
H A N G M A N
+---+
| |
|
|
|
|
=========
Missed letters:
_ _ _
Guess a letter.
a
+---+
| |
|
|
|
|
=========
Missed letters:
_ a _
Guess a letter.
o
+---+
| |
O |
|
|
|
=========
Missed letters: o
_ a _
Guess a letter.
r
+---+
| |
O |
| |
|
|
=========
Missed letters: or
_ a _
Guess a letter.
t
+---+
| |
O |
| |
|
|
=========
Missed letters: or
_ a t
Guess a letter.
a
You have already guessed that letter. Choose again. Guess a letter.
c
Yes! The secret word is "cat"! You have won! Do you want to play again? (yes or no)
no
Half of the lines of code in the Hangman program aren't really code at all, but are multiline strings that use
This game is a bit more complicated than the ones we've seen so far, so let's take a
(рис 8.1) The complete flow chart for what happens in the Hangman game.
Of course, we don't have to make a flow chart. We could just start writing code. But often, once we start programming, we will think of things that need to be added or changed that we hadn't considered before. We may end up having to change or delete a lot of code that we had already written, which would be a waste of
The following flow chart is provided as an example of what flow charts look like and how to make them. For now, since you're just using the source code from this book, you don't need to draw a flow chart before writing code. The program is already written, so you don't have to plan anything out. But when you make your own games, a flow chart can be very handy.
Keep in mind, your flow charts don't always have to look exactly like this one. As long as you understand the flow chart you made, it will be helpful when you start coding. We'll begin with a flow chart that only has a "Start" and an "End" box, as shown in Figure 8.2:
(рис 8.2) Begin your flow chart with a Start and End box.
Now let's think about what happens when we play Hangman. First, one player (the computer in this case) thinks of a secret word. Then the second player (the person running the program) will
(рис 8.3) Draw out the first two steps of Hangman as boxes with descriptions.
But the game doesn't end after the player guesses one letter. It needs to check to see if that letter is in the secret word or not.
There are two possibilities: the letter will either be in the word or it won't be. This means we need to add two new boxes to our
(рис 8.4) There are two different things that could happen after the player guesses, so have two arrows going to separate boxes.
If the letter is in the secret word, we need to check to see if the player has
We can add boxes for those cases too. We don't need an arrow from the "Letter is in secret word" box to the "Player has run out of body parts and loses" box, because it's impossible to lose as long as you are only guessing correct letters. Also, it's impossible to win as long as you are guessing only
(рис 8.5) After the branch, the steps continue on their separate paths.
Once the player has won or lost, we'll ask them if they want to play again with a new secret word. If the player doesn't want to play again, the program will end. If the program doesn't end, we think of a new secret word, as shown in Figure 8.6:
(рис 8.6) The game ends if the player doesn't want to play again, or the game goes back to the beginning.
This flow chart might look like it is finished, but there's something we're forgetting: the player doesn't
(рис 8.7) The game does not always end after a guess. The new arrows (outlined) show that the player can guess
again.
We are forgetting something else, as well. What if the player guesses a letter that they've
(рис 8.8) Adding a step in case the player guesses a letter they already guessed.
We also need some way to show the player how they're doing. In order to do this, we'll show them the hangman board, as well as the secret word (with blanks for the letters they haven't
We'll need to update this information every time the player guesses a letter. We can add a "Show the board and blanks to the player." box to the flow chart between the "Come up with a secret word" box and the "Ask player to
(рис 8.9) Adding "Show the board and blanks to the player." to give the player feedback.
That looks good! This flow chart completely maps out everything that can possibly happen in Hangman, and in what order. Of course this flow chart is just an example-you won't really need to use it, because you're just using the source code that's given here. But when you design your own games, a flow chart can help you remember everything you need to code.
It may seem like a lot of work to sketch out a flow chart about the program first. After all, people want to play games, not look at flowcharts! But it is much easier to make changes and notice problems by thinking about how the program works before writing the code for it.
If you jump in to write the code first, you may discover problems that require you to change the code you've already written. Every time you change your code, you are taking a chance that you create bugs by changing too little or too much. It is much better to know what you want to build before you build it.
Для получения официальных документов о завершении программы дополнительного профессионального образования (удостоверения о повышении квалификации, дипломов о профессиональной переподготовке и MBA) необходимо предоставить:
Внимание! Вы можете не заказывать доставку бумажной версии официального документы, а скачать его в электронном виде и распечатать самостоятельно. Информация о выданном документе в течение 1 месяца загружается в Федеральную информационную систему «Федеральный реестр сведений о документах об образовании и (или) о квалификации, документах об обучении» - ФИС ФРДО.
Доступ на новый сайт осуществляется с использованием адреса электронной почты, который был указан вами при регистрации на "старом". Мы постарались перенести все ваши данные с прежнего ресурса, однако не исключена вероятность потери части информации.
При возникновении проблемы со входом, воспользуйтесь функцией сброса пароля
Если вы обнаружите несоответствия, пожалуйста, сообщите нам.