#!/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# Copyright (C) 2008 Vino Fernando Crescini
# Towers of Hanoi
# hanoi(peg_init, peg_dest, peg_temp, n)
function hanoi
{
if [ $4 -ne 0 ]; then
# move n - 1 discs from peg_init to peg_temp using peg_dest as temp
hanoi $1 $3 $2 $(($4 - 1))
# move nth disc from peg_init to peg_dest
echo "move disc $4 from peg $1 to peg $2"
# move n - 1 discs from peg_temp to peg_dest using peg_init as temp
hanoi $3 $2 $1 $(($4 - 1))
fi
}
peg_init="init"
peg_dest="dest"
peg_temp="temp"
if [ $# -ne 1 ] && [ $# -ne 4 ]; then
echo "usage: hanoi.pl [ ]"
exit 1;
fi
# is n a number?
tmp=${1//[0-9]/}
if [ "$tmp" != "" ]; then
echo "first argument must be a positive integer"
exit 2
fi
if [ $# -eq 4 ]; then
peg_init=$2
peg_dest=$3
peg_temp=$4
fi
hanoi $peg_init $peg_dest $peg_temp $1
exit 0;