Browse Source

feat(joker-num): add new exercise to scripting piscine

DEV-4397-piscine-ai-missing-file-for-ex-7-of-nlp
Michele Sessa 1 year ago committed by Michele
parent
commit
3f21c5d32e
  1. 113
      sh/tests/joker-num_test.sh
  2. 66
      sh/tests/solutions/joker-num.sh
  3. 79
      subjects/devops/joker-num/README.md

113
sh/tests/joker-num_test.sh

@ -1,51 +1,76 @@
test_empty_input() { #!/usr/bin/env bash
output=$(echo -n "" | ./solutions/joker-num.sh)
expected_output="Error: Input is empty, please try again."
diff <(echo "$output") <(echo "$expected_output")
}
test_not_a_number() { IFS='
output=$(echo "a" | ./solutions/joker-num.sh) '
expected_output="Error: Input is not a number, please try again." script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd)
diff <(echo "$output") <(echo "$expected_output")
}
test_number_out_of_range() { challenge() {
output=$(echo "100001" | ./solutions/joker-num.sh) args=${@:1:$#-1}
expected_output="Error: Number out of range, please try again." input="${@: -1}"
diff <(echo "$output") <(echo "$expected_output")
}
test_correct_guess() { submitted=$(
output=$(echo "50000" | ./solutions/joker-num.sh; echo "50000" | ./solutions/joker-num.sh) ./student/joker-num.sh $args <<EOF
expected_output="Congratulations! You guessed the number." $input
diff <(echo "$output") <(echo "$expected_output") EOF
)
expected=$(
./solutions/joker-num.sh $args <<EOF
$input
EOF
)
diff <(echo "$submitted") <(echo "$expected")
} }
test_guess_too_low() { # Good input, win
output=$(echo "50000" | ./solutions/joker-num.sh; echo "49999" | ./solutions/joker-num.sh) input="1
expected_output="Go up." 100
diff <(echo "$output") <(echo "$expected_output") 49
} 51
50
"
test_guess_too_high() { # Good input, win
output=$(echo "50000" | ./solutions/joker-num.sh; echo "50001" | ./solutions/joker-num.sh) challenge 50 "1
expected_output="Go down." 100
diff <(echo "$output") <(echo "$expected_output") 49
} 51
test_player_one() { 50
test_empty_input "
test_not_a_number
test_number_out_of_range # Good input, lose
} challenge 50 "10
test_player_2() { 20
test_empty_input 30
test_not_a_number 40
test_number_out_of_range 41
test_correct_guess 42
test_guess_too_low "
test_guess_too_high
} # Bad arguments
challenge "10"
# Bad arguments
challenge 0 "10"
# Bad arguments
challenge 101 "10"
# Bad arguments
challenge -20 "10"
# Bad arguments
challenge aa "10"
# Handle bad input
challenge 78 "10
aa
test_player_1 3000
test_player_2 -10
0
0
40
80
79
78"

66
sh/tests/solutions/joker-num.sh

@ -1,63 +1,49 @@
#!/bin/bash #!/bin/bash
# Loop for player one if [[ $# != 1
for (( ; ; )) || -z "$1"
do || ! "$1" =~ ^[0-9]+$
echo "Player one, please enter a number between 1 and 100000 (inclusive) and press enter:" || "$1" -lt 1
read number || "$1" -gt 100 ]]
# timeout 1s read -s number then
# read -p "Player one, please enter a number between 1 and 100000 (inclusive) and press enter:" -s number echo "Error: wrong argument"
# sleep 5s exit 1
# Check if input is empty fi
if [[ -z "$number" ]]
then
echo "Error: Input is empty, please try again."
# Check if input is a number number=$1
elif ! [[ "$number" =~ ^[0-9]+$ ]]
then
echo "Error: Input is not a number, please try again."
# Check if input is between 1 and 100000 (inclusive)
elif [[ "$number" -lt 1 || "$number" -gt 100000 ]]
then
echo "Error: Number out of range, please try again."
else
break
fi
done
# Start the for loop for player two # Start the for loop for player two
for (( ; ; )) for (( tries_left=5 ; tries_left > 0; tries_left-- ))
do do
echo "Player two, please enter your guess:" echo "Enter your guess ($tries_left tries left):"
read guess read guess
if [[ -z "$guess" ]] if [[ $? < 0 ]]
then then
echo "Error: Input is empty" exit 1
continue
fi fi
# Check if input is a number if [[ -z "$guess"
if ! [[ "$guess" =~ ^[0-9]+$ ]] || ! "$guess" =~ ^[0-9]+$
|| "$guess" -lt 1
|| "$guess" -gt 100 ]]
then then
echo "Error: Input is not a number" tries_left=$tries_left+1
continue continue
fi fi
# Check if guess is correct
if [[ "$guess" -eq "$number" ]] if [[ "$guess" -eq "$number" ]]
then then
echo "Congratulations! You guessed the number." echo "Congratulations, you found the number in $((5-$tries_left+1)) moves!"
break exit
fi fi
# Check if guess is too low or too high
if [[ "$guess" -lt "$number" ]] if [[ "$guess" -lt "$number" ]]
then then
echo "Go up." echo "Go up"
else else
echo "Go down." echo "Go down"
fi fi
done done
echo "You lost, the number was $number"

79
subjects/devops/joker-num/README.md

@ -2,45 +2,76 @@
### Instructions ### Instructions
In this exercise you are going to create a guessing game. The script will prompt player one to submit a number between 1 and 100000 (inclusive) and hide the input from the other player. The script will use a for loop to give player two an infinite amount of chances to guess the number. You will also need to check if the input of player one is valid and if it is not ask again. In this exercise you are going to create a guessing game.
The script will receive the number to guess as argument. The number should be between 1 and 100 (inclusive). The player will then have 5 tries to guess the number.
To achieve this you will have to use a `for` loop.
Player two will be prompted with a visible prompt to guess the number, then the script will check if the guess is correct, too low or too high. When guessing a valid number the output will be:
- Number bigger than the secret one: `Go down`.
- Number smaller than the secret one: `Go up`.
- Number equal than the secret one: `Congratulations, you found the number!`.
- Ran out of tries: `You lost, the number was <number>`.
Your script needs to check if the input is empty, not a number or not between 1 and 100000 (inclusive). ### Usage
- If the input is empty the script will output `Error: Input is not a number` and continue the script execution. ```console
- If the input contains multiple numbers, the script will output `Error: Input is not a number` and continue the script execution. $ ./joker-num.sh 55
- If the input is not a number, the script will output as well `Error: Input is not a number`. Enter your guess (5 tries left):
- If the input is not between 1 and 100000 (inclusive), the script will output `Error: Number out of range` and continue the script execution. 50
Go up
Enter your guess (4 tries left):
75
Go down
Enter your guess (3 tries left):
55
Congratulations, you found the number in 3 moves!
$ ./joker-num.sh 70
Enter your guess (5 tries left):
70
Congratulations, you found the number in 1 moves!
$
```
If the guess is correct the script will display `Congratulations! You guessed the number.` If the guess is lower than the number, the script will display `Go up.` If the guess is higher, the script will display `Go down.` ### Error handling
### Usage - Wrong number of arguments or number not between `1` and `100`: The program will print `Error: wrong argument` and return `1`.
- The player writing something that is not a valid number as an answer: The program will ask again to enter a guess and the number of tries won't be decremented.
Here an example for the last scenario:
```console ```console
$ ./joker-num.sh $ ./joker-num.sh 100
Player one, please enter a number between 1 and 100000 (inclusive) and press enter: Enter your guess (5 tries left):
Player two, please enter your guess: aaaa
asdf Enter your guess (5 tries left):
Error: Input is not a number # Here the player only press enter
Player two, please enter your guess: Enter your guess (5 tries left):
100 10
Go down. Go up
Player two, please enter your guess: Enter your guess (4 tries left):
20
Go up
Enter your guess (3 tries left):
10000
Enter your guess (3 tries left):
30 30
Player two, please enter your guess: Go up
34 Enter your guess (2 tries left):
Congratulations! You guessed the number. 40
Go up
Enter your guess (1 tries left):
50
Go up
You lost, the number was 100
``` ```
### Hints ### Hints
- `read` : this command can be used to read input from the command line. To be able to input and hide sensitive information like passwords, the `-s` flag can be used. - `read`: this command can be used to read input from the command line.
For example: For example:
```console ```console
$ read -s password $ read guess
``` ```
After running that line, you would type your password, press enter, and it would be stored in the `$password` variable for later use. After running that line, you would type your password, press `Enter`, and it would be stored in the `$password` variable for later use.

Loading…
Cancel
Save