aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/abigail/awk/ch-2.awk
blob: 9748d637d316ce47a45fb16a52ab366dded6a263 (plain)
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
#!/usr/bin/awk

#
# See ../README.md
#

#
# Run as: awk -f ch-2.awk
#

BEGIN {
    tokens   = 12
    max_take =  3

    while (tokens > 0) {
        printf "How many tokens do you take? (%2d token%s are left) ", \
                tokens, (tokens == 1 ? "" : "s");
        if ((getline take) > 0) {
            if (1 <= take && take <= max_take) {
                takes = max_take + 1 - take
                printf "Computer takes %d token%s\n", \
                        takes, (takes == 1 ? "" : "s")
                tokens -= (max_take + 1)
            }
        }
    }

    print "Computer wins"
}