Every testcase passes except
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
My code so far
--
-- 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,
name character varying(22) NOT NULL,
game_cnt integer DEFAULT 0,
best_game integer DEFAULT 0
);
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: 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: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.users_user_id_seq', 1, false);
--
-- Name: users users_name_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_name_key UNIQUE (name);
--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);
--
-- PostgreSQL database dump complete
--
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
echo -e "\nEnter your username:"
read USER_NAME
USER_ID=$($PSQL "select user_id from users where name='$USER_NAME'")
GAMES_PLAYED=0
BEST_GAME=1001
# echo $USER_ID
if ! [[ -z $USER_ID ]]
then
GAMES_PLAYED=$($PSQL "select game_cnt from users where user_id=$USER_ID")
BEST_GAME=$($PSQL "select best_game from users where user_id=$USER_ID")
echo "Welcome back, $USER_NAME You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
INSERT=$($PSQL "INSERT INTO users(name) VALUES('$USER_NAME')")
echo "Welcome, $USER_NAME! It looks like this is your first time here."
USER_ID=$($PSQL "SELECT user_id FROM users WHERE name='$USER_NAME'")
GAMES_PLAYED=$($PSQL "select game_cnt from users where user_id=$USER_ID")
BEST_GAME=$($PSQL "select best_game from users where user_id=$USER_ID")
fi
echo "Guess the secret number between 1 and 1000:"
USER_GUESS=0
NUM=$(($RANDOM %1000 +1))
CNT=0
while [[ $USER_GUESS -ne $NUM ]]
do
read USER_GUESS
if [[ $USER_GUESS =~ ^[0-9]+$ ]]
then
if [[ $USER_GUESS -lt $NUM ]]
then
echo "It's higher than that, guess again:"
elif [[ $USER_GUESS -gt $NUM ]]
then
echo "It's lower than that, guess again:"
fi
else
echo "That is not an integer, guess again:"
USER_GUESS=0
fi
((CNT++))
done
echo "You guessed it in $CNT tries. The secret number was $NUM. Nice job!"
# update games played and best score
UPDATE=$($PSQL "update users set game_cnt=$GAMES_PLAYED+1 where user_id=$USER_ID")
if [[ $CNT -lt $BEST_GAME || $BEST_GAME -eq 0 ]]
then
UPDATE=$($PSQL "update users set best_game=$CNT where user_id=$USER_ID")
fi
Browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Number Guessing Game - Build a Number Guessing Game
Link to the challenge: