Browse Source

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

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

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

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

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

@ -2,45 +2,76 @@
### 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.
- If the input contains multiple numbers, the script will output `Error: Input is not a number` and continue the script execution.
- If the input is not a number, the script will output as well `Error: Input is not a number`.
- If the input is not between 1 and 100000 (inclusive), the script will output `Error: Number out of range` and continue the script execution.
```console
$ ./joker-num.sh 55
Enter your guess (5 tries left):
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
$ ./joker-num.sh
Player one, please enter a number between 1 and 100000 (inclusive) and press enter:
Player two, please enter your guess:
asdf
Error: Input is not a number
Player two, please enter your guess:
100
Go down.
Player two, please enter your guess:
$ ./joker-num.sh 100
Enter your guess (5 tries left):
aaaa
Enter your guess (5 tries left):
# Here the player only press enter
Enter your guess (5 tries left):
10
Go up
Enter your guess (4 tries left):
20
Go up
Enter your guess (3 tries left):
10000
Enter your guess (3 tries left):
30
Player two, please enter your guess:
34
Congratulations! You guessed the number.
Go up
Enter your guess (2 tries left):
40
Go up
Enter your guess (1 tries left):
50
Go up
You lost, the number was 100
```
### 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:
```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