Tell us what’s happening:
Hello the program is running correctly but I can’t get this two tests to pass:
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
You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job!
Here is my code so far
#!/bin/bash
# Number Guessing Game
PSQL="psql --username=freecodecamp --dbname=number_guessing_game -t --no-align -c"
# generate a random variable with a 1 to 1000 range
ACTUAL_NUMBER=$(( RANDOM%1000 +1))
#echo $ACTUAL_NUMBER
# First part which is getting the user info and seeing if the user is in the database or not.
USER_INFO(){
# Asking user for name and reading it
echo -e "\nEnter your username:"
read NAME
# Query for checking if number is in the database
USER=$($PSQL "SELECT username FROM number_guessing WHERE username='$NAME'")
# if user is not in the database
if [[ -z $USER ]]
then
INSERT_USER_INFO=$($PSQL "INSERT INTO number_guessing(username) VALUES('$NAME')")
echo $INSERT_USER_INFO
echo -e "Welcome, $NAME! It looks like this is your first time here."
else
# if user is in the database
if [[ ! -z $USER ]]
then
PLAYED_GAMES=$($PSQL "SELECT games_played FROM number_guessing WHERE username='$NAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM number_guessing WHERE username='$NAME'")
echo -e "Welcome back, $NAME! You have played $PLAYED_GAMES games, and your best game took $BEST_GAME guesses."
fi
fi
}
USER_INFO
# Building the guessing game
GUESSING_GAME(){
# Asking user to guess a number and reading it.
echo -e "Guess the secret number between 1 and 1000:"
COUNT=0
# Loop which continues until the user gets the correct number
while [[ $ACTUAL_NUMBER -ne $NUMBER ]]
do
read NUMBER
# if input is not a number
if [[ $NUMBER =~ ([^0-9]+)$ ]]
then
echo -e "That is not an integer, guess again:"
# Count is the number of guesses it takes to get the right answer
((COUNT++))
else
# if input is a number and is greater than the actual number
if [[ $NUMBER -gt $ACTUAL_NUMBER ]]
then
echo -e "It's lower than that, guess again:"
((COUNT++))
elif [[ $NUMBER -lt $ACTUAL_NUMBER ]]
# if input is a number and is lower than the actual number
then
echo -e "It's higher than that, guess again:"
((COUNT++))
else [[ $NUMBER -eq $ACTUAL_NUMBER ]]
((GAMES_PLAYED++))
# if input is a number and is equals to the actual number
echo -e "You guessed it in $COUNT tries. The secret number was $ACTUAL_NUMBER. Nice job!"
fi
fi
done
}
GUESSING_GAME
# Afterwards updating user info with the games played and the best game which is the one which took the least no of guesses
UPDATED_INFO(){
# First updating the games played from the guessing game
# First check if the games played are in the database
# If there are no games played in the database
if [[ -z $PLAYED_GAMES ]]
then
PLAYED_GAMES=0
PLAYED_GAMES=$(( $PLAYED_GAMES+1 ))
GAMES_PLAYED_UPDATED=$($PSQL "UPDATE number_guessing SET games_played=$PLAYED_GAMES WHERE username='$NAME'")
else
# If there are games played in the database
if [[ ! -z $PLAYED_GAMES ]]
then
PLAYED_GAMES=$(( $PLAYED_GAMES+1 ))
GAMES_PLAYED_UPDATED=$($PSQL "UPDATE number_guessing SET games_played=$PLAYED_GAMES WHERE username='$NAME'")
fi
fi
# Then choosing the game with the least no of guesses
UPDATE_BEST_GAME=$($PSQL "UPDATE number_guessing SET best_game=$COUNT WHERE username='$NAME' AND (best_game > $COUNT OR best_game ISNULL)")
}
UPDATED_INFO
Here is my database dump
--
-- 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_guessing_game;
--
-- Name: number_guessing_game; Type: DATABASE; Schema: -; Owner: freecodecamp
--
CREATE DATABASE number_guessing_game WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';
ALTER DATABASE number_guessing_game OWNER TO freecodecamp;
\connect number_guessing_game
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: number_guessing; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.number_guessing (
username character varying(22),
games_played integer,
best_game integer
);
ALTER TABLE public.number_guessing OWNER TO freecodecamp;
--
-- Data for Name: number_guessing; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
INSERT INTO public.number_guessing VALUES ('user_1677138720983', 1, NULL);
INSERT INTO public.number_guessing VALUES ('Mark', 2, 7);
INSERT INTO public.number_guessing VALUES ('user_1677138720984', 1, NULL);
INSERT INTO public.number_guessing VALUES ('user_1677142506608', 2, 176);
INSERT INTO public.number_guessing VALUES ('user_1677145592907', 2, 489);
INSERT INTO public.number_guessing VALUES ('user_1677142506609', 5, 116);
INSERT INTO public.number_guessing VALUES ('Lenox', 3, 4);
INSERT INTO public.number_guessing VALUES ('user_1677145592908', 5, 263);
INSERT INTO public.number_guessing VALUES ('James', 5, 8);
INSERT INTO public.number_guessing VALUES ('user_1677140815414', 2, 559);
INSERT INTO public.number_guessing VALUES ('John', 3, 10);
INSERT INTO public.number_guessing VALUES ('Dennis', 1, 8);
INSERT INTO public.number_guessing VALUES ('user_1677140815415', 2, 81);
INSERT INTO public.number_guessing VALUES ('user_1677142955128', 2, 202);
INSERT INTO public.number_guessing VALUES ('user_1677146080533', 2, 382);
INSERT INTO public.number_guessing VALUES ('user_1677142955129', 5, 4);
INSERT INTO public.number_guessing VALUES ('user_1677141988237', 2, 694);
INSERT INTO public.number_guessing VALUES ('user_1677141988238', 5, 851);
INSERT INTO public.number_guessing VALUES ('user_1677146080534', 5, 221);
INSERT INTO public.number_guessing VALUES ('user_1677143390417', 2, 438);
INSERT INTO public.number_guessing VALUES ('user_1677143390418', 5, 124);
INSERT INTO public.number_guessing VALUES ('user_1677143673523', 2, 937);
INSERT INTO public.number_guessing VALUES ('user_1677143673524', 5, 143);
INSERT INTO public.number_guessing VALUES ('user_1677137441735', NULL, NULL);
INSERT INTO public.number_guessing VALUES ('user_1677137441734', NULL, NULL);
INSERT INTO public.number_guessing VALUES ('user_1677138537868', 1, NULL);
INSERT INTO public.number_guessing VALUES ('user_1677144051881', 2, 430);
INSERT INTO public.number_guessing VALUES ('user_1677138537869', 1, NULL);
INSERT INTO public.number_guessing VALUES ('user_1677144051882', 5, 307);
--
-- PostgreSQL database dump complete
--
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.55
Challenge: Number Guessing Game - Build a Number Guessing Game
Link to the challenge: