Skip to content

Commit 0b46cca

Browse files
committed
Update README.md and guess-number-game.sh added
1 parent b0d2f4a commit 0b46cca

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ A collection of simple bash scripts.
1111
## Functions
1212
1. [What day is today?](scripts/what-day-today.sh): It prints which day of the week is today
1313
2. [Square a number](scripts/squarenumber.sh): A simple function that squares an integer
14+
15+
## $RANDOM
16+
1. [Guess number](scripts/guess-number-game.sh): User should guess a number from 1 to 100

scripts/guess-number-game.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
# Lucas Saito, 2020/04/08
3+
# Simple game to guess a number from 1 to 100
4+
# The person has 5 tries to guess the number
5+
6+
7+
RANDOM_NUMBER=$(( $RANDOM % 100 + 1 ))
8+
TRIES=5
9+
GUESS=0
10+
11+
echo "Welcome to the guessing game!"
12+
echo "Guess a number from 1 to 100:"
13+
14+
while [[ GUESS -lt TRIES ]]; do
15+
read guess_number
16+
if [[ guess_number -eq RANDOM_NUMBER ]]; then
17+
echo "You've won! The number was $RANDOM_NUMBER"
18+
exit 0
19+
else
20+
TRIES=$((TRIES - 1))
21+
echo "You miss! You have ${TRIES} tries left!"
22+
fi
23+
done
24+
echo "You lost!"
25+
exit 0

0 commit comments

Comments
 (0)