1 # $NetBSD: hanoi-include.mk,v 1.1 2020/10/03 17:30:54 rillig Exp $
3 # Implements the Towers of Hanoi puzzle, thereby demonstrating a bunch of
4 # useful programming techniques:
6 # * default assignment using the ?= assignment operator
7 # * including the same file recursively
8 # * extracting the current value of a variable using the .for loop
9 # * using shell commands for calculations since make is a text processor
10 # * using the :: dependency operator for adding commands to a target
11 # * on-the-fly variable assignment expressions using the ::= modifier
14 # env N=3 make -f hanoi-include.mk
16 # make -f hanoi-include.mk N=3
18 N?= 5 # Move this number of disks ...
19 FROM?= A # ... from this stack ...
20 VIA?= B # ... via this stack ...
21 TO?= C # ... to this stack.
24 . for from to in ${FROM} ${TO}
26 @echo "Move the upper disk from stack ${from} to stack ${to}."
29 _:= ${N::!=expr $N - 1} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}}
30 . include "${.PARSEDIR}/${.PARSEFILE}"
31 _:= ${N::!=expr $N + 1} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}}
33 . for from to in ${FROM} ${TO}
35 @echo "Move the upper disk from stack ${from} to stack ${to}."
38 _:= ${N::!=expr $N - 1} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}}
39 . include "${.PARSEDIR}/${.PARSEFILE}"
40 _:= ${N::!=expr $N + 1} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}}