]> git.sur5r.net Git - cc65/blob - test/ref/hanoi.c
remote TABs in doc/ and test/
[cc65] / test / ref / hanoi.c
1 /*
2   !!DESCRIPTION!! solves the "towers of hanoi" problem
3   !!ORIGIN!!      BYTE UNIX Benchmarks
4   !!LICENCE!!     Public Domain
5 */
6
7 /*******************************************************************************
8  *  The BYTE UNIX Benchmarks - Release 3
9  *          Module: hanoi.c   SID: 3.3 5/15/91 19:30:20
10  *
11  *******************************************************************************
12  * Bug reports, patches, comments, suggestions should be sent to:
13  *
14  *      Ben Smith, Rick Grehan or Tom Yager
15  *      ben@bytepb.byte.com   rick_g@bytepb.byte.com   tyager@bytepb.byte.com
16  *
17  *******************************************************************************
18  *  Modification Log:
19  *  $Header: hanoi.c,v 3.5 87/08/06 08:11:14 kenj Exp $
20  *  August 28, 1990 - Modified timing routines (ty)
21  *
22  ******************************************************************************/
23
24 #define VERBOSE
25 /*#define USECMDLINE*/
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 unsigned short iter = 0; /* number of iterations */
31 char num[4];
32 long cnt;
33
34 int disk=5,       /* default number of disks */
35     duration=10;  /* default time to run test */
36
37 void mov(unsigned char n,unsigned char f,unsigned char t)
38 {
39 char o;
40
41         if(n == 1)
42         {
43                 num[f]--;
44                 num[t]++;
45         }
46         else
47         {
48                 o = (6-(f+t));
49                 mov(n-1,f,o);
50                 mov(1,f,t);
51                 mov(n-1,o,t);
52         }
53
54         #ifdef VERBOSE
55         printf("%2d: %2d %2d %2d %2d\n",
56                 (int)iter,(int)num[0],(int)num[1],(int)num[2],(int)num[3]);
57         #endif
58 }
59
60 int main(int argc,char **argv)
61 {
62         #ifdef USECMDLINE
63         if (argc < 2) {
64                 printf("Usage: %s [duration] [disks]\n", argv[0]);
65                 exit(1);
66         }
67         else
68         {
69                 if(argc > 1) duration = atoi(argv[1]);
70                 if(argc > 2) disk = atoi(argv[2]);
71         }
72         #endif
73
74         printf("towers of hanoi\ndisks: %d\n\n",disk);
75
76         num[1] = disk;
77
78         #ifdef VERBOSE
79         printf("%2d: %2d %2d %2d %2d\n",
80                 (int)iter,(int)num[0],(int)num[1],(int)num[2],(int)num[3]);
81         #endif
82
83         while(num[3]<disk)
84         {
85                 mov(disk,1,3);
86                 ++iter;
87         }
88
89         return 0;
90 }