-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_guess.sh
84 lines (65 loc) · 2.47 KB
/
number_guess.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
# variable to query database
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# promp player for username
echo -e "\nEnter your username:"
read USERNAME
# get username data
USERNAME_RESULT=$($PSQL "SELECT username FROM players WHERE username='$USERNAME'")
# get user id
USER_ID_RESULT=$($PSQL "SELECT user_id FROM players WHERE username='$USERNAME'")
# if player is not found
if [[ -z $USERNAME_RESULT ]]
then
# greet player
echo -e "\nWelcome, $USERNAME! It looks like this is your first time here.\n"
# add player to database
INSERT_USERNAME_RESULT=$($PSQL "INSERT INTO players(username) VALUES ('$USERNAME')")
else
GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM games LEFT JOIN players USING(user_id) WHERE username='$USERNAME'")
BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games LEFT JOIN players USING(user_id) WHERE username='$USERNAME'")
echo Welcome back, $USERNAME\! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.
fi
# generate random number between 1 and 1000
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
# variable to store number of guesses/tries
GUESS_COUNT=0
# prompt first guess
echo "Guess the secret number between 1 and 1000:"
read USER_GUESS
# loop to prompt user to guess until correct
until [[ $USER_GUESS == $SECRET_NUMBER ]]
do
# check guess is valid/an integer
if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
then
# request valid guess
echo -e "\nThat is not an integer, guess again:"
read USER_GUESS
# update guess count
((GUESS_COUNT++))
# if its a valid guess
else
# check inequalities and give hint
if [[ $USER_GUESS < $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
read USER_GUESS
# update guess count
((GUESS_COUNT++))
else
echo "It's lower than that, guess again:"
read USER_GUESS
#update guess count
((GUESS_COUNT++))
fi
fi
done
# loop ends when guess is correct so, update guess
((GUESS_COUNT++))
# get user id
USER_ID_RESULT=$($PSQL "SELECT user_id FROM players WHERE username='$USERNAME'")
# add result to game history/database
INSERT_GAME_RESULT=$($PSQL "INSERT INTO games(user_id, secret_number, number_of_guesses) VALUES ($USER_ID_RESULT, $SECRET_NUMBER, $GUESS_COUNT)")
# winning message
echo You guessed it in $GUESS_COUNT tries. The secret number was $SECRET_NUMBER. Nice job\!