-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverage_game
More file actions
executable file
·47 lines (43 loc) · 889 Bytes
/
Copy pathaverage_game
File metadata and controls
executable file
·47 lines (43 loc) · 889 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
41
42
43
44
45
46
47
#!/bin/bash
# playig 2/3 average game
# written by Ofek Nacht
# function that cjecks who is the closest
check_winner() {
calc=$(( $avg - ${arr[$i - 1]} ))
# absolute number is needed
acalc=${calc#-}
if (( $acalc < $close ))
then
close=$acalc
winner=$i
elif [[ $calc -eq $close ]]
then
winner="$winner and player$i"
fi
}
close=101
sum=0
check="5"
# choose how many players will play
read -p 'enter number of players :' players
# accepting answers from players
for (( i=1; i<=$players; i++ ))
do
read -p "player$i answer is: " answer
if ! (( $answer >= 0 && $answer <= 100 ))
then
echo not valid number
(( i-- ))
else
arr+=($answer)
sum=$(( $sum + $answer ))
fi
done
avg=$(( $sum / $players * 2/3 ))
# looping through array and check who is the winner
for (( i=1; i<=${#arr[@]}; i++ ))
do
check_winner
done
echo $avg
echo "the winner is player$winner "