1 /****************************************************************************
2 * Copyright 2019-2021,2022 Thomas E. Dickey *
3 * Copyright 1998-2014,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * Name: Towers of Hanoi.
33 * This is a playable copy of towers of hanoi.
34 * Its sole purpose is to demonstrate my Amiga Curses package.
35 * This program should compile on any system that has Curses.
36 * 'hanoi' will give a manual game with 7 playing pieces.
37 * 'hanoi n' will give a manual game with n playing pieces.
38 * 'hanoi n a' will give an auto solved game with n playing pieces.
40 * Author: Simon J Raybould (sie@fulcrum.bt.co.uk).
41 * (This version has been slightly modified by the ncurses maintainers.)
45 * $Id: hanoi.c,v 1.47 2022/12/04 00:40:11 tom Exp $
48 #include <test.priv.h>
51 #define NPEGS 3 /* This is not configurable !! */
54 #define DEFAULTTILES 7
57 #define STATUSLINE (LINES-3)
62 #define LENTOIND(x) (((int)(x)-1)/2)
63 #define OTHER(a,b) (3-((a)+(b)))
66 size_t Length[MAXTILES];
70 static struct Peg Pegs[NPEGS];
77 static short TileColour[] =
79 COLOR_GREEN, /* Length 3 */
80 COLOR_MAGENTA, /* Length 5 */
81 COLOR_RED, /* Length 7 */
82 COLOR_BLUE, /* Length 9 */
83 COLOR_CYAN, /* Length 11 */
84 COLOR_YELLOW, /* Length 13 */
85 COLOR_GREEN, /* Length 15 */
86 COLOR_MAGENTA, /* Length 17 */
87 COLOR_RED, /* Length 19 */
89 static int NTiles = 0;
90 static int NMoves = 0;
91 static bool AutoFlag = FALSE;
94 InvalidMove(int From, int To)
106 if (!Pegs[From].Count)
108 if (Pegs[To].Count &&
109 Pegs[From].Length[Pegs[From].Count - 1] >
110 Pegs[To].Length[Pegs[To].Count - 1])
120 for (Size = NTiles * 2 + 1, SlotNo = 0; Size >= 3; Size -= 2)
121 Pegs[0].Length[SlotNo++] = (size_t) Size;
123 Pegs[0].Count = NTiles;
140 int Line, peg, SlotNo;
141 char TileBuf[BUFSIZ];
144 MvAddStr(1, 24, "T O W E R S O F H A N O I");
145 MvAddStr(3, 34, "SJR 1990");
146 MvPrintw(19, 5, "Moves : %d of %d", NMoves, two2n(NTiles) - 1);
147 (void) attrset(A_REVERSE);
148 MvAddStr(BASELINE, 8,
151 for (Line = TOPLINE; Line < BASELINE; Line++) {
152 MvAddCh(Line, LEFTPEG, ' ');
153 MvAddCh(Line, MIDPEG, ' ');
154 MvAddCh(Line, RIGHTPEG, ' ');
156 MvAddCh(BASELINE, LEFTPEG, '1');
157 MvAddCh(BASELINE, MIDPEG, '2');
158 MvAddCh(BASELINE, RIGHTPEG, '3');
159 (void) attrset(A_NORMAL);
162 for (peg = 0; peg < NPEGS; peg++) {
163 for (SlotNo = 0; SlotNo < Pegs[peg].Count; SlotNo++) {
164 size_t len = Pegs[peg].Length[SlotNo];
165 if (len < sizeof(TileBuf) - 1 && len < (size_t) PegPos[peg]) {
166 memset(TileBuf, ' ', len);
169 (void) attrset(AttrArg(COLOR_PAIR(LENTOIND(len)), 0));
171 (void) attrset(A_REVERSE);
172 MvAddStr(BASELINE - (SlotNo + 1),
173 (PegPos[peg] - (int) len / 2),
178 (void) attrset(A_NORMAL);
183 GetMove(int *From, int *To)
185 MvAddStr(STATUSLINE, 0, "Next move ('q' to quit) from ");
188 if ((*From = getch()) == 'q')
195 if ((*To = getch()) == 'q')
209 MakeMove(int From, int To)
212 Pegs[To].Length[Pegs[To].Count] = Pegs[From].Length[Pegs[From].Count];
219 AutoMove(int From, int To, int Num)
225 AutoMove(From, OTHER(From, To), Num - 1);
228 AutoMove(OTHER(From, To), To, Num - 1);
237 for (i = 1; i < NPEGS; i++)
238 if (Pegs[i].Count == NumTiles)
246 static const char *msg[] =
248 "Usage: hanoi [options] [[<No Of Tiles>] [a]]"
252 #if HAVE_USE_DEFAULT_COLORS
253 ," -d invoke use_default_colors"
255 ," -n NUM set number of tiles (positional param is deprecated)"
256 ," -X solve automatically (positional \"a\" is deprecated)"
260 for (n = 0; n < SIZEOF(msg); n++)
261 fprintf(stderr, "%s\n", msg[n]);
263 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
270 main(int argc, char **argv)
272 int ch, FromCol, ToCol;
274 #if HAVE_USE_DEFAULT_COLORS
275 bool d_option = FALSE;
278 NTiles = DEFAULTTILES;
279 while ((ch = getopt(argc, argv, OPTS_COMMON "dn:X")) != -1) {
281 #if HAVE_USE_DEFAULT_COLORS
287 NTiles = atoi(optarg);
294 ExitProgram(EXIT_SUCCESS);
296 usage(ch == OPTS_USAGE);
300 setlocale(LC_ALL, "");
302 switch (argc - optind) {
304 if (strcmp(argv[optind + 1], "a")) {
310 NTiles = atoi(argv[optind]);
318 if (NTiles > MAXTILES || NTiles < MINTILES) {
319 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
326 short bg = COLOR_BLACK;
328 #if HAVE_USE_DEFAULT_COLORS
329 if (d_option && (use_default_colors() == OK))
332 for (i = 0; i < 9; i++)
333 init_pair((short) (i + 1), bg, TileColour[i]);
338 fprintf(stderr, "Min screen length 24 lines\n");
339 ExitProgram(EXIT_FAILURE);
343 leaveok(stdscr, TRUE); /* Attempt to remove cursor */
350 AutoMove(0, 2, NTiles);
351 } while (!Solved(NTiles));
356 if (GetMove(&FromCol, &ToCol))
358 if (InvalidMove(FromCol, ToCol)) {
359 MvAddStr(STATUSLINE, 0, "Invalid Move !!");
364 MakeMove(FromCol, ToCol);
365 if (Solved(NTiles)) {
366 MvPrintw(STATUSLINE, 0,
367 "Well Done !! You did it in %d moves", NMoves);
375 ExitProgram(EXIT_SUCCESS);