From ed02670e1644e051bc76740b873a20aace4b5d14 Mon Sep 17 00:00:00 2001 From: eslopfer Date: Thu, 12 Jan 2023 15:14:24 +0000 Subject: [PATCH] feat(joker-num): add solution to exercise --- sh/tests/solutions/joker-num.sh | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 sh/tests/solutions/joker-num.sh diff --git a/sh/tests/solutions/joker-num.sh b/sh/tests/solutions/joker-num.sh new file mode 100755 index 000000000..f004c1b20 --- /dev/null +++ b/sh/tests/solutions/joker-num.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +# Loop for player one +for (( ; ; )) +do + echo "Player one, please enter a number between 1 and 100000 (inclusive) and press enter:" + read -rs number + + # Check if input is empty + if [[ -z "$number" ]] + then + echo "Error: Input is empty, please try again." + continue + fi + + # Check if input is a number + if ! [[ "$number" =~ ^[0-9]+$ ]] + then + echo "Error: Input is not a number, please try again." + continue + fi + + # Check if input is between 1 and 100000 (inclusive) + if [[ "$number" -lt 1 || "$number" -gt 100000 ]] + then + echo "Error: Number out of range, please try again." + continue + fi + break +done + +# Start the for loop for player two +for (( ; ; )) +do + echo "Player two, please enter your guess:" + read -r guess + if [[ -z "$guess" ]] + then + echo "Error: Input is empty" + continue + fi + + # Check if input is a number + if ! [[ "$guess" =~ ^[0-9]+$ ]] + then + echo "Error: Input is not a number" + continue + fi + + # Check if guess is correct + if [[ "$guess" -eq "$number" ]] + then + echo "Congratulations! You guessed the number." + break + fi + + # Check if guess is too low or too high + if [[ "$guess" -lt "$number" ]] + then + echo "Go up." + else + echo "Go down." + fi +done