Number Guessing Game

Hello, we are currently working on this project to obtain the certification in relational databases. The tasks are already finished and after testing all the requested points, we noticed that it works correctly, but for some reason, it does not allow us to finish. This is the database:

--
-- 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: games_guess; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.games_guess (
    game_guess_id integer NOT NULL,
    number_guess integer NOT NULL,
    secret_number integer NOT NULL,
    group_guess integer NOT NULL,
    result integer NOT NULL,
    user_id integer NOT NULL
);


ALTER TABLE public.games_guess OWNER TO freecodecamp;

--
-- Name: games_guess_game_guess_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.games_guess_game_guess_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.games_guess_game_guess_id_seq OWNER TO freecodecamp;

--
-- Name: games_guess_game_guess_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.games_guess_game_guess_id_seq OWNED BY public.games_guess.game_guess_id;


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

CREATE TABLE public.users (
    user_id integer NOT NULL,
    username character varying(22) NOT NULL
);


ALTER TABLE public.users OWNER TO freecodecamp;

--
-- Name: users_user_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.users_user_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.users_user_id_seq OWNER TO freecodecamp;

--
-- Name: users_user_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.users_user_id_seq OWNED BY public.users.user_id;


--
-- Name: games_guess game_guess_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games_guess ALTER COLUMN game_guess_id SET DEFAULT nextval('public.games_guess_game_guess_id_seq'::regclass);


--
-- Name: users user_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users ALTER COLUMN user_id SET DEFAULT nextval('public.users_user_id_seq'::regclass);


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

INSERT INTO public.games_guess VALUES (1, 1, 476, 1, 0, 1);
INSERT INTO public.games_guess VALUES (2, 476, 476, 1, 1, 1);
INSERT INTO public.games_guess VALUES (3, 2, 889, 1, 0, 2);
INSERT INTO public.games_guess VALUES (4, 100, 889, 1, 0, 2);
INSERT INTO public.games_guess VALUES (5, 1, 889, 1, 0, 2);
INSERT INTO public.games_guess VALUES (6, 4, 889, 1, 0, 2);
INSERT INTO public.games_guess VALUES (7, 889, 889, 1, 1, 2);
INSERT INTO public.games_guess VALUES (8, 1, 961, 2, 0, 1);
INSERT INTO public.games_guess VALUES (9, 6, 961, 2, 0, 1);
INSERT INTO public.games_guess VALUES (10, 4, 961, 2, 0, 1);
INSERT INTO public.games_guess VALUES (11, 961, 961, 2, 1, 1);
INSERT INTO public.games_guess VALUES (12, 1, 280, 2, 0, 2);
INSERT INTO public.games_guess VALUES (13, 2, 280, 2, 0, 2);
INSERT INTO public.games_guess VALUES (14, 280, 280, 2, 1, 2);


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

INSERT INTO public.users VALUES (1, 'A');
INSERT INTO public.users VALUES (2, 'B');


--
-- Name: games_guess_game_guess_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.games_guess_game_guess_id_seq', 14, true);


--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.users_user_id_seq', 2, true);


--
-- Name: games_guess games_guess_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games_guess
    ADD CONSTRAINT games_guess_pkey PRIMARY KEY (game_guess_id);


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

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);


--
-- Name: games_guess games_guess_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games_guess
    ADD CONSTRAINT games_guess_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(user_id);


--
-- PostgreSQL database dump complete

This is the number_guess.sh file we are using:

#!/bin/bash

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

secret_number=$((RANDOM % 1000 + 1))
echo "Enter your username:"
read username

INICIO()
{
  USER_NAME_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$username';")
  VALIDAR_USUARIO  
}

VALIDAR_USUARIO()
{
  if [[ -z $USER_NAME_ID ]]
  then
    INSERT_USER_NAME=$($PSQL "INSERT INTO users (username) VALUES('$username');")
    NEW_USER_NAME_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$username';")
    GROUP_GUESS=1
    echo $"Welcome, $username! It looks like this is your first time here."
    echo "Guess the secret number between 1 and 1000:"
    VALIDAR_NUMBER_GUESS
    VALIDAR_NUMBER
  else
    NEW_USER_NAME_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$username';")
    games_played=$($PSQL "SELECT SUM(result) FROM games_guess WHERE user_id = '$NEW_USER_NAME_ID';")
    best_game=$($PSQL "SELECT MIN(count) FROM (SELECT group_guess AS group, COUNT(game_guess_id) AS count, MAX(result) AS resultado FROM games_guess WHERE user_id = '$NEW_USER_NAME_ID' GROUP BY group_guess) as a WHERE resultado = 1;")
    LAST_GUESS_ID=$($PSQL "SELECT MAX(game_guess_id) FROM games_guess WHERE user_id = '$NEW_USER_NAME_ID';")
    LAST_RESULT=$($PSQL "SELECT result FROM games_guess WHERE game_guess_id = $LAST_GUESS_ID;")
    LAST_GROUP_GUESS=$($PSQL "SELECT group_guess FROM games_guess WHERE game_guess_id = $LAST_GUESS_ID;")
    echo "Welcome back, $username! You have played $games_played games, and your best game took $best_game guesses."
    echo "Guess the secret number between 1 and 1000:"
    VALIDAR_NUMBER_GUESS
    if [[ $LAST_RESULT == 1 ]]
    then
      GROUP_GUESS=$((LAST_GROUP_GUESS + 1))
      VALIDAR_NUMBER
    else
      GROUP_GUESS=$LAST_GROUP_GUESS
      VALIDAR_NUMBER
    fi
  fi
}

VALIDAR_NUMBER()
{
  if [[ $NUMBER_GUESS == $secret_number ]]
  then
    INSERT_GUESS=$($PSQL "INSERT INTO games_guess (number_guess, secret_number, group_guess, result, user_id) VALUES ($NUMBER_GUESS, $secret_number, $GROUP_GUESS, 1, $NEW_USER_NAME_ID);") 
    number_of_guesses=$($PSQL "SELECT COUNT(game_guess_id) FROM games_guess WHERE user_id = '$NEW_USER_NAME_ID' AND group_guess = $GROUP_GUESS;")
    echo "You guessed it in $number_of_guesses tries. The secret number was $secret_number. Nice job!"
  elif [[ $NUMBER_GUESS > $secret_number ]]
  then
    INSERT_GUESS=$($PSQL "INSERT INTO games_guess (number_guess, secret_number, group_guess, result, user_id) VALUES ($NUMBER_GUESS, $secret_number, $GROUP_GUESS, 0, $NEW_USER_NAME_ID);") 
    echo "It's higher than that, guess again:"
    VALIDAR_NUMBER_GUESS
    VALIDAR_NUMBER
  else
    INSERT_GUESS=$($PSQL "INSERT INTO games_guess (number_guess, secret_number, group_guess, result, user_id) VALUES ($NUMBER_GUESS, $secret_number, $GROUP_GUESS, 0, $NEW_USER_NAME_ID);") 
    echo "It's lower than that, guess again:"
    VALIDAR_NUMBER_GUESS
    VALIDAR_NUMBER
  fi
}

VALIDAR_NUMBER_GUESS()
{
  read NUMBER_GUESS
  if [[ $NUMBER_GUESS =~ ^[0-9]+$ ]]
  then
    NUMBER_GUESS=$NUMBER_GUESS
  else
    echo "That is not an integer, guess again:"
    VALIDAR_NUMBER_GUESS
  fi  
}

INICIO

When we run to validate if we have already finished the tasks, we get this message:
SUBTASKS 1.1 :8 Your script should print the correct welcome message for returning users.

First of all, we thought it was because we had called the variables with different names, so we changed them. Since the message is still appearing, we copied and pasted the messages that should appear again, since suddenly we included spaces or letters that were not there, but the same message continued to appear. We’ve been changing things for a little over a week and nothing.
Searching today, we found out about these forums and saw several regarding this same project, but we did not find one that answered the cause of the problem we have.
thanks for your help

Please copy the project requirements here for our information. I think there is a sample output file included in this project? Please post it here as well and post a copy of a sample run of your code.

Also note that whenever the user makes a guess, even if it is invalid, you should increase the guess count.

So if I guess: a
Then that counts as one guess. (Even though it is not a number)

hello, these are the steps to complete:

  • Create a number_guessing_game folder in the project folder for your program
  • Create number_guess.sh in your number_guessing_game folder and give it executable permissions
  • Your script should have a shebang at the top of the file that uses #!/bin/bash`
  • Turn the number_guessing_game folder into a git repository
  • Your git repository should have at least five commits
  • Your script should randomly generate a number that users have to guess
  • When you run your script, you should prompt the user for a username with Enter your username:, and take a username as input. Your database should allow usernames of at least 22 characters
  • If that username has been used before, it should print Welcome back, ! You have played <games_played> games, and your best game took <best_game> guesses. with 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
  • If the username has not been used before, you should print Welcome, ! It looks like this is your first time here.
  • The next line printed should be Guess the secret number between 1 and 1000: and input from the user should be read
  • Until they guess the secret number, it should print It’s lower than that, guess again: if the previous input was higher than the secret number, and `It’s higher than that, guess again: if the previous input was lower than the secret number. Asking for input each time until they input the secret number.
  • If anything other than an integer is input as a guess, it should print That is not an integer, guess again:
  • When the secret number is guessed, your script should print You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job!
  • The message for the first commit should be Initial commit
  • The rest of the commit messages should start with fix: feat:, refactor:, chore:, or test:
  • You should finish your project while on the main branch, your working tree should be clean, and you should not have any uncommitted changes.

At the moment I do not have access to the project, since it exceeds the 500 changes that it allows me and only until November 14, we will have access again. I don’t remember that it uploaded a file with what it should output.

Thanks.

please remember to pin the workspace on your dashboard, old workspaces that are not pinned are deleted automatically

thanks for the directions. This is our workspace: https://freecodecam-learnnumber-t6e7f0wauv0.ws-us116.gitpod.io/
We still cannot identify why we did not comply with the steps. We appreciate your help.

It helps if you can be more specific about how you are stuck and what you don’t understand.

Of course, I already have the entire solution finished, which meets the required requirements, but I can’t get past point 1.1.8 which says: SUBTASKS 1.1 :8 Your script should print the correct welcome message for returning users.

Welcome to the forum @quinchiag

You are using the variable username for both bash and postgreSQL.
Try using all uppercase for the bash variable.

Happy coding

Hello Teller, we proceed to do what you tell us, taking the variables in bash in capital letters, but we continue without being able to finish the project. We share the workplace: https://freecodecam-learnnumber-t6e7f0wauv0.ws-us116.gitpod.io/.
If you manage to find something else that can help us, we will be attentive.

I think the test maybe timing out.
Try removing some to the unnecessary columns from the table.

Happy coding