1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * Name: Towers of Hanoi.
32 * This is a playable copy of towers of hanoi.
33 * Its sole purpose is to demonstrate my Amiga Curses package.
34 * This program should compile on any system that has Curses.
35 * 'hanoi' will give a manual game with 7 playing pieces.
36 * 'hanoi n' will give a manual game with n playing pieces.
37 * 'hanoi n a' will give an auto solved game with n playing pieces.
39 * Author: Simon J Raybould (sie@fulcrum.bt.co.uk).
40 * (This version has been slightly modified by the ncurses maintainers.)
44 * $Id: hanoi.c,v 1.31 2010/11/14 01:01:07 tom Exp $
47 #include <test.priv.h>
49 #define NPEGS 3 /* This is not configurable !! */
52 #define DEFAULTTILES 7
55 #define STATUSLINE (LINES-3)
60 #define LENTOIND(x) (((x)-1)/2)
61 #define OTHER(a,b) (3-((a)+(b)))
64 size_t Length
[MAXTILES
];
68 static struct Peg Pegs
[NPEGS
];
75 static short TileColour
[] =
77 COLOR_GREEN
, /* Length 3 */
78 COLOR_MAGENTA
, /* Length 5 */
79 COLOR_RED
, /* Length 7 */
80 COLOR_BLUE
, /* Length 9 */
81 COLOR_CYAN
, /* Length 11 */
82 COLOR_YELLOW
, /* Length 13 */
83 COLOR_GREEN
, /* Length 15 */
84 COLOR_MAGENTA
, /* Length 17 */
85 COLOR_RED
, /* Length 19 */
87 static int NMoves
= 0;
88 static bool AutoFlag
= FALSE
;
90 static void InitTiles(int NTiles
);
91 static void DisplayTiles(void);
92 static void MakeMove(int From
, int To
);
93 static void AutoMove(int From
, int To
, int Num
);
94 static void Usage(void);
95 static int Solved(int NumTiles
);
96 static int GetMove(int *From
, int *To
);
97 static int InvalidMove(int From
, int To
);
100 main(int argc
, char **argv
)
102 int NTiles
, FromCol
, ToCol
;
104 setlocale(LC_ALL
, "");
108 NTiles
= DEFAULTTILES
;
111 NTiles
= atoi(argv
[1]);
112 if (NTiles
> MAXTILES
|| NTiles
< MINTILES
) {
113 fprintf(stderr
, "Range %d to %d\n", MINTILES
, MAXTILES
);
114 ExitProgram(EXIT_FAILURE
);
118 if (strcmp(argv
[2], "a")) {
120 ExitProgram(EXIT_FAILURE
);
122 NTiles
= atoi(argv
[1]);
123 if (NTiles
> MAXTILES
|| NTiles
< MINTILES
) {
124 fprintf(stderr
, "Range %d to %d\n", MINTILES
, MAXTILES
);
125 ExitProgram(EXIT_FAILURE
);
131 ExitProgram(EXIT_FAILURE
);
134 trace(TRACE_MAXIMUM
);
139 short bg
= COLOR_BLACK
;
141 #if HAVE_USE_DEFAULT_COLORS
142 if (use_default_colors() == OK
)
145 for (i
= 0; i
< 9; i
++)
146 init_pair((short) (i
+ 1), bg
, TileColour
[i
]);
151 fprintf(stderr
, "Min screen length 24 lines\n");
152 ExitProgram(EXIT_FAILURE
);
156 leaveok(stdscr
, TRUE
); /* Attempt to remove cursor */
163 AutoMove(0, 2, NTiles
);
164 } while (!Solved(NTiles
));
169 if (GetMove(&FromCol
, &ToCol
))
171 if (InvalidMove(FromCol
, ToCol
)) {
172 MvAddStr(STATUSLINE
, 0, "Invalid Move !!");
177 MakeMove(FromCol
, ToCol
);
178 if (Solved(NTiles
)) {
179 MvPrintw(STATUSLINE
, 0,
180 "Well Done !! You did it in %d moves", NMoves
);
188 ExitProgram(EXIT_SUCCESS
);
192 InvalidMove(int From
, int To
)
204 if (!Pegs
[From
].Count
)
206 if (Pegs
[To
].Count
&&
207 Pegs
[From
].Length
[Pegs
[From
].Count
- 1] >
208 Pegs
[To
].Length
[Pegs
[To
].Count
- 1])
214 InitTiles(int NTiles
)
218 for (Size
= NTiles
* 2 + 1, SlotNo
= 0; Size
>= 3; Size
-= 2)
219 Pegs
[0].Length
[SlotNo
++] = (size_t) Size
;
221 Pegs
[0].Count
= NTiles
;
229 int Line
, peg
, SlotNo
;
230 char TileBuf
[BUFSIZ
];
233 MvAddStr(1, 24, "T O W E R S O F H A N O I");
234 MvAddStr(3, 34, "SJR 1990");
235 MvPrintw(19, 5, "Moves : %d", NMoves
);
236 (void) attrset(A_REVERSE
);
237 MvAddStr(BASELINE
, 8,
240 for (Line
= TOPLINE
; Line
< BASELINE
; Line
++) {
241 MvAddCh(Line
, LEFTPEG
, ' ');
242 MvAddCh(Line
, MIDPEG
, ' ');
243 MvAddCh(Line
, RIGHTPEG
, ' ');
245 MvAddCh(BASELINE
, LEFTPEG
, '1');
246 MvAddCh(BASELINE
, MIDPEG
, '2');
247 MvAddCh(BASELINE
, RIGHTPEG
, '3');
248 (void) attrset(A_NORMAL
);
251 for (peg
= 0; peg
< NPEGS
; peg
++) {
252 for (SlotNo
= 0; SlotNo
< Pegs
[peg
].Count
; SlotNo
++) {
253 size_t len
= Pegs
[peg
].Length
[SlotNo
];
254 if (len
< sizeof(TileBuf
) - 1 && len
< (size_t) PegPos
[peg
]) {
255 memset(TileBuf
, ' ', len
);
258 (void) attrset(COLOR_PAIR(LENTOIND(len
)));
260 (void) attrset(A_REVERSE
);
261 MvAddStr(BASELINE
- (SlotNo
+ 1),
262 (PegPos
[peg
] - (int) len
/ 2),
267 (void) attrset(A_NORMAL
);
272 GetMove(int *From
, int *To
)
274 MvAddStr(STATUSLINE
, 0, "Next move ('q' to quit) from ");
277 if ((*From
= getch()) == 'q')
284 if ((*To
= getch()) == 'q')
298 MakeMove(int From
, int To
)
301 Pegs
[To
].Length
[Pegs
[To
].Count
] = Pegs
[From
].Length
[Pegs
[From
].Count
];
308 AutoMove(int From
, int To
, int Num
)
315 AutoMove(From
, OTHER(From
, To
), Num
- 1);
318 AutoMove(OTHER(From
, To
), To
, Num
- 1);
326 for (i
= 1; i
< NPEGS
; i
++)
327 if (Pegs
[i
].Count
== NumTiles
)
335 fprintf(stderr
, "Usage: hanoi [<No Of Tiles>] [a]\n");
337 "The 'a' option causes the tower to be solved automatically\n");