Skip to content

A terminal-based number guessing game built using Bash scripting and PostgreSQL. User data is saved in a PostgreSQL database, and the game logic is implemented in Bash. The project includes a database dump (number_guess.sql) and a script file (number_guess.sh).

Notifications You must be signed in to change notification settings

davidecarluccio/number-guessing-game

 
 

Repository files navigation

Build a Number Guessing Game

This is one of the required projects to earn your certification. For this project, you will use Bash scripting, PostgreSQL, and Git to create a number guessing game that runs in the terminal and saves user information.

This course runs in a virtual Linux machine using Gitpod. Follow these instructions to start the course:

  1. Create a GitHub account if you don't have one
  2. Click the start button below
  3. Login to Gitpod with your GitHub account if you aren't already
  4. Once the virtual Linux machine is finished loading, start the CodeRoad extension by:
    • Clicking the "hamburger" menu near the top left of the VSCode window,
    • Going to the "View" menu,
    • Clicking on the "Command Palette" option,
    • and running the "CodeRoad: Start" command
  5. Follow the instructions in CodeRoad to complete the course

Complete both steps below to finish the challenge.


Step 1: Complete the project

The project runs in a virtual machine, complete the user stories described in there and get all the tests to pass to finish step 1.

Important: After you pass all the project tests, save a dump of your database into a number_guess.sql file, as well as your number_guess.sh file, so you can complete step 2. There will be instructions on how to do that within the virtual machine.

Clicking the button below will start a new project. If you have previously started the Build a Number Guessing Game course, go to your Gitpod dashboard to continue.

Learn more about Gitpod workspaces.

If you log out of freeCodeCamp before you complete the entire Build a Number Guessing Game course, your progress will not be saved to your freeCodeCamp account.

Open file solution.txt To follow my terminal command. Solutions divided into several steps :

1. Prepare database.
2. Primary Key and Foreign Key assignment.
3. Compact sql db schema into number_guess.sql.
4. Prepare git directory.
5. Prepare shell script file.
6. Copy scipt line by line, meanwhile commit changes on terminal.
7. Run The Script.

Instructions

To complete this project, you need to write a script that generates a random number between 1 and 1000 for users to guess. Create a number_guess database to hold the information suggested in the user stories. Connect to the interactive psql shell with psql --username=freecodecamp --dbname=postgres to create the database. In your script, you can create a PSQL variable for querying the database like this: PSQL="psql --username=freecodecamp --dbname=<database_name> -t --no-align -c". Your script should only ask for input from the user to get the username and to take guesses. The tests will add users to your database when the script has that ability, feel free to delete those. Some script related user stories may not pass until the script is completely working. Don't forget to commit your work frequently.

Notes: If you leave your virtual machine, your database may not be saved. You can make a dump of it by entering pg_dump -cC --inserts -U freecodecamp number_guess > number_guess.sql in a bash terminal (not the psql one). It will save the commands to rebuild your database in number_guess.sql. The file will be located where the command was entered. If it's anything inside the project folder, the file will be saved in the VM. You can rebuild the database by entering psql -U postgres < number_guess.sql in a terminal where the .sql file is.

If you are saving your progress on freeCodeCamp.org, after getting all the tests to pass, follow the instructions above to save a dump of your database. Save the number_guess.sql file, as well as the final version of your number_guess.sh file, in a public repository and submit the URL to it on freeCodeCamp.org.

CodeRoad

  1. Create a number_guessing_game folder in the project folder for your program.
  2. Create number_guess.sh in your number_guessing_game folder and give it executable permissions.
  3. Your script should have a shebang at the top of the file that uses #!/bin/bash.
  4. Turn the number_guessing_game folder into a git repository.
  5. Your git repository should have at least five commits.
  6. Your script should randomly generate a number that users have to guess.
  7. When you run your script, you should prompt the user for a username with Enter your username:, and take a username as input. Your database should allow usernames that are 22 characters.
  8. If that username has been used before, it should print Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses., with <username> being a users name from the database, <games_played> being the total number of games that user has played, and <best_game> being the fewest number of guesses it took that user to win the game.
  9. If the username has not been used before, you should print Welcome, <username>! It looks like this is your first time here.
  10. The next line printed should be Guess the secret number between 1 and 1000: and input from the user should be read
  11. Until they guess the secret number, it should print It's lower than that, guess again: if the previous input was higher than the secret number, and It's higher than that, guess again: if the previous input was lower than the secret number. Asking for input each time until they input the secret number.
  12. If anything other than an integer is input as a guess, it should print That is not an integer, guess again:
  13. When the secret number is guessed, your script should print You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job! and finish running.
  14. The message for the first commit should be Initial commit.
  15. The rest of the commit messages should start with fix:, feat:, refactor:, chore:, or test:.
  16. You should finish your project while on the main branch, your working tree should be clean, and you should not have any uncommitted changes.

Step 2: Submit your code

When you have completed the project, save all the required files into a public repository and submit the URL to it below.

Required files: number_guess.sql, number_guess.sh

About

A terminal-based number guessing game built using Bash scripting and PostgreSQL. User data is saved in a PostgreSQL database, and the game logic is implemented in Bash. The project includes a database dump (number_guess.sql) and a script file (number_guess.sh).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%