Advertisement
j0h

guess.sh

j0h
May 5th, 2023
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.61 KB | None | 0 0
  1. #!/bin/bash
  2. # bash based guessing game.
  3. # Define the game function
  4. function play_game() {
  5.   echo "Welcome to the game!"
  6.   echo "Guess a number between 1 and 100:"
  7.   secret_number=$((1 + RANDOM % 100))
  8.   attempts=0
  9.     while true; do
  10.     read -p "Enter your guess: " guess
  11.     ((attempts++))
  12.     if [[ $guess -eq $secret_number ]]; then
  13.       echo "Congratulations! You guessed the secret number in $attempts attempts."
  14.       break
  15.     elif [[ $guess -lt $secret_number ]]; then
  16.       echo "Too low, try again."
  17.     else
  18.       echo "Too high, try again."
  19.     fi
  20.   done
  21. }
  22.  
  23. # Call the game function
  24. play_game
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement