Wednesday, March 10, 2021

Project 4: 3 times the... GAME OVER

Welcome back to my blog for this month's post!  Somehow, I've just realized that I didn't need to rewrite the post titles each time!  😅  Previews are a lot more valuable than I initially thought...  Anyway, this is my third and FINAL post in what ended up becoming my Guess the Number series.  If you would like a re-cap for my previous Guess the Number posts, here is Project 2 and Project 3.  

As you may have guessed from the title, I will talk about how I implemented a "game over" after a limited number of guesses.  My previous posts introduced codes that would allow users to make guesses as to what number the program has chosen, but the user was given an unlimited number of guesses.  Many people who have ever played games would know that a game with no changes in difficulty, limitations or consequences can become quite dull after a few playthroughs.  In this final instalment of Guess the Number, I will introduce two codes that give the user three guesses, and if the user gets them all wrong, the game will declare a game over.  

I won't be introducing any new types of functions and operators this time, but rather use nested if statements and add extra lines defining the conditions.  There are two versions of the updated game.  In both versions, if a number below 1 or above 10 was entered, the response will be an error message suggesting that they have guessed a number outside of the range.  However, in the first version the user will lose a guess if they type in a number outside of the range and in the second version they will not.  I decided to write about both versions of the game in this post because I found it interesting that despite the first version arguably being the harder game, it was actually the easier code to plan for.  

First version code:  You can view the code below by either downloading the PDF from the link or by looking at the image posted here.

Code PDF:  Loss of chances 


Here's a list of statements that I've added to implement a game over:

  • c=3
    • Allows Python to define c as 3
    • The user has three chances to guess the correct number 
  • c=c-1
    • Every time the loop is repeated, the new c is minus 1 of the old c
    • The user originally has three chances.  If they get one guess wrong, they lose one chance and is left with two chances.  If they get another guess wrong, they lose another chance and is left with one chance...
  • if c>0
    • As long as the user hasn't run out of chances, they will receive a statement regarding their input (and the number of chances they have left)
  • else
    • In this case, if c is 0 or smaller
    • When the user runs out of guesses, they will receive a "GAME OVER"
First version playthroughs:  You can view the outcome of the playthrough by downloading the PDF from the link or by looking at the image here.



















Second version code:  You can view the code below by either downloading the PDF from the link or by looking at the image posted here.

Code PDF:  No loss of chances 

 
The additional statement:
  • c=c+1
    • Before the loop c=3
    • During the first loop c=2 because c=c-1
    • But when the user types in a number lower than 1 and above 10, c=3 again because c=(c-1)+1
Second version playthrough:  You can view the outcome of the playthrough by downloading the PDF from the link or by looking at the image here.




















See how adding a few simple equations can build a GAME OVER?  Before I started coding, I never really knew how games seemed to figure out how I made the wrong move, but at least now I see that it's more-or-less a matter of getting the program to recognize specific patterns as either "wrong" or "right."  I thoroughly enjoyed making Guess the Number minigames and I hope you enjoyed reading about my journey!  However, I would like to move on to other projects to diversify my coding skills, so stay tuned for future posts 😙

As always, let me know what you thought of my codes 😀  Do you have any better ideas as to how to make Guess the Number?  Leave comments below or if you prefer to contact me personally, check out the Contact Me page.  👍

The next post will NOT be a project but will most likely be a storytime 😋  Nevertheless, it is something that I have planned to write about for some time now and I hope it will be an interesting read 😉


No comments:

Post a Comment

A New Frontier: Building bots without code!!!

 Dear Readers,  Welcome back to this month's Chronicles of a Neurodivergent Programmer.  Last month, I took a break from writing about t...