-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbowling.sh
More file actions
40 lines (35 loc) · 1006 Bytes
/
bowling.sh
File metadata and controls
40 lines (35 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env bash
declare -A error_message=(
[incomplete]="Score cannot be taken until the end of the game"
[negative]="Negative roll is invalid"
[exceeds]="Pin count exceeds pins on the lane"
[game_over]="Cannot roll after game is over"
)
exit_error() {
echo "${error_message[$1]:-$1}" >&2; exit 1
}
validate_rolls() {
for roll in "$@"; do
[[ -z "$roll" ]] && exit_error incomplete
(( roll < 0 )) && exit_error negative
(( roll > 10 )) && exit_error exceeds
done
}
main() {
local frame score=0
for frame in {1..10}; do
validate_rolls "$1" "$2"
(( score += $1 + $2 ))
if (( $1 == 10 || $1 + $2 == 10 )); then
validate_rolls "$3"
(( score += "$3" ))
(( $1 == 10 && frame == 10 && $2 != 10 && $2 + $3 > 10 )) && exit_error exceeds
elif (( $1 + $2 < 10 )); then shift 2; continue
else exit_error exceeds
fi
shift $(( frame == 10 ? 3 : 2 - ($1 == 10) ))
done
(( $# > 0 )) && exit_error game_over
echo "$score"
}
main "$@"