guess-the-number

GUESS-THE-NUMBER!

Guess-The-Number is a Python based application fully run within the terminal.

This app runs 3 core programs:

  1. Guess-The-Number: The Player has to guess a randomly generated number.
  2. Guess-The-Number (AI): Same as above, except this time, the computer has to try to guess the player’s number!
  3. Coin-Toss: A bonus program that simulates ‘flipping a coin’ with the computer.

This application explores the posibilites of automation with Python, handling user inputs and random outcomes through the use of classes and functions.

The desired goals from the perspective of myself and for the player reflects this. I want to demonstrate what can be done with Python through the aim of creating 3 programs that can simulate random game events that the user is likely to have played in real life, whether attempting to guess a number that their friend was thinking of to flipping a coin to decide on who goes first in a game.

Guess-The-Number is deployed on Heroku.

Live Link to the app on Heroku here: Guess-The-Number


Application across difference platforms

Responsiveness Check

Note: The application is run in a ‘mini terminal’ in the browser through Heroku. Therefore, projecting the terminal to showcase the python logic at work is more important that ensuring the width of the ‘screen’ is aesthetically pleasing across all devices.

Application in action

Game1 GIF

Game2 GIF

Game3 GIF


Table of Contents

  1. Wireframe and Flow charts
  2. UX Description
  3. Technologies used
  4. Application Features
  5. Features to implement
  6. Testing and Validation
  7. Bugs and Issues
  8. Peer Reviews
  9. Deployment
  10. Credits

Wireframe

I used this model as a template for me to visualise how I was going to go about making this application. You can see in the original flow chart I didn’t come up with the option to exit the program yet. The coloured asterisks are meant to help visually guide where you are taken back to at the end of round depending on whether you want to play again or exit the game.

Program flow charts:


UX Description

How to play the game:

Guess-The-Number is an input based application, where depending on assigned input options, the player can make guesses or make selections on what game they’d like to play.

In the first game, Guess-The-Number, the player must try to guess a number between 1-100, which the computer has “thought off”. The player can enter a number one at a time, and they will get feedback from the terminal on whether their guess was higher or lower than the computer’s number. When they guess correctly, they will be told along with the number of attempts it took to guess it.

A prompt then encourages the player to either play again, or, go back to the main menu.

In the second game, Guess-The-Number (AI), the player now calls the number they’re thinking of between 1-100. The computer makes guesses and the player provides feedback on whether the guess is too low, high or spot on. This is done by entering 1 for too low, 2 for too high and 3 for a correct guess. The computer then prints how many attempts it took to guess the player’s number.

A prompt then encourages the player to either play again, or, go back to the main menu.

In the third and final game, Coin-Toss, the player hits enter to begin and is asked to call Heads or Tails by selecting 0 or 1 on their keyboard respectively.

The computer will “flip a coin” and the result of the coin toss is printed. Whilst this is predominantly a player vs computer coin toss, you could use this program to make a traditional coin toss decision as well!

As per the previous games, a prompt then encourages the player to either play again, or, go back to the main menu.

I’ve used colorama to provide more visual cues as user feedback when they engage with the application. Typically, the colour red will indicate an error, yellow will indicate a prompt to action or a warning and green indicates a round win or end of round message. I use cyan and magenta for some other prompts including too high or too low guesses in the second game and for the goodbye message if you exit the application.

User Stories


Technologies and Libraries Used


Application Features

This application comprises of 3 core programs:

  1. A guessing game (Player)
  2. Another guessing game (Computer)
  3. A ‘Coin-Toss’ program

At the beginning of the application, there is a welcome message/main menu that also re-prints at the end of a game round should the player wish to go back to the main menu:

Welcome/Menu

First Game Option: Guess-The-Number. The player will be greeted with the start of the game like this:

Game1 start

When a player makes a guess, which will most likely be incorrect on first guess, a response message is printed in the terminal which will influence the player’s next guess:

Too High

Too Low

With each passing guess in the round, where the player doesn’t successfully guess the correct number, the previous guesses are printed in a list so that the player doesn’t forget/guess numbers that they have already guessed previously:

Previous Guesses

When the player guesses the number correctly, the following will print to the terminal:

Correct!

If you press x, you will return to the start of game 1 (Guess-The-Number):

Press-x

If you press c, you will return to the menu and you will have the ability to start a new game or exit the application:

Press-c

Second Game Option: Guess-The-Number (AI). The player will be greeted with the start of the game like this:

Game2 start

In this demo, I have decided to pick the number 75. As you can see below, the computer has made its first guess that is too low - the user would press 1 to indicate this to the computer:

Too Low

Now, in the computer’s second attempt, the computer has made a guess that is too high. The user will now feedback to the computer that this is the case by entering the number 2 where directed:

Too High

Eventually, assuming of course that the player informs the computer in good faith, the computer will guess the correct number, and when this happens, the player should press the number 3 to confirm that their number has been guesses. A statement is then printed to say how many attempts it took for the computer to guess the correct number:

Correct!

The player, as per the first game, is presented with the option to play again (press x) or go back to the menu and exit the current game (press c).

If they press x:

Press-x

And if they press c: Press-c

Third Game Option: Coin Toss The player will see this message at the start of the program:

Game3 start

When the player presses Enter, there will be a prompt that asks the player to pick heads or tails by entering 0 or 1 respectively:

Heads or Tails?

If the player enters 0 (Heads) - one of two options will print:

If they call heads and it lands on tails:

heads-tails

If they call heads and it lands on heads:

heads-heads

If the player enters 1(Tails) - one of two options will print:

If they call tails and it lands on heads:

tails-heads

If they call tails and it lands on tails:

tails-tails

At the end of the round, as per the previous games, the player is always presented with the option to play again or exit the current game and return to the menu.

If they press x:

Press-x

And if they press c:

Press-c

Error/Invalid input messages:

In this application, there are various different messages to inform the user that they have made a mistake and entered an invalid option.

In the main menu, you will see a message like this printed if you enter an invalid input:

Menu Input error

In game1 (Guess-The-Number), these are the errors you will find if you enter incorrect integers, strings or floating point numbers:

Incorrect Integer Incorrect Input: String Incorrect Input:Float

If you enter an invalid option at the end of a round to either play again or return to menu, such as entering a number, incorrect string or float, you’ll find an error message like this:

Go Back Input error

In game2 (Guess-The-Number (AI)), these are the errors you will expect by entering incorrect integers, strings or floating point numbers:

game2 start errors

Similarly, if you don’t input correct feedback options, you’ll be presented with errors:

Too-high Too-low errors

As above, if you enter incorrect inputs at the end of a round, errors will show as below:

Go Back Input errors

In game3 (Coin-Toss), these are the errors you will see by entering incorrect inputs where asked:

Heads or Tails errors

At the end of a round of Coin-Toss, if you input incorrect options, you’ll get messages like this:

Go Back Input errors

Specific errors for miss-use of Guess-The-Number (AI)

It might be obvious to you after looking at the second game that a player might lie about a number they’ve entered and either say it’s too high/low when it’s correct or press too low when the number is higher or vice versa.

There are some catches for this scenario, as I’ll demonstrate below:

If a player sees that the computer has correctly guessed the number, and lies, there is a fallback piece of code that will register this and print the following message:

Lying about correct number

If a player intentially keeps lying about whether the number that the computer guesses is higher or lower, the computer will catch on eventually and print the following message:

Not playing properly


Other Features to Implement

Interesting Coin-Toss function

Testing and Validation

I ran my code through a PEP8 Validator and it passed with no issues/warnings: PEP8 Validator PEP8-check

To test my application, I did my preliminary checks as I went along in the terminal in Gitpod using many print() statements in various sections of code. I did further testing in the browser on Mac through a deployed link through Heroku to make sure all output was working as expected. This did indeed lead me to make adjustments particularly in print statement spacing and the clear terminal function.

Any indentation or miss-types were also corrected as I went along, and individual or small pieces of code were usually tested in Google Collab first before being segmented in to the run.py file, particularly if I felt that what I was writing was stretching my comfort zone.

I checked to make sure the game ran in correct order by making first running small sections of the game followed by the whole game when it was near completion.

To test game1, game2 and game3:

All inputs were tested in the gitpod terminal meticulously during the process of the application build. I would estimate several hours worth of testing on inputs alone to make sure they behaved as expected.

I checked for the following:

I am happy to say I am pleased that all inputs work well. The same rigorous testing would be applied to any future input options added to the application.

I have also run the program on mobile. Personally, I ran the game on iPhone 5, X and 12 with no obvious issues. Aside from aesthetics in the ‘terminal view port’ width and layout, I’m happy to say the project is running well across different media. As well as different media, I ran the project on 2 different browsers, Google Chrome and Firefox. Again, I am happy with performance across both.


Bugs and Issues


Peer Reviews

To get some peer reviews for this project, I went to the following:

I had my friend Nickolay, who is fantastic with Back-end languages, review my code and during the project he has pushed me to do better any time I’ve asked him to review my code. He’s helped me solve issues/bugs his great understanding of Python.

I submitted my project to the peer-code-review channel in the C.I Slack community and received the following feedback:


Deployment

Follow the steps below to see how to deploy a repository through Heroku:

Build an app in Heroku:

To build an app in Heroku, follow these steps:

  1. Open the Heroku site, login and access your dashboard. In the top right hand corner of the website, click on the “New” button and select “create new app”. Heroku Home

  2. You will be brought to a new page where you can enter the name of your app and the region you are based in. Once you do this, click on the “create app” button: Create New app

  3. Once you do this, you will be brought to the app-specific dashboard. Heroku Pipeline

  4. Make sure you connect/link your github account for ease of deployment and then head to the settings tab to configure vars and install required buildpacks: Settings
    • You will want your config vars to have the key and value –> PORT and 8000
    • Buildpacks required will be Python and Node.JS in that order Config Vars and Buildpacks
  5. Once this is done, you are ready to begin deploying your application!

Steps to deploy your app to Heroku:

  1. Login to Heroku in the terminal by entering the command heroku login -i followed by your login details.
  2. Retrieve your application’s name from heroku and enter heroku apps.
  3. Set the heroku remote: (make sure you replace the with your actual application name) heroku git:remote -a
  4. Next, as per a usual deployment to github, enter the following commands with a similar message: git add. (followed by –>) git commit -m “Deploy to Heroku through CLI” (followed by –>) git push origin main (followed by –>) git push heroku main

Alternatively, if available, you can also enable auto or manual deployments if you link your github account and repository to your Heroku account. This enables much faster linked deployments without using the CLI.


Credits

I sought inspiration from many different websites/sources for this project. With many thanks to the following:

I couldn’t have done this project without the help of some key individuals:


Return to top