fehler127: WTF - autobreak expects this to compile.
[libfirm] / ir / be / test / Hanoi.c
1 /*
2  * Project:     GCC-firm
3  * File name:   test/Hanoi.c
4  * Purpose:     Towers of hanoi
5  * Author:      Arne Frick (in Sather-k)
6  * Modified by: Michael Beck (for C)
7  * Created:     XX.11.2001
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001 Universitaet Karlsruhe
10  * Licence:
11  */
12
13 #include <stdlib.h>
14 #include <stdio.h>
15
16 typedef int boolean;
17
18 #define true    1
19 #define false   0
20
21 static int stick[3];
22 static int moves;
23 static boolean verbose = false;
24
25 void init(int n) {
26   //-- initializes an array of pegs
27   stick[0] = n;
28   stick[1] = 0;
29   stick[2] = 0;
30   moves = 0;
31 }
32
33 void hanoi(int n, int from, int to) {
34   int spare = 3 - from - to;
35   if(n > 0) {
36     //-- moves the stack of pegs from stick[from] via stick[temp]
37     //-- to stick[to]
38
39     hanoi (n-1, from, spare);
40     ++moves;
41     stick[from] = stick[from] - 1;
42     stick[to] = stick[to] + 1;
43     if (verbose) {
44       printf("move %d to %d\n", from, to);
45     }
46     hanoi (n-1, spare, to);
47   }
48 }
49
50 //--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
52 int main(int argc, char *argv[]) {
53   int i = 1;
54   boolean n_specified = false;
55
56   int n;
57
58   /*
59     if args.asize > i  and  args[i].equals ("-v") {
60     verbose = true;
61     i = i + 1;
62     }
63   */
64   printf("Hanoi.c\n");
65
66   if (argc <= 1) {
67     /*
68       if i /= args.asize - 1 {
69       << " wrong # of arguments\n\n"
70       "Usage: hanoi [-v] n\n"
71       "   where n is the number of pegs\n"
72       "   -v enables verbose printing of moves\n";
73     */
74       printf("Usage: hanoi n\nWhere n is the number of pegs.\nContinuing with default: n = 21 (-> 2097151)\n");
75       n = 21;
76   }
77   else {
78     n = atoi(argv[1]);
79   }
80
81   init (n);
82   hanoi (n, 0, 2);
83   //<< "Total #moves: " << p.moves << "\n";
84   // hanoi(28) = 268435455
85   printf(" number of moves : %d\n", moves);
86
87   return 0;
88 }