PHP
The Towers of Hanoi as a PHP script.
<?php
/*
* The Towers Of Hanoi
* PHP
* Copyright (C) 2001 Amit Singh. All Rights Reserved.
* http://hanoi.kernelthread.com
*/
$HANOI_MAXDISKS = 4;
function PHP_printmove($from, $to)
{
print "move $from ==> $to
\n";
}
function PHP_movedisk($from, $to, $using, $N)
{
if ($N <= 1) {
PHP_printmove($from, $to);
} else {
PHP_movedisk($from, $using, $to, $N - 1);
PHP_printmove($from, $to);
PHP_movedisk($using, $to, $from, $N - 1);
}
}
function PHP_hanoierror()
{
GLOBAL $HANOI_MAXDISKS;
GLOBAL $HTTP_HOST;
GLOBAL $PHP_SELF;
print "You must specify the number of disks (maximum $HANOI_MAXDISKS) as an
appropriate integer argument, for example:
http://$HTTP_HOST$PHP_SELF?N
where 1 <= N <= $HANOI_MAXDISKS";
}
function PHP_hanoi($N)
{
GLOBAL $HANOI_MAXDISKS;
if (!ereg("^[0-9]+", $N) || ($N > $HANOI_MAXDISKS)) {
PHP_hanoierror();
} else {
PHP_movedisk(1, 3, 2, $N);
}
}
?>
<?php
if ($argc != 1) {
PHP_hanoierror();
} else {
PHP_hanoi($argv[0]);
}
?>