mirror of https://github.com/01-edu/public.git
Michele Sessa
2 years ago
committed by
Michele
3 changed files with 150 additions and 108 deletions
@ -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 |
||||||
|
" |
||||||
|
|
||||||
|
# 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_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 |
|
||||||
} |
|
||||||
|
|
||||||
test_player_1 |
3000 |
||||||
test_player_2 |
-10 |
||||||
|
0 |
||||||
|
0 |
||||||
|
40 |
||||||
|
80 |
||||||
|
79 |
||||||
|
78" |
||||||
|
@ -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 |
|
||||||
# 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." |
|
||||||
|
|
||||||
# Check if input is a number |
|
||||||
elif ! [[ "$number" =~ ^[0-9]+$ ]] |
|
||||||
then |
then |
||||||
echo "Error: Input is not a number, please try again." |
echo "Error: wrong argument" |
||||||
|
exit 1 |
||||||
# 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 |
fi |
||||||
done |
|
||||||
|
number=$1 |
||||||
|
|
||||||
# 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" |
||||||
|
Loading…
Reference in new issue