1 /****************************************************************************
2 * Copyright (c) 1998-2014,2017 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.39 2017/09/09 00:19:24 tom Exp $
47 #include <test.priv.h>
50 #define NPEGS 3 /* This is not configurable !! */
53 #define DEFAULTTILES 7
56 #define STATUSLINE (LINES-3)
61 #define LENTOIND(x) (((int)(x)-1)/2)
62 #define OTHER(a,b) (3-((a)+(b)))
65 size_t Length[MAXTILES];
69 static struct Peg Pegs[NPEGS];
76 static short TileColour[] =
78 COLOR_GREEN, /* Length 3 */
79 COLOR_MAGENTA, /* Length 5 */
80 COLOR_RED, /* Length 7 */
81 COLOR_BLUE, /* Length 9 */
82 COLOR_CYAN, /* Length 11 */
83 COLOR_YELLOW, /* Length 13 */
84 COLOR_GREEN, /* Length 15 */
85 COLOR_MAGENTA, /* Length 17 */
86 COLOR_RED, /* Length 19 */
88 static int NTiles = 0;
89 static int NMoves = 0;
90 static bool AutoFlag = FALSE;
93 InvalidMove(int From, int To)
105 if (!Pegs[From].Count)
107 if (Pegs[To].Count &&
108 Pegs[From].Length[Pegs[From].Count - 1] >
109 Pegs[To].Length[Pegs[To].Count - 1])
119 for (Size = NTiles * 2 + 1, SlotNo = 0; Size >= 3; Size -= 2)
120 Pegs[0].Length[SlotNo++] = (size_t) Size;
122 Pegs[0].Count = NTiles;
130 int Line, peg, SlotNo;
131 char TileBuf[BUFSIZ];
134 MvAddStr(1, 24, "T O W E R S O F H A N O I");
135 MvAddStr(3, 34, "SJR 1990");
136 MvPrintw(19, 5, "Moves : %d of %.0f", NMoves, pow(2.0, (float) NTiles) - 1);
137 (void) attrset(A_REVERSE);
138 MvAddStr(BASELINE, 8,
141 for (Line = TOPLINE; Line < BASELINE; Line++) {
142 MvAddCh(Line, LEFTPEG, ' ');
143 MvAddCh(Line, MIDPEG, ' ');
144 MvAddCh(Line, RIGHTPEG, ' ');
146 MvAddCh(BASELINE, LEFTPEG, '1');
147 MvAddCh(BASELINE, MIDPEG, '2');
148 MvAddCh(BASELINE, RIGHTPEG, '3');
149 (void) attrset(A_NORMAL);
152 for (peg = 0; peg < NPEGS; peg++) {
153 for (SlotNo = 0; SlotNo < Pegs[peg].Count; SlotNo++) {
154 size_t len = Pegs[peg].Length[SlotNo];
155 if (len < sizeof(TileBuf) - 1 && len < (size_t) PegPos[peg]) {
156 memset(TileBuf, ' ', len);
159 (void) attrset(AttrArg(COLOR_PAIR(LENTOIND(len)), 0));
161 (void) attrset(A_REVERSE);
162 MvAddStr(BASELINE - (SlotNo + 1),
163 (PegPos[peg] - (int) len / 2),
168 (void) attrset(A_NORMAL);
173 GetMove(int *From, int *To)
175 MvAddStr(STATUSLINE, 0, "Next move ('q' to quit) from ");
178 if ((*From = getch()) == 'q')
185 if ((*To = getch()) == 'q')
199 MakeMove(int From, int To)
202 Pegs[To].Length[Pegs[To].Count] = Pegs[From].Length[Pegs[From].Count];
209 AutoMove(int From, int To, int Num)
215 AutoMove(From, OTHER(From, To), Num - 1);
218 AutoMove(OTHER(From, To), To, Num - 1);
227 for (i = 1; i < NPEGS; i++)
228 if (Pegs[i].Count == NumTiles)
236 static const char *msg[] =
238 "Usage: hanoi [options] [[<No Of Tiles>] [a]]"
241 #if HAVE_USE_DEFAULT_COLORS
242 ," -d invoke use_default_colors"
244 ," -n NUM set number of tiles (positional param is deprecated)"
245 ," -X solve automatically (positional \"a\" is deprecated)"
249 for (n = 0; n < SIZEOF(msg); n++)
250 fprintf(stderr, "%s\n", msg[n]);
252 ExitProgram(EXIT_FAILURE);
256 main(int argc, char **argv)
258 int ch, FromCol, ToCol;
260 #if HAVE_USE_DEFAULT_COLORS
261 bool d_option = FALSE;
264 NTiles = DEFAULTTILES;
265 while ((ch = getopt(argc, argv, "dn:X")) != -1) {
267 #if HAVE_USE_DEFAULT_COLORS
273 NTiles = atoi(optarg);
283 setlocale(LC_ALL, "");
285 switch (ch = (argc - optind)) {
287 if (strcmp(argv[optind + 1], "a")) {
293 NTiles = atoi(argv[optind]);
301 if (NTiles > MAXTILES || NTiles < MINTILES) {
302 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
309 short bg = COLOR_BLACK;
311 #if HAVE_USE_DEFAULT_COLORS
312 if (d_option && (use_default_colors() == OK))
315 for (i = 0; i < 9; i++)
316 init_pair((short) (i + 1), bg, TileColour[i]);
321 fprintf(stderr, "Min screen length 24 lines\n");
322 ExitProgram(EXIT_FAILURE);
326 leaveok(stdscr, TRUE); /* Attempt to remove cursor */
333 AutoMove(0, 2, NTiles);
334 } while (!Solved(NTiles));
339 if (GetMove(&FromCol, &ToCol))
341 if (InvalidMove(FromCol, ToCol)) {
342 MvAddStr(STATUSLINE, 0, "Invalid Move !!");
347 MakeMove(FromCol, ToCol);
348 if (Solved(NTiles)) {
349 MvPrintw(STATUSLINE, 0,
350 "Well Done !! You did it in %d moves", NMoves);
358 ExitProgram(EXIT_SUCCESS);