Help: Relational Database Number Guess Game

Hello, I’m having trouble with one last task in the Number Guessing Game I tried a lot of different things and I still can’t get the desired output, according to the tester.

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.

My code below:

#!/bin/bash

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

get_user_info() {
  echo "Enter your username:"
  read USERNAME

  MAX_LENGTH=22
  USERNAME_LENGTH=${#USERNAME}
  if [[ $USERNAME_LENGTH -gt $MAX_LENGTH ]]; then
    echo "Error: Username cannot exceed $MAX_LENGTH characters. Please enter a shorter username."
    exit 1
  fi

  USER_INFO=$($PSQL "SELECT u.user_id, COUNT(g.game_id) AS games_played, COALESCE(MIN(g.number_of_guesses), 0) AS best_game FROM users u LEFT JOIN games g ON u.user_id = g.user_id WHERE u.username = '$USERNAME' GROUP BY u.user_id")

  if [[ -z $USER_INFO ]]; then
    echo -e "Welcome, $USERNAME! It looks like this is your first time here.\n"
    INSERT_RESULT=$($PSQL "INSERT INTO users (username) VALUES ('$USERNAME') RETURNING user_id")
    USER_ID=$(echo $INSERT_RESULT | xargs)
    GAMES_PLAYED=0
    BEST_GAME=0
  else
    IFS='|' read -r USER_ID GAMES_PLAYED BEST_GAME <<< "$USER_INFO"
    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  fi
}

start_game() {
  SECRET_NUMBER=$((RANDOM % 1000 + 1))
  GUESS_COUNT=0

  echo "Guess the secret number between 1 and 1000:"
  read USER_GUESS

  while [[ $USER_GUESS -ne $SECRET_NUMBER ]]; do
    if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]; then
      echo "That is not an integer, guess again:"
    else
      if [[ $USER_GUESS -lt $SECRET_NUMBER ]]; then
        echo "It's higher than that, guess again:"
      elif [[ $USER_GUESS -gt $SECRET_NUMBER ]]; then
        echo "It's lower than that, guess again:"
      fi
    fi
    read USER_GUESS
    ((GUESS_COUNT++))
  done

  ((GUESS_COUNT++))

  INSERT_GAME_RESULT=$($PSQL "INSERT INTO games (user_id, secret_number, number_of_guesses) VALUES ($USER_ID, $SECRET_NUMBER, $GUESS_COUNT)")

  echo "You guessed it in $GUESS_COUNT tries. The secret number was $SECRET_NUMBER. Nice job!"
}

get_user_info
start_game

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

some of the common issues people have with this project is
1- adding too much code, more than is needed (for eg if you are adding checks for username length and that was not requested or any other checks that were not explicitly defined by the project requirements)
2- not having a long enough username field (25)
3- not counting each guess correctly (or miscounting either because they didn’t count the bad guesses or they didn’t count the correct guess)
4- using too many queries which slows down execution
5- using recursion and not properly accounting for the execution which requires that you exit as soon as the person guesses correctly and the final message is printed.
6- not matching the exact strings of the prompts and responses

I’ve given you a list here of the most common issues people have. Hopefully you can go through them and see if making changes accordingly will help.

Thank you very much! I got it!!

1 Like