Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

Tests For returning user and correct guess won’t pass, even though everything works fin e in the terminal.

Your code so far

#!/bin/bash

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

# -- GET USER --
while true; do
  echo "Enter your username:"
  read USERNAME
  # if more than 22 chars,
  if [ ${#USERNAME} -gt 22 ]; then
    echo "Your username can be no more than 22 characters"
  elif [[ -z $USERNAME ]] 
  then 
    echo "Username cannot be blank."
  else
    break
  fi
done

USER_DATA=$($PSQL "SELECT games_played, best_game FROM users WHERE username = '$USERNAME'")

if [[ -z $USER_DATA ]]
then
  echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
  # create new user in db
  NEW_USER=$($PSQL "INSERT INTO users(username, games_played) VALUES('$USERNAME', 0);")
else 
  IFS="|" read GAMES_PLAYED BEST_GAME <<< "$USER_DATA"
  echo -e "\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

# Generate a random number between 0 and 1000
SECRET_NUMBER=$(( 0 + RANDOM % 1000 ))

TURN_PROMPT() {
  if [[ $1 ]]
  then
    echo -e "\n$1"
  else
    echo "Guess the secret number between 1 and 1000:"
  fi
  read GUESS
}

#start game
TURN_PROMPT "Guess the secret number between 1 and 1000:"

# -- BEGIN LOOP --
NUMBER_OF_GUESSES=0
while true
do
  # if not an integer,
  if [[ ! $GUESS =~ ^[0-9]+$ ]]
  then
    TURN_PROMPT "That is not an integer, guess again:"
    # don't increment NUMBER_OF_GUESSES here
    continue
  fi

  # Convert GUESS to a number so a string such as '9p' won't error out
  GUESS_NUM=$(echo $GUESS | grep -E '^[0-9]+$')
  
  if [[ $SECRET_NUMBER -lt $GUESS_NUM ]]
  then
    (( NUMBER_OF_GUESSES++ ))
    TURN_PROMPT "It's lower than that, guess again:"
  elif [[ $SECRET_NUMBER -gt $GUESS_NUM ]]
  then
      (( NUMBER_OF_GUESSES++ ))
    TURN_PROMPT "It's higher than that, guess again:"
  elif [[ $SECRET_NUMBER -eq $GUESS_NUM ]]
  then
    (( NUMBER_OF_GUESSES++ ))
    break
  fi
done

# if guesses < best_game, update best game variable to guesses
if [[ -z $BEST_GAME ]] || [[ $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
then
  BEST_GAME=$NUMBER_OF_GUESSES
fi

# save new stats
UPDATE_GAMES_PLAYED=$($PSQL "UPDATE users SET games_played = games_played + 1, best_game = '$BEST_GAME' WHERE username = '$USERNAME'")

# print final output
echo -e "\nYou guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

Please be sure to escape your code when pasting directly into the forum. Wrap it in triple backtick characters:

```
<code goes here>
```

Also please share your SQL dump.

That said, it looks like you never read the user_id after inserting a new user in that case, which would prevent it from properly updating after the first completed game. That might be causing the issue with the tests.

I’ve edited your code for readability, as @SpookyBlump suggests.

Thank you for editing the code.
I include a copy of my sql dump below.

I am not using user_id as a column in my users table.
I thought a unique username would be sufficient.

I also did not see a reason to read the new user upon insertion, as I already have the values I need at that point - the username, games played (0) and best game, (null)

Thanks!

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.17 (Ubuntu 12.17-1.pgdg22.04+1)
-- Dumped by pg_dump version 12.17 (Ubuntu 12.17-1.pgdg22.04+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

DROP DATABASE number_guess;
--
-- Name: number_guess; Type: DATABASE; Schema: -; Owner: freecodecamp
--

CREATE DATABASE number_guess WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';


ALTER DATABASE number_guess OWNER TO freecodecamp;

\connect number_guess

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: users; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.users (
    username character varying(22),
    games_played integer,
    best_game integer
);


ALTER TABLE public.users OWNER TO freecodecamp;

--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.users VALUES ('poppi', 0, 0);


--
-- Name: users unique_username; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT unique_username UNIQUE (username);

--
-- PostgreSQL database dump complete
--

My mistake, not sure how I missed that. I tried having another look but I really couldn’t find any problem, unfortunately. Hopefully someone else can chime in.

1 Like