Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

my number_guess.sh seems to function correctly in my terminal, and I have no idea why it isn’t passing. the code will pass some tests and then fail them when they’re run again on the same code. if there’s a bug in the tests, I have no idea how to identify it. please help – thanks in advance!

Your code so far

#! /bin/bash

PSQL="psql -U freecodecamp -d number_guess --tuples-only --no-align -c"

RUN_GAME () {
   SECRET_NUMBER=$(( (RANDOM % 1000) + 1 ))
   INSERT_GAME=$($PSQL "INSERT INTO games (user_id) VALUES ($USER_ID);" | xargs)
   GAME_NUMBER=$($PSQL "SELECT MAX(game_id) FROM games WHERE user_id = $USER_ID;" | xargs)
    echo "Guess the secret number between 1 and 1000:"
   while true
    do
        read GUESS
        GUESS=$(echo $GUESS | xargs)
        if [[ ! $GUESS =~ ^[0-9]+$ ]]
        then
            echo "That is not an integer, guess again:"
            continue
        elif [[ $GUESS -eq $SECRET_NUMBER ]]
        then
            INSERT_GUESS=$($PSQL "INSERT INTO guesses (game_id, user_id, correct) VALUES ($GAME_NUMBER, $USER_ID, TRUE);" | xargs)
            NUMBER_OF_GUESSES=$($PSQL "SELECT COUNT (*) FROM guesses WHERE game_id = $GAME_NUMBER;" | xargs)
            MARK_WON=$($PSQL "UPDATE games SET won = TRUE WHERE game_id = $GAME_NUMBER;" | xargs)
            echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
            break
        elif [[ $GUESS -gt $SECRET_NUMBER ]]
        then
            echo "It's lower than that, guess again:" 
            INSERT_GUESS=$($PSQL "INSERT INTO guesses (game_id, user_id, correct) VALUES ($GAME_NUMBER, $USER_ID, FALSE);" | xargs)
            continue 
        elif [[ $GUESS -lt $SECRET_NUMBER ]]
        then
            echo "It's higher than that, guess again:"
            INSERT_GUESS=$($PSQL "INSERT INTO guesses (game_id, user_id, correct) VALUES ($GAME_NUMBER, $USER_ID, FALSE);" | xargs)
            continue
        fi
    done

}

echo "Enter your username:"
read USERNAME

if [[ -n $($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';" | xargs) ]]
then
    USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';" | xargs)
    GAMES_PLAYED=$($PSQL "SELECT COUNT (*) FROM games WHERE user_id = $USER_ID;" | xargs)
    BEST_GAME=$($PSQL "SELECT MIN(guess_count) AS lowest_guesses FROM (SELECT game_id, COUNT(*) AS guess_count FROM guesses WHERE user_id = $USER_ID GROUP BY game_id HAVING MAX(correct::int) = 1) AS guess_counts;" | xargs)
    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
    RUN_GAME
else
    INSERT_USER=$($PSQL "INSERT INTO users (username) VALUES ('$USERNAME');" | xargs)
    USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';" | xargs)
    echo "Welcome, $USERNAME! It looks like this is your first time here."
    RUN_GAME
fi
--
-- PostgreSQL database dump
--

-- Dumped from database version 14.14 (Homebrew)
-- Dumped by pg_dump version 14.14 (Homebrew)

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

CREATE TABLE public.games (
    user_id integer NOT NULL,
    game_id integer NOT NULL,
    won boolean DEFAULT false
);


ALTER TABLE public.games OWNER TO freecodecamp;

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

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


ALTER TABLE public.games_game_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.games_game_id_seq OWNED BY public.games.game_id;


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

CREATE TABLE public.guesses (
    user_id integer NOT NULL,
    game_id integer NOT NULL,
    guess_id integer NOT NULL,
    correct boolean NOT NULL
);


ALTER TABLE public.guesses OWNER TO freecodecamp;

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

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


ALTER TABLE public.guesses_guess_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.guesses_guess_id_seq OWNED BY public.guesses.guess_id;


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

CREATE TABLE public.users (
    username character varying(22) NOT NULL,
    user_id integer 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 game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games ALTER COLUMN game_id SET DEFAULT nextval('public.games_game_id_seq'::regclass);


--
-- Name: guesses guess_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.guesses ALTER COLUMN guess_id SET DEFAULT nextval('public.guesses_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; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

COPY public.games (user_id, game_id, won) FROM stdin;
\.


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

COPY public.guesses (user_id, game_id, guess_id, correct) FROM stdin;
\.


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

COPY public.users (username, user_id) FROM stdin;
\.


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

SELECT pg_catalog.setval('public.games_game_id_seq', 10, true);


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

SELECT pg_catalog.setval('public.guesses_guess_id_seq', 84, true);


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

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


--
-- Name: games game_id_unique; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT game_id_unique UNIQUE (game_id);


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

ALTER TABLE ONLY public.users
    ADD CONSTRAINT user_id_unique UNIQUE (user_id);


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

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


--
-- Name: games games_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(user_id);


--
-- Name: guesses guesses_game_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.guesses
    ADD CONSTRAINT guesses_game_id_fkey FOREIGN KEY (game_id) REFERENCES public.games(game_id);


--
-- Name: guesses guesses_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.guesses
    ADD CONSTRAINT guesses_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(user_id);


--
-- PostgreSQL database dump complete
--

Your browser information:

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

Challenge Information:

Number Guessing Game - Build a Number Guessing 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 (').