Build a Number Guessing Game Tests Running

Hi,

I’ve been struggling to get pretty much any of the tests to pass on this game, yet when I play it myself everything works perfectly. The issue seems to be that the test case users NEVER reach the number, as evidenced by 1000+ guesses entered into my database before the tests end.

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
echo "Enter your username:"
read USERNAME
#generate random
RAND=$(echo $(( $RANDOM % 1000 + 1 )))
#check username
USERNAME_RESULT=$($PSQL "SELECT username FROM maintable WHERE username='$USERNAME'" )
#if its a new user
if [[ -z $USERNAME_RESULT ]]
then
#insert into database a new user
INSERT_NAME=$($PSQL "INSERT into maintable (username) values('$USERNAME')")
#greet them
echo "Welcome, $USERNAME! It looks like this is your first time here."
FIRSTTIME=true
else
#if returning user, pull the best game and the games played
BESTGAME=$($PSQL "SELECT bestgame FROM maintable WHERE username='$USERNAME'")
GAMESPLAYED=$($PSQL "SELECT gamesplayed FROM maintable WHERE username='$USERNAME'")
echo "Welcome back, $USERNAME! You have played $GAMESPLAYED games, and your best game took $BESTGAME guesses."
fi

#first guess
echo "Guess the secret number between 1 and 1000:"
read GUESS

while true
do
#add one guess per loop
INCREM=$($PSQL "UPDATE maintable SET guesses = guesses + 1 WHERE username='$USERNAME'")
#if not integer
if [[ ! $GUESS =~ ^[0-9]+$ ]]
then
echo -e "\nThat is not an integer, guess again:"
read GUESS
continue
fi

#It's lower than that, guess again:
if [[ $GUESS -gt $RAND ]]
then
echo -e "\nIt's lower than that, guess again:"
read GUESS
continue
fi

#It's higher than that, guess again:
if [[ $GUESS -lt $RAND ]]
then
echo -e "\nIt's higher than that, guess again:"
read GUESS
continue
fi

if [[ $GUESS -eq $RAND ]]
then
GUESSES=$($PSQL "SELECT guesses from maintable where username='$USERNAME'")
echo -e "\nYou guessed it in $GUESSES tries. The secret number was $RAND. Nice job!"
#set guesses back to 0
ZERO=$($PSQL "UPDATE maintable SET guesses = 0 WHERE username='$USERNAME'")
#increment total games
INCR_GAMES=$($PSQL "UPDATE maintable SET gamesplayed = gamesplayed + 1 WHERE username='$USERNAME'")
#check if bestgame is lower than current guesses
BESTGAME=$($PSQL "SELECT bestgame FROM maintable WHERE username='$USERNAME'")
if (( GUESSES < BESTGAME ))
then
#set new best game
NEWBESTGAME=$($PSQL "UPDATE maintable SET bestgame = $GUESSES WHERE username='$USERNAME'")
fi
if [[ $BESTGAME -eq 0 ]]
then
  #set new best game
NEWBESTGAME=$($PSQL "UPDATE maintable SET bestgame = $GUESSES WHERE username='$USERNAME'")
fi
fi
break
done

I can paste the DB dumb if necessary, but it is basically a CREATE TABLE maintable (username varchar(30) NOT NULL, bestgame int default 0, gamesplayed int default 0, and guesses int default 0;)

When I personally go through the game, it will insert the data into my table correctly. When I run the tests I can’t get even the “Enter your username:” test to pass, let alone the others. The culprit seems to be that the game NEVER completes for the tests and perhaps times out after hundreds or thousands of guesses. I’ve tried refactoring it a few different ways now, using an until case, a while case, and splitting it into separate smaller functions. I feel like I’m missing something basic or the tests are broken, because the rest of this course was really straightforward.

One hint: do not store the guesses in the database. Use the script to count the guesses and store only the final number when the user makes the correct guess.

1 Like

Wow that was fast. Thanks I’ll try that. I was thinking that is probably a best practice, but the average game is only a dozen guesses.

1 Like

Welp. I changed the GUESSES from a DB query to a (( GUESSES +=1 )) and it worked. Learning moment. I suppose it also would have helped to know the test suites seem to test completely randomly as they take hundreds of tries even with correct feedback. Thanks!

1 Like