Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:
I’ve been stuck on the Number guessing game project in the Relational databases course for a couple of days now. SUBTASKS 1.1 :7 Your script should prompt for a username and every other task under it are unticked even though my script does every thing correctly when I test it manually. I played around with the code multiple times, changed how things work, changed the format of the database, etc. Any help on this would be greatly appreciated.

Here’s my code so far:

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

# Login screen
LOGIN_SCREEN () {
  # If function called with argument
  if [[ $1 ]]
  then

    # Print argument
    echo -e "\n$1"
  fi

  # Ask for username
  echo -e "\nEnter your username:"
  read USERNAME

  # If username less than 22 characters
  if [[ ${#USERNAME} -lt 22 ]]
  then 

    # Return to login with error message
    LOGIN_SCREEN "\nUsername has to be at least 22 characters"
    return
  fi

  # Get user_id from database
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$USERNAME'")

  # If doesn't exist
  if [[ -z $USER_ID ]]
  then

    # Welcome user
    echo -e "\nWelcome, $(echo $USERNAME | sed -E 's/^ *| *$//g')! It looks like this is your first time here."

    # Add to database
    ADD_USER_RESULT=$($PSQL "INSERT INTO users(name) VALUES('$USERNAME')")

    # Fetch new user_id
    USER_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$USERNAME'")
    
    # Go to game
    GAME "$USER_ID" "$USERNAME" "0" "0"
    return


  fi
  # Fetch games_played and best number of guesses from database
  USER_INFORMATION=$($PSQL "SELECT games_played, best_game FROM users WHERE user_id = $USER_ID")
  
  IFS="|" read -r GAMES_PLAYED BEST_NUMBER_GUESSES <<< $USER_INFORMATION

  # Display user information
  echo -e "\nWelcome back, $(echo $USERNAME | sed -E 's/^ *| *$//g')! You have played $(echo $GAMES_PLAYED | sed -E 's/^ *| *$//g') games, and your best game took $(echo $BEST_NUMBER_GUESSES | sed -E 's/^ *| *$//g') guesses."

  # Go to game
  GAME "$USER_ID" "$USERNAME" "$GAMES_PLAYED" "$BEST_NUMBER_GUESSES"
}

GAME () {
  USER_ID=$1
  USERNAME=$2
  GAMES_PLAYED=$3
  BEST_NUMBER_GUESSES=$4
  GUESSES=0


  # Generate random number between 1 and 1000 
  NUMBER_TO_GUESS=$(( $RANDOM % 1000 + 1 ))

  # Display game message
  echo -e "\nGuess the secret number between 1 and 1000:"

  # Input while loop
  while true
  do
    # Increment number of guesses
    let "GUESSES++"

    # Ask for user input
    read NUMBER

    # If input isn't an integer 
    if [[ ! $NUMBER =~ ^[0-9]+$ ]]
    then 

      # Print error message
      echo -e "\nThat is not an integer, guess again:"

      # Ask for input again
      continue
    fi

    # If input is higher than number
    if [[ $NUMBER -gt $NUMBER_TO_GUESS ]]
    then

      # Display appropriate message
      echo -e "\nIt's lower than that, guess again:"

      # Ask for input again
      continue
    fi

    # Else if input is lower than number
    if [[ $NUMBER -lt $NUMBER_TO_GUESS ]]
    then

      # Display appropriate message
      echo -e "\nIt's higher than that, guess again:"

      # Ask for input again
      continue
    fi

    # Display congrats message and exit the loop
    echo -e "\nYou guessed it in $(echo $GUESSES | sed -E 's/^ *| *$//g') tries. The secret number was $(echo $NUMBER_TO_GUESS | sed -E 's/^ *| *$//g'). Nice job!"

    # Add game to database
    INSERT_GAME_RESULT=$($PSQL "INSERT INTO games(user_id, number_guesses) VALUES($USER_ID, $GUESSES)")

    # If number of games played is zero or this game has lower guesses
    if [[ $GAMES_PLAYED = 0 ]] || [[ $GUESSES -lt $BEST_NUMBER_GUESSES ]]
    then

      # Update user's best_game
      UPDATE_BEST_GAME_RESULT=$($PSQL "UPDATE users SET best_game = $GUESSES WHERE user_id = $USER_ID")
    fi
    # Insert incremented number of games to database
    INCREMENT_NUMBER_GAMES=$($PSQL "UPDATE users SET games_played = games_played + 1 WHERE user_id = $USER_ID")
    

    break
  done

}

LOGIN_SCREEN "~~~~ Welcome to the number guessing game ~~~~"

And here’s a link to the GitHub repo in case you need the database dump file.

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Number Guessing Game - Build a Number Guessing Game

Link to the challenge:

Hey @eslamallam73 - sounds like the user story isn’t clear enough. The main issue is that you are requiring the name to be at least 22 characters. The username the tests use is user_${Date.now()}, which is actually 18 characters, e.g. user_1658763060295. Not sure why I put 22 characters - but someone had a similar issue where they only allowed names up to 10 characters or something, so I figured that I could mention that you should allow names up to 22 characters - that isn’t clear enough, or accurate really. I changed your script so that it doesn’t have a limit on the username length, and removed the username check in your database and that got many of the tests to pass. You could do that, or probably change it to 18 and that would work.

After you change that, there’s another bug with a few of the tests - the ones that start with If that username has been used before, it should print... and When the secret number is guessed, your script should print... The tests are being a little goofy with your script and don’t quite work all that great. They should all pass if you run the tests a bunch of times - until the tests get what they are looking for. I will look into it and see if I can improve those tests.

Let us know if this works for you :+1:

2 Likes

First off, thanks alot for your very early reply again! I made the modifications you mentioned and successfully passed all tests, finally earning my first FCC certification! I believe the problem lies with the wording of the user story as I believed that “at least 22 characters” implied a minimum length of 22. It could be just me as I’m not a native speaker but maybe a different choice of words would be better overall to be understood by a larger audience and avoid misunderstandings like mine. Thanks again for your reply and I greatly appreciate all your efforts. Cheers mate :D.

2 Likes