Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Towers of Hanoi

Part of TutorialIntermediate

Description

Solves the Hanoi problem for seven discs.

Example

/+
 + hanoi!
 + solves the hanoi problem for seven discs
 +
 + by Brian Waters
 + edited by Chris Sauls, Jan 2, 2006
 +/

import std.stdio;

int main(char[][] args) {
  void solve(int from, int aux, int to, int numdiscs) {
    if (numdiscs == 0)
      return;
    solve(from, to, aux, numdiscs - 1);
    writefln("%d to %d", from, to);
    solve(aux, from, to, numdiscs - 1);
    return;
  }

  solve(1, 2, 3, 7);
  return 0;
}