forked from divyanshu-rawat/gameof3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.sh
58 lines (43 loc) · 1.12 KB
/
b.sh
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
48
49
50
51
52
53
54
55
56
57
58
#! /bin/bash
# Pipe Names
input=aOutput
output=bOutput
# set -x
if [ $# -eq 0 ]; then
player_name="player 2"
fi
if [ $# -eq 1 ]; then
player_name="$1"
fi
echo "Player Name: $player_name"
while true; do
## read number sent from Player 1
read current < "$input"
## the keyword "won" means that Player 1 has one.
if [[ "$current" -eq "won" ]]; then
echo
echo "LOSER: $player_name!"
exit
fi
echo "Number received: $current"
## find the nearest number, that is divisible by 3.
## Sbuilt-in command let for performing arithmetic operations.
if (( current % 3 == 0 )); then
let "next = ((current / 3))"
elif (( (current + 1) % 3 == 0 )); then
let "next = (((current + 1) / 3))"
elif (( (current - 1) % 3 == 0 )); then
let "next = (((current - 1) / 3))"
fi
echo "Number sent: $next"
## next == 1, implies Player 2 has won!.
if (( next == 1 )); then
echo
echo "WINNER: $player_name!"
echo "won" > "$output"
exit
fi
## else, Player 1 turn.
echo "$next" > "$output"
done
# set +x