1 /****************************************************************************
2 * Copyright 2019-2020,2021 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.42 2021/05/08 20:44:44 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;
131 int Line, peg, SlotNo;
132 char TileBuf[BUFSIZ];
135 MvAddStr(1, 24, "T O W E R S O F H A N O I");
136 MvAddStr(3, 34, "SJR 1990");
137 MvPrintw(19, 5, "Moves : %d of %.0f", NMoves, pow(2.0, (float) NTiles) - 1);
138 (void) attrset(A_REVERSE);
139 MvAddStr(BASELINE, 8,
142 for (Line = TOPLINE; Line < BASELINE; Line++) {
143 MvAddCh(Line, LEFTPEG, ' ');
144 MvAddCh(Line, MIDPEG, ' ');
145 MvAddCh(Line, RIGHTPEG, ' ');
147 MvAddCh(BASELINE, LEFTPEG, '1');
148 MvAddCh(BASELINE, MIDPEG, '2');
149 MvAddCh(BASELINE, RIGHTPEG, '3');
150 (void) attrset(A_NORMAL);
153 for (peg = 0; peg < NPEGS; peg++) {
154 for (SlotNo = 0; SlotNo < Pegs[peg].Count; SlotNo++) {
155 size_t len = Pegs[peg].Length[SlotNo];
156 if (len < sizeof(TileBuf) - 1 && len < (size_t) PegPos[peg]) {
157 memset(TileBuf, ' ', len);
160 (void) attrset(AttrArg(COLOR_PAIR(LENTOIND(len)), 0));
162 (void) attrset(A_REVERSE);
163 MvAddStr(BASELINE - (SlotNo + 1),
164 (PegPos[peg] - (int) len / 2),
169 (void) attrset(A_NORMAL);
174 GetMove(int *From, int *To)
176 MvAddStr(STATUSLINE, 0, "Next move ('q' to quit) from ");
179 if ((*From = getch()) == 'q')
186 if ((*To = getch()) == 'q')
200 MakeMove(int From, int To)
203 Pegs[To].Length[Pegs[To].Count] = Pegs[From].Length[Pegs[From].Count];
210 AutoMove(int From, int To, int Num)
216 AutoMove(From, OTHER(From, To), Num - 1);
219 AutoMove(OTHER(From, To), To, Num - 1);
228 for (i = 1; i < NPEGS; i++)
229 if (Pegs[i].Count == NumTiles)
237 static const char *msg[] =
239 "Usage: hanoi [options] [[<No Of Tiles>] [a]]"
242 #if HAVE_USE_DEFAULT_COLORS
243 ," -d invoke use_default_colors"
245 ," -n NUM set number of tiles (positional param is deprecated)"
246 ," -X solve automatically (positional \"a\" is deprecated)"
250 for (n = 0; n < SIZEOF(msg); n++)
251 fprintf(stderr, "%s\n", msg[n]);
253 ExitProgram(EXIT_FAILURE);
257 main(int argc, char **argv)
259 int ch, FromCol, ToCol;
261 #if HAVE_USE_DEFAULT_COLORS
262 bool d_option = FALSE;
265 NTiles = DEFAULTTILES;
266 while ((ch = getopt(argc, argv, "dn:X")) != -1) {
268 #if HAVE_USE_DEFAULT_COLORS
274 NTiles = atoi(optarg);
284 setlocale(LC_ALL, "");
286 switch (argc - optind) {
288 if (strcmp(argv[optind + 1], "a")) {
294 NTiles = atoi(argv[optind]);
302 if (NTiles > MAXTILES || NTiles < MINTILES) {
303 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
310 short bg = COLOR_BLACK;
312 #if HAVE_USE_DEFAULT_COLORS
313 if (d_option && (use_default_colors() == OK))
316 for (i = 0; i < 9; i++)
317 init_pair((short) (i + 1), bg, TileColour[i]);
322 fprintf(stderr, "Min screen length 24 lines\n");
323 ExitProgram(EXIT_FAILURE);
327 leaveok(stdscr, TRUE); /* Attempt to remove cursor */
334 AutoMove(0, 2, NTiles);
335 } while (!Solved(NTiles));
340 if (GetMove(&FromCol, &ToCol))
342 if (InvalidMove(FromCol, ToCol)) {
343 MvAddStr(STATUSLINE, 0, "Invalid Move !!");
348 MakeMove(FromCol, ToCol);
349 if (Solved(NTiles)) {
350 MvPrintw(STATUSLINE, 0,
351 "Well Done !! You did it in %d moves", NMoves);
359 ExitProgram(EXIT_SUCCESS);