Tell us what’s happening:
Why two test failed? Number 8 and number 13, no matter what I try. Very annoying…
Your code so far
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# ASK FOR THE NAME
echo Enter your username:
read USERNAME
PLAYER=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
# IF THE NAME IS NEW
if [[ ! -z $PLAYER ]]
then
# WELCOME THE USER
GAMES=$($PSQL "SELECT COUNT(games_id) FROM games WHERE user_id=$PLAYER")
BEST_GAME=$($PSQL "SELECT MIN(try_number) FROM games WHERE user_id=$PLAYER")
echo Welcome back, $USERNAME! You have played $GAMES games, and your best game took $BEST_GAME guesses.
else
# CREATE A NEW USER
NEW_PLAYER=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME')")
PLAYER=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
echo Welcome, $USERNAME! It looks like this is your first time here.
fi
# START THE GAME
NUMERO=$(( RANDOM % 1000 + 1 ))
# RECORD THE SECRET NUMBER IN THE GAME
NEW_GAME=$($PSQL "INSERT INTO games(user_id, secret_number, try_number) VALUES($PLAYER, $NUMERO, 0)")
NEW_GAME=$($PSQL "SELECT games_id FROM games ORDER BY games_id DESC LIMIT 1")
# GET THE INPUT
echo Guess the secret number between 1 and 1000:
read THE_NUMBER
NUMBER_TRY=1
until [[ $THE_NUMBER -eq $NUMERO ]]
do
UPDATE=$PSQL "UPDATE games SET try_number=$NUMBER_TRY WHERE games_id=$NEW_GAME"
if [[ ! $THE_NUMBER =~ ^[0-9]+$ ]]
then
echo That is not an integer, guess again:
read THE_NUMBER
# IF THE INPUT IS LOWER THAN THE NUMBER
elif [[ $THE_NUMBER -gt $NUMERO ]]
then
echo It\'s lower than that, guess again:
read THE_NUMBER
NUMBER_TRY=$((NUMBER_TRY + 1))
# IF THE INPUT IS HIGHER THAN THE NUMBER
elif [[ $THE_NUMBER -lt $NUMERO ]]
then
echo It\'s higher than that, guess again:
read THE_NUMBER
NUMBER_TRY=$((NUMBER_TRY + 1))
fi
done
$PSQL "UPDATE games SET try_number=$NUMBER_TRY WHERE games_id=$NEW_GAME"
echo You guessed it in $NUMBER_TRY tries. The secret number was $NUMERO. Nice job!
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15
Challenge Information:
Number Guessing Game - Build a Number Guessing Game