I need help with Build a Number Guessing Game Challenge

Can someone please tell me what’s wrong with my code?
I’ve been trying for hours and still can’t pass the 1.1:8, and 1.1:13 tests

Here’s my code:

#!/bin/bash

# Variable to handle database operations
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

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

# Function to handle the guessing game
play_game() {
    local user_id=$1
    local username=$2
    local count_tries=1
    local guess

    while true; do
        echo -e "\nGuess the secret number between 1 and 1000:"
        read guess

        if [[ ! $guess =~ ^[0-9]+$ ]]; then
            echo "That is not an integer, guess again:"
            continue
        fi

        if [[ $guess -eq $RANDOM_NUMBER ]]; then
            echo "You guessed it in $count_tries tries. The secret number was $RANDOM_NUMBER. Nice job!"
            break
        elif [[ $guess -gt $RANDOM_NUMBER ]]; then
            echo "It's lower than that, guess again:"
        else
            echo "It's higher than that, guess again:"
        fi

        ((count_tries++))
    done

    # Insert user id and number of tries into game_data table
    RESULT=$($PSQL "INSERT INTO game_data(user_id, tries) VALUES ($user_id, $count_tries)")
}



# Prompt client to enter username
echo "Enter your username:"
read USERNAME

# Try to get the user id
GET_USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME'")

if [[ -z $GET_USER_ID ]]; then
    # Create user with username entered by the user
    CREATE_NEW_USER=$($PSQL "INSERT INTO users (username) VALUES ('$USERNAME')")
    # Get the new user id
    GET_USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
    
    echo "Welcome, $USERNAME! It looks like this is your first time here."
else
    NUM_OF_TIMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM game_data WHERE user_id = $GET_USER_ID")
    BEST_GAME=$($PSQL "SELECT MIN(tries) FROM game_data WHERE user_id = $GET_USER_ID")
    
    echo "Welcome back, $USERNAME! You have played $NUM_OF_TIMES_PLAYED games, and your best game took ${BEST_GAME:-0} guesses."
fi

# Play the game
play_game "$GET_USER_ID" "$USERNAME"


can you post the details of the tests that are failing?

1 Like

Sorry

I’ve updated the post

is your algorithm counting the bad guesses (non numerical values) as guesses? you need to do that to get the correct number of guesses.

Edit: also please confirm that you are counting the last correct guess also? (you must do that too)

I don’t think it’s counting bad guesses, but I’ll check out if it’s not counting the last guess. Thank you.

I’ve changed the code to count the correct guess, but I’m still not succeeding

 while true; do
        echo -e "\nGuess the secret number between 1 and 1000:"
        read guess

        if [[ ! $guess =~ ^[0-9]+$ ]]; then
            echo "That is not an integer, guess again:"
            continue
        fi

        if [[ $guess -eq $RANDOM_NUMBER ]]; then
            echo "You guessed it in $count_tries tries. The secret number was $RANDOM_NUMBER. Nice job!"
            ((count_tries++))
            break
        elif [[ $guess -gt $RANDOM_NUMBER ]]; then
            echo "It's lower than that, guess again:"
        else
            echo "It's higher than that, guess again:"
        fi

        ((count_tries++))
    done

    # Insert user id and number of tries into game_data table
    RESULT=$($PSQL "INSERT INTO game_data(user_id, tries) VALUES ($user_id, $count_tries)")

You need to also count the bad non-numeric guesses too.

1 Like

Still no sucess

 while true; do
        echo -e "\nGuess the secret number between 1 and 1000:"
        read guess

        if [[ ! $guess =~ ^[0-9]+$ ]]; then
            echo "That is not an integer, guess again:"
            ((count_tries++))
        fi

        if [[ $guess -eq $RANDOM_NUMBER ]]; then
            echo "You guessed it in $count_tries tries. The secret number was $RANDOM_NUMBER. Nice job!"
            ((count_tries++))
            break
        elif [[ $guess -gt $RANDOM_NUMBER ]]; then
            echo "It's lower than that, guess again:"
        else
            echo "It's higher than that, guess again:"
        fi

        ((count_tries++))
    done

It seems you are double counting now?

Try to hard code the number temporarily and test your code.

1 Like

I changed the range of values from the RANDOM variable to 2 for testing and it’s counting normally. I really don’t know what the hell is wrong.

Can you hardcode the number to 5 specifically (not random, just hardcode it)
And then run the code and go through a the sequence of guesses below? (I am counting the guesses so you can confirm)

  1. A
  2. b
  3. 9
  4. 3
  5. 4
  6. 5

Make sure that you enter the sequence above after you hard code the number to 5. Then confirm that you are printing the correct guess value (which is 6 in this case).

Also confirm the database logs the value correctly.

Can you move the line that updates the database so it runs inside the if statement that confirms the correct guess and then exit the program explicitly after that? (Make sure you have the exit command to exit that is)