forked from Valsuh45/number-guessing-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-game.sh
executable file
·109 lines (91 loc) · 2.8 KB
/
my-game.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Initialize variables
high_score=999 # Store high score across sessions
attempts=0 # Count attempts
# Function to display welcome message with colorful text
function welcome_message() {
tput setaf 2 # Green text
echo "===================================================="
echo " 🎮 Welcome to the Number Guessing Game 🎮"
echo "===================================================="
tput sgr0 # Reset text color
}
# Function to set the difficulty level and inform the user of the range
function set_difficulty() {
while true; do # Loop until valid input is received
echo "Choose a difficulty level:
1. Easy
2. Medium
3. Hard"
read -p "Enter 1, 2, or 3: " difficulty
case $difficulty in
1)
max_number=50; max_attempts=10
echo "You chose Easy. Guess a number between 1 and 50."
break
;; # Easy mode
2)
max_number=100; max_attempts=7
echo "You chose Medium. Guess a number between 1 and 100."
break
;; # Medium mode
3)
max_number=200; max_attempts=5
echo "You chose Hard. Guess a number between 1 and 200."
break
;; # Hard mode
*)
echo "Invalid choice. Please enter 1, 2, or 3." # Re-prompt
;;
esac
done
target=$((RANDOM % max_number + 1)) # Random target number
}
# Function to provide hints
function provide_hint() {
if (( target % 2 == 0 )); then
echo "Hint: The number is even."
else
echo "Hint: The number is odd."
fi
}
# Main game loop
function play_game() {
attempts=0 # Reset attempts for each game
while [[ $attempts -lt $max_attempts ]]; do
read -p "Enter your guess: " guess
attempts=$((attempts + 1))
if [[ $guess -lt $target ]]; then
echo "Too low! Attempts left: $((max_attempts - attempts))"
elif [[ $guess -gt $target ]]; then
echo "Too high! Attempts left: $((max_attempts - attempts))"
else
echo "🎉 Congratulations! You guessed the number in $attempts attempts."
# Check and update high score
if [[ $attempts -lt $high_score ]]; then
high_score=$attempts
echo "🏆 New High Score: $attempts attempts!"
else
echo "High Score: $high_score attempts."
fi
return # Exit the game loop
fi
# Provide a hint after 3 failed attempts
if [[ $attempts -eq 3 ]]; then
provide_hint
fi
done
echo "😞 Game over! The correct number was $target."
}
# Main program
welcome_message
while true; do
set_difficulty # Set difficulty level and target number
play_game # Start the game
# Ask if the player wants to play again
read -p "Do you want to play again? (y/n): " choice
if [[ $choice == "n" ]]; then
echo "Thanks for playing! Goodbye!"
break
fi
done