2 * Name: Towers of Hanoi.
5 * This is a playable copy of towers of hanoi.
6 * Its sole purpose is to demonstrate my Amiga Curses package.
7 * This program should compile on any system that has Curses.
8 * 'hanoi' will give a manual game with 7 playing pieces.
9 * 'hanoi n' will give a manual game with n playing pieces.
10 * 'hanoi n a' will give an auto solved game with n playing pieces.
12 * Author: Simon J Raybould (sie@fulcrum.bt.co.uk).
13 * (This version has been slightly modified by the ncurses maintainers.)
17 * $Id: hanoi.c,v 1.14 1997/05/06 23:07:55 tom Exp $
20 #include <test.priv.h>
24 #define NPEGS 3 /* This is not configurable !! */
27 #define DEFAULTTILES 7
30 #define STATUSLINE (LINES-3)
35 #define LENTOIND(x) (((x)-1)/2)
36 #define OTHER(a,b) (3-((a)+(b)))
39 size_t Length[MAXTILES];
43 static struct Peg Pegs[NPEGS];
44 static int PegPos[] = { LEFTPEG, MIDPEG, RIGHTPEG };
45 static int TileColour[] = {
46 COLOR_GREEN, /* Length 3 */
47 COLOR_MAGENTA, /* Length 5 */
48 COLOR_RED, /* Length 7 */
49 COLOR_BLUE, /* Length 9 */
50 COLOR_CYAN, /* Length 11 */
51 COLOR_YELLOW, /* Length 13 */
52 COLOR_GREEN, /* Length 15 */
53 COLOR_MAGENTA, /* Length 17 */
54 COLOR_RED, /* Length 19 */
56 static int NMoves = 0;
58 static void InitTiles(int NTiles);
59 static void DisplayTiles(void);
60 static void MakeMove(int From, int To);
61 static void AutoMove(int From, int To, int Num);
62 static void Usage(void);
63 static int Solved(int NumTiles);
64 static int GetMove(int *From, int *To);
65 static int InvalidMove(int From, int To);
68 main(int argc, char **argv)
70 int NTiles, FromCol, ToCol;
71 unsigned char AutoFlag = 0;
75 NTiles = DEFAULTTILES;
78 NTiles = atoi(argv[1]);
79 if (NTiles > MAXTILES || NTiles < MINTILES) {
80 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
85 if (strcmp(argv[2], "a")) {
89 NTiles = atoi(argv[1]);
90 if (NTiles > MAXTILES || NTiles < MINTILES) {
91 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
100 #ifdef NCURSES_VERSION
101 trace(TRACE_MAXIMUM);
106 int bg = COLOR_BLACK;
108 #ifdef NCURSES_VERSION
109 if (use_default_colors() == OK)
112 for (i = 0; i < 9; i++)
113 init_pair(i+1, bg, TileColour[i]);
118 fprintf(stderr, "Min screen length 24 lines\n");
122 leaveok(stdscr, TRUE); /* Attempt to remove cursor */
128 AutoMove(0, 2, NTiles);
129 } while(!Solved(NTiles));
134 if(GetMove(&FromCol, &ToCol))
136 if(InvalidMove(FromCol, ToCol)) {
137 mvaddstr(STATUSLINE, 0, "Invalid Move !!");
142 MakeMove(FromCol, ToCol);
144 mvprintw(STATUSLINE, 0, "Well Done !! You did it in %d moves", NMoves);
157 InvalidMove(int From, int To)
169 if(!Pegs[From].Count)
172 Pegs[From].Length[Pegs[From].Count-1] >
173 Pegs[To].Length[Pegs[To].Count-1])
179 InitTiles(int NTiles)
183 for(Size=NTiles*2+1, SlotNo=0; Size>=3; Size-=2)
184 Pegs[0].Length[SlotNo++] = Size;
186 Pegs[0].Count = NTiles;
194 int Line, Peg, SlotNo;
195 char TileBuf[BUFSIZ];
198 mvaddstr(1, 24, "T O W E R S O F H A N O I");
199 mvaddstr(3, 34, "SJR 1990");
200 mvprintw(19, 5, "Moves : %d", NMoves);
202 mvaddstr(BASELINE, 8, " ");
204 for(Line=TOPLINE; Line<BASELINE; Line++) {
205 mvaddch(Line, LEFTPEG, ' ');
206 mvaddch(Line, MIDPEG, ' ');
207 mvaddch(Line, RIGHTPEG, ' ');
209 mvaddch(BASELINE, LEFTPEG, '1');
210 mvaddch(BASELINE, MIDPEG, '2');
211 mvaddch(BASELINE, RIGHTPEG, '3');
215 for(Peg=0; Peg<NPEGS; Peg++) {
216 for(SlotNo=0; SlotNo<Pegs[Peg].Count; SlotNo++) {
217 memset(TileBuf, ' ', Pegs[Peg].Length[SlotNo]);
218 TileBuf[Pegs[Peg].Length[SlotNo]] = '\0';
220 attrset(COLOR_PAIR(LENTOIND(Pegs[Peg].Length[SlotNo])));
223 mvaddstr(BASELINE-(SlotNo+1),
224 (int)(PegPos[Peg] - Pegs[Peg].Length[SlotNo]/2),
233 GetMove(int *From, int *To)
235 mvaddstr(STATUSLINE, 0, "Next move ('q' to quit) from ");
238 if((*From = getch()) == 'q')
245 if((*To = getch()) == 'q')
258 MakeMove(int From, int To)
261 Pegs[To].Length[Pegs[To].Count] = Pegs[From].Length[Pegs[From].Count];
268 AutoMove(int From, int To, int Num)
275 AutoMove(From, OTHER(From, To), Num-1);
278 AutoMove(OTHER(From, To), To, Num-1);
286 for(i = 1; i < NPEGS; i++)
287 if (Pegs[i].Count == NumTiles)
295 fprintf(stderr, "Usage: hanoi [<No Of Tiles>] [a]\n");
296 fprintf(stderr, "The 'a' option causes the tower to be solved automatically\n");