Towers
Towers
The Towers of Hanoi is an ancient puzzle consisting of a number of disks placed on three columns.
The disks all have different diameters and holes in the middle so they will fit over the columns.
All the disks start out on column A
.
The object of the puzzle is to transfer all the disks from column A
to column C
.
Only one disk can be moved at a time, and no disk can be placed on a disk that’s smaller than itself
$ dart 05_recursion/towers.dart
import 'dart:io';
class TowerApp {
static int nDisk = 3;
static void doTowers(int topN, String from, String inter, String to) {
if (topN == 1) {
stdout.writeln('Disk 1 from $from to $to');
} else {
doTowers(topN - 1, from, to, inter);
stdout.writeln('Disk $topN from $from to $to');
doTowers(topN - 1, inter, from, to);
}
}
}
void main(List<String> args) {
TowerApp.doTowers(TowerApp.nDisk, 'A', 'B', 'C');
}