Loading…
![]() String updated in the repository |
|
![]() Source string changed |
|
![]() Automatically translated |
|
![]() String added in the repository |
|
Things to check
Placeholders
The following format strings are missing: ## , The following format strings are missing: `game_over = {ask} 'Did you win?'`, The following format strings are missing: `game_over = {call} detect_winner {with} field, sign`, The following format strings are missing: `game_over`, The following format strings are missing: `print_field`
The following format strings are extra: `{elif}`, The following format strings are extra: `{while}`
Key
adventures->tic->levels->17->story_textLabels
Automatically translatedFlags
ignore-double-space, ignore-ellipsis, ignore-inconsistent, ignore-long-untranslated, ignore-same, ignore-translated, placeholders:r"`([^`\n\r]+)`|## ", python-brace-format
Now that we have the {elif} command, we can let the game decide if someone has won and it can stop asking us!
### Exercise
***Paste your code*** Paste your code from the previous level here and make it level 17 proof. In this level you've learned to use a colon everytime you create a block of code. Please add the colons in the correct spots.
***Create a function that detects if someone's won*** We have started the function for you, paste it under the function `print_field` and finish the function. You can see that this first piece of code checks to see if spot 1, 2 and 3 are the same, because if they are you have 3 in a row.
It also checks if they are not a dot, because if they are, the line might have the same symbols on them, but that's just because it's still empty.
If all these conditions are met, the game is over and the winner is printed.
Finish this function with all possible ways to win. This means you have to make this for the other 2 rows, 3 columns and 2 diagonals.
If you've finished all the other options, the function should return the variable `game_over` so we can use it in our game.
***Call the function in the game*** Go to the line `game_over = {ask} 'Did you win?'` and change it to `game_over = {call} detect_winner {with} field, sign`. Now the function will check if there's a winner and the game doesn't need to keep asking anymore!
***Enjoy your game!*** Great job! You have finished the game! Enjoy playing it!
This made the game playable, but also quite slow. In this level we have learned the `{elif}` command, that could solve that problem.
To use the `{elif}` to let Hedy check whether or not a player has won is fun, but it also requires a fair amount of extra lines.
In the example code you see that we added the requirements to win (3 spots in a row horizontally, vertically or diagonally).
You can find them in line 46 to 69. You can see that each time the requirement is met (so if a player has won) the variable game is set to 'over'.
This means that the `{while}` loop will be stopped and the game ends.