Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:
Firstly I want to point out that from the user stories you have to guess a number between 1 and 1000, but if your random number is between 1 and 1000 the tests run for too long I assume and you won’t never have it correct. That’s why I had to create a random number between 1 and 10. But I still have 2 tasks that are not correct according to the tests, but I assume my script is correct.

Those tasks are:
1.

If that username has been used before, it should print Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses., with <username> 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
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!` and finish running

I assume it has something to do with me counting the guesses. I assume when a user guess the number and also guess non integer number that is one count. I also tried to count only guesses for just a integer number but it doesn’t work either.

Also I’m not sure how the game for a user should be counted. I assume that when a user finishes a game succesfully that counts as a one game.

I’m not sure though I’m kinda desparate and don’t know what to do.

solution: universe-freecodecamp/number_guessing_game at main · jamm3e3333/universe-freecodecamp · GitHub

number_guess.sh:

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

echo "Enter your username:"
read USERNAME

EXISTING_USER=$($PSQL "select username from users where username='$USERNAME';")
if [[ -z $EXISTING_USER ]]
then
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  $PSQL "insert into users (username) values ('$USERNAME')"  
else
  USER_ID=$($PSQL "select user_id from users where username='$USERNAME';")
  GAMES_COUNT=$($PSQL "select count(*) from won_user_games where user_id='$USER_ID';")
  BEST_GAME_GUESS_COUNT=$($PSQL "select coalesce(min(guess_count), -1) from won_user_games where user_id=$USER_ID;")

  echo "Welcome back, $USERNAME! You have played $GAMES_COUNT games, and your best game took $BEST_GAME_GUESS_COUNT guesses."
fi

GUESS_COUNT=0
RANDOM_NUMBER=$(( RANDOM % 10 + 1 ))
NUMBER_GUESSED=-1

until [ $RANDOM_NUMBER == $NUMBER_GUESSED ]
do
  echo "Guess the secret number between 1 and 1000:"
  read NUMBER_GUESSED
  if [[ ! $NUMBER_GUESSED =~ ^[0-9]+$ ]]
  then
    echo "That is not an integer, guess again:"
  else
    (( GUESS_COUNT++ ))
    if [[ $NUMBER_GUESSED -gt $RANDOM_NUMBER ]]
    then
      echo "It's lower than that, guess again:"
    elif [[ $NUMBER_GUESSED -lt $RANDOM_NUMBER ]]
    then
      echo "It's higher than that, guess again:"
    fi
  fi
done

USER_ID=$($PSQL "select user_id from users where username='$USERNAME';")
$PSQL "insert into won_user_games (user_id, guess_count) values ('$USER_ID', $GUESS_COUNT);"
# finish the game
echo "You guessed it in $GUESS_COUNT tries. The secret number was $RANDOM_NUMBER. Nice job!"

number_guess.sql:

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
-- Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.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 (
    user_id integer NOT NULL,
    username character varying(22) NOT NULL,
    games_played bigint DEFAULT 0 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: won_user_games; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.won_user_games (
    won_game_id integer NOT NULL,
    user_id integer NOT NULL,
    guess_count integer
);


ALTER TABLE public.won_user_games OWNER TO freecodecamp;

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

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


ALTER TABLE public.won_user_games_won_game_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.won_user_games_won_game_id_seq OWNED BY public.won_user_games.won_game_id;


--
-- 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);


--
-- Name: won_user_games won_game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.won_user_games ALTER COLUMN won_game_id SET DEFAULT nextval('public.won_user_games_won_game_id_seq'::regclass);


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

INSERT INTO public.users VALUES (67, 'user_1672954897659', 0);
INSERT INTO public.users VALUES (68, 'user_1672954897658', 0);


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

INSERT INTO public.won_user_games VALUES (180, 67, 9);
INSERT INTO public.won_user_games VALUES (181, 67, 2);
INSERT INTO public.won_user_games VALUES (182, 68, 8);
INSERT INTO public.won_user_games VALUES (183, 68, 5);
INSERT INTO public.won_user_games VALUES (184, 67, 1);
INSERT INTO public.won_user_games VALUES (185, 67, 7);
INSERT INTO public.won_user_games VALUES (186, 67, 6);


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

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


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

SELECT pg_catalog.setval('public.won_user_games_won_game_id_seq', 186, true);


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

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


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

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


--
-- Name: won_user_games won_user_games_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.won_user_games
    ADD CONSTRAINT won_user_games_pkey PRIMARY KEY (won_game_id);


--
-- Name: won_user_games won_user_games_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.won_user_games
    ADD CONSTRAINT won_user_games_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/108.0.0.0 Safari/537.36

Challenge: Number Guessing Game - Build a Number Guessing Game

Link to the challenge:

hi there,

use 1000. The game will most certainly finish. Even if you are manually playing the game to check, all you have to do is apply some ‘binary’ thinking. (whatever range is left, guess a number in the middle of it and eventually you will reach the correct guess - it won’t take long at all actually).
Eg. if the number is 980 and I guessed 1 , obviously it is higher, so I just half the remaining 999 numbers and guess about 500, still higher, then half of that 750 is still higher, then half again etc, Eventually the game ends.
(you cannot pass the test unless you use 1000 so fix that one first)

You guessed that you should not count invalid guesses that are not numbers. Correct. Do not count those.

For the number of games, everytime someone plays to the end (and gets the correct guess), then count that as one game.

One other thing is you must count the last (correct) guess as one guess.
(It doesn’t look like you are doing that).

1 Like

Thanks for pointing out counting the last successfull guess, that was the issue.

Also when I said “the random number beeing between 0 and 1000 slows down tests” was stupid because as you said you can apply some binary search logic to it and it doesn’t take that long to find out.

The issue, however, was that I had some logs from the update or insert queries and the tests, I think, were checking the logs so that’s why maybe, not sure.

e.g. with this:

$PSQL "insert into won_user_games (user_id, guess_count) values ('$USER_ID', $GUESS_COUNT);"

it was failing, because it was logging smth like:

INSERT 0 1

but with this:

QUERY_RESULT=$($PSQL "insert into won_user_games (user_id, guess_count) values ('$USER_ID', $GUESS_COUNT);")

tests passed.

Anyway thank you, for checking my issue and helping me out :blue_heart:

1 Like