-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessing_game_pro.sh
executable file
·78 lines (65 loc) · 1.61 KB
/
Guessing_game_pro.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
#!/bin/bash
# Title
TITLE="WELCOME TO ICE ULTIMATE GAME"
figlet "$TITLE"
# inittializing WIN/LOSS
WIN=0
LOSS=0
# Function to display game modes
usage() {
echo "Select a game mode:"
echo "0. EASY (1-10)"
echo "1. NORMAL (1-25)"
echo "2. HARD (1-50)"
echo "3. SUPER HARD (1-100)"
echo "4. WIN TWICE AND RECEIVE 1K (1-1000)"
}
# Function to generate random number
generate_random_number() {
local max_number=$1
echo $((RANDOM % max_number + 1))
}
# Function to play the game
play_game() {
local mode=$1
local max_number
case $mode in
0) max_number=10 ;;
1) max_number=25 ;;
2) max_number=50 ;;
3) max_number=100 ;;
4) max_number=1000 ;;
esac
local rand_number=$(generate_random_number $max_number)
local attempts=0
local max_attempts=3
while [ $attempts -lt $max_attempts ]; do
read -p "Guess the number (1-$max_number): " guess_number
((attempts++))
if [ "$rand_number" -eq "$guess_number" ]; then
echo "Congratulations, You Won!"
WIN=$((WIN + 1))
return
elif [ "$rand_number" -gt "$guess_number" ]; then
echo "You missed! The random number is greater than $guess_number"
LOSS=$((LOSS + 1))
else
echo "You missed! The random number is lesser than $guess_number"
LOSS=$((LOSS + 1))
fi
done
echo "You lose, the number was $rand_number"
}
# Main game loop
while true; do
usage
read -p "Enter your choice: " mode
case $mode in
0|1|2|3|4) play_game $mode ;;
*) echo "Invalid choice. Please try again." ;;
esac
read -p "Play again? (y/n): " play_again
if [ "$play_again" != "y" ]; then
break
fi
done