-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_guess.sh
63 lines (53 loc) · 1.93 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
#!/bin/bash
PSQL="psql --username=yellowflash2041 --dbname=users -t --no-align -c"
echo "Enter your username:"
read GET_USERNAME
let COUNT=0
USERNAME=$($PSQL "SELECT username FROM users WHERE username='$GET_USERNAME'")
if [[ -z $USERNAME ]]
then
USERNAME=$GET_USERNAME
echo "Welcome, $USERNAME! It looks like this is your first time here.\n"
INSERT_USERNAME=$($PSQL "INSERT INTO users VALUES('$USERNAME',0,0)")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username='$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username='$USERNAME'")
else
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username='$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username='$USERNAME'")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
RANGE=$((1000))
SECRET_NUMBER=$(($(($RANDOM%$RANGE))+1))
echo "Guess the secret number between 1 and 1000:"
read GUESSING_NUMBER
NEW_GAMES_PLAYED=$(($GAMES_PLAYED+1))
UPATE_GAME_PLAYED=$($PSQL "UPDATE users SET games_played=$NEW_GAMES_PLAYED WHERE username='$USERNAME'")
let COUNT++
while ! [[ "$GUESSING_NUMBER" =~ ^[0-9]+$ ]]
do
echo "That is not an integer, guess again:"
read GUESSING_NUMBER
done
while [ $GUESSING_NUMBER != $SECRET_NUMBER ]
do
if ! [[ "$GUESSING_NUMBER" =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
read GUESSING_NUMBER
elif [[ $GUESSING_NUMBER > $SECRET_NUMBER ]]
then
echo "It's lower than that, guess again:"
read GUESSING_NUMBER
let COUNT++
elif [[ $GUESSING_NUMBER < $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
read GUESSING_NUMBER
let COUNT++
fi
done
if [[ $COUNT < $BEST_GAME ]] || [[ $BEST_GAME = 0 ]]
then
UPATE_BEST_GAME=$($PSQL "UPDATE users SET best_game=$COUNT WHERE username='$USERNAME'")
fi
echo "You guessed it in $COUNT tries. The secret number was $SECRET_NUMBER. Nice job!"