Im buildind the “number guessing game project” from the relational database course. The problem is that my tests doesn’t pass even though the code is working perfectly fine and giving the expect behaviour.
These are the tests that doesn’t pass:
-
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 -
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
Also, sorry for my bad english, it isn’t my main language
This is my .sh file:
#!/bin/bash
PSQL=“psql --username=freecodecamp --dbname=number_guess -t --no-align -c”
MAIN(){
echo “Enter your username:”
read USERNAME
CHECK_USER=$($PSQL "SELECT id FROM users WHERE username = '$USERNAME'")
if [[ $CHECK_USER ]]; then
GAMES_PLAYED=$($PSQL "SELECT COUNT(user_id) FROM games WHERE user_id = $CHECK_USER")
BEST_GAME=$($PSQL "SELECT MIN(tries) FROM games WHERE user_id = $CHECK_USER")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
echo "Welcome, $USERNAME! It looks like this is your first time here."
$PSQL "INSERT INTO users(username) VALUES('$USERNAME')"
CHECK_USER=$($PSQL "SELECT id FROM users WHERE username = '$USERNAME'")
fi
MENU
}
MENU(){
RANDOM_NUMBER=$((1 + $RANDOM % 1000))
TRIES=0;
GUESSED=0;
echo -e “Guess the secret number between 1 and 1000:”
while [[ $GUESSED = 0 ]]; do
read GUESS_NUMBER
if [[ ! $GUESS_NUMBER =~ ^[0-9]+$ ]]; then
echo -e "That is not an integer, guess again:"
elif [[ $RANDOM_NUMBER = $GUESS_NUMBER ]]; then
TRIES=$(($TRIES + 1))
echo -e "You guessed it in $TRIES tries. The secret number was $RANDOM_NUMBER. Nice job!"
$PSQL "INSERT INTO games(user_id, tries) VALUES($CHECK_USER, $TRIES)"
GUESSED=1;
elif [[ $RANDOM_NUMBER -gt $GUESS_NUMBER ]]; then
TRIES=$(($TRIES + 1))
echo -e "It's higher than that, guess again:"
else
TRIES=$(($TRIES + 1))
echo -e "It's lower than that, guess again:"
fi
done
}
MAIN