https://github.com/z-bj/number-guessing-script
Number — GUESS INC. — "guessing" 😂 Script runs in the terminal and saves the user's inputs in PostgreSQL DB until it finds the randomly generated number.
https://github.com/z-bj/number-guessing-script
bash guessinc number postgresql script vim
Last synced: 8 months ago
JSON representation
Number — GUESS INC. — "guessing" 😂 Script runs in the terminal and saves the user's inputs in PostgreSQL DB until it finds the randomly generated number.
- Host: GitHub
- URL: https://github.com/z-bj/number-guessing-script
- Owner: z-bj
- Created: 2023-02-11T10:07:15.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-03T15:45:27.000Z (almost 3 years ago)
- Last Synced: 2025-03-30T23:41:14.440Z (10 months ago)
- Topics: bash, guessinc, number, postgresql, script, vim
- Language: Shell
- Homepage:
- Size: 99.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README


# Number — Guess inc. — Script
This is a simple bash script that allows a user to play a number guessing game. The script prompts the user to enter their username, checks if the user exists in the database, and adds them to the database if they are new. Then, it generates a random number between 1 and 1000 and prompts the user to guess the number. The user is given hints and asked to guess again until they guess correctly. Once the user guesses the number correctly, their guess count and the secret number are stored in the database.
### Usage
To run the game, execute the following command in your terminal:
bashCopy code
`./guessing_game.sh`
You must have PostgreSQL installed and running, and you need to set up a database with the name "number_guess". The script assumes that the database has a "players" table with "user_id" (serial), "username" (text), and "timestamp" (timestamp) columns, and a "games" table with "game_id" (serial), "user_id" (integer), "secret_number" (integer), "number_of_guesses" (integer), and "timestamp" (timestamp) columns.
### Script
``` bash
#!/bin/bash
# variable to query the database
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# promp player for username
echo -e "\nEnter your username:"
read USERNAME
# get username data
USERNAME_RESULT=$($PSQL "SELECT username FROM players WHERE username='$USERNAME'")
# get user id
USER_ID_RESULT=$($PSQL "SELECT user_id FROM players WHERE username='$USERNAME'")
# if player was not found
if [[ -z $USERNAME_RESULT ]]
then
# greet gamer
echo -e "\nWelcome, $USERNAME! It looks like this is your first time here.\n"
# add player to database
INSERT_USERNAME_RESULT=$($PSQL "INSERT INTO players(username) VALUES ('$USERNAME')")
else
GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM games LEFT JOIN players USING(user_id) WHERE username='$USERNAME'")
BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games LEFT JOIN players USING(user_id) WHERE username='$USERNAME'")
echo Welcome back, $USERNAME\! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.
fi
# generate random number between 1 and 1000
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
# variable to store number of guesses/tries
GUESS_COUNT=0
# prompt first guess
echo "Guess the secret number between 1 and 1000:"
read USER_GUESS
# loop to prompt user to guess until correct
until [[ $USER_GUESS == $SECRET_NUMBER ]]
do
# check guess is valid/an integer
if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
then
# request valid guess
echo -e "\nThat is not an integer, guess again:"
read USER_GUESS
# update guess count
((GUESS_COUNT++))
# if its a valid guess
else
# check inequalities and give hint
if [[ $USER_GUESS < $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
read USER_GUESS
# update guess count
((GUESS_COUNT++))
else
echo "It's lower than that, guess again:"
read USER_GUESS
#update guess count
((GUESS_COUNT++))
fi
fi
done
# loop ends when guess is correct so, update guess
((GUESS_COUNT++))
# get user id
USER_ID_RESULT=$($PSQL "SELECT user_id FROM players WHERE username='$USERNAME'")
# add result to game history/database
INSERT_GAME_RESULT=$($PSQL "INSERT INTO games(user_id, secret_number, number_of_guesses) VALUES ($USER_ID_RESULT, $SECRET_NUMBER, $GUESS_COUNT)")
# winning message
echo You guessed it in $GUESS_COUNT tries. The secret number was $SECRET_NUMBER. Nice job\!
# some comment
```