1 /****************************************************************************
2 * Copyright (c) 1998-2002,2006 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.25 2006/04/22 22:41:22 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];
70 {LEFTPEG, MIDPEG, RIGHTPEG};
71 static int TileColour[] =
73 COLOR_GREEN, /* Length 3 */
74 COLOR_MAGENTA, /* Length 5 */
75 COLOR_RED, /* Length 7 */
76 COLOR_BLUE, /* Length 9 */
77 COLOR_CYAN, /* Length 11 */
78 COLOR_YELLOW, /* Length 13 */
79 COLOR_GREEN, /* Length 15 */
80 COLOR_MAGENTA, /* Length 17 */
81 COLOR_RED, /* Length 19 */
83 static int NMoves = 0;
85 static void InitTiles(int NTiles);
86 static void DisplayTiles(void);
87 static void MakeMove(int From, int To);
88 static void AutoMove(int From, int To, int Num);
89 static void Usage(void);
90 static int Solved(int NumTiles);
91 static int GetMove(int *From, int *To);
92 static int InvalidMove(int From, int To);
95 main(int argc, char **argv)
97 int NTiles, FromCol, ToCol;
100 setlocale(LC_ALL, "");
104 NTiles = DEFAULTTILES;
107 NTiles = atoi(argv[1]);
108 if (NTiles > MAXTILES || NTiles < MINTILES) {
109 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
110 ExitProgram(EXIT_FAILURE);
114 if (strcmp(argv[2], "a")) {
116 ExitProgram(EXIT_FAILURE);
118 NTiles = atoi(argv[1]);
119 if (NTiles > MAXTILES || NTiles < MINTILES) {
120 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
121 ExitProgram(EXIT_FAILURE);
127 ExitProgram(EXIT_FAILURE);
130 trace(TRACE_MAXIMUM);
135 int bg = COLOR_BLACK;
137 #if HAVE_USE_DEFAULT_COLORS
138 if (use_default_colors() == OK)
141 for (i = 0; i < 9; i++)
142 init_pair(i + 1, bg, TileColour[i]);
147 fprintf(stderr, "Min screen length 24 lines\n");
148 ExitProgram(EXIT_FAILURE);
152 leaveok(stdscr, TRUE); /* Attempt to remove cursor */
159 AutoMove(0, 2, NTiles);
160 } while (!Solved(NTiles));
165 if (GetMove(&FromCol, &ToCol))
167 if (InvalidMove(FromCol, ToCol)) {
168 mvaddstr(STATUSLINE, 0, "Invalid Move !!");
173 MakeMove(FromCol, ToCol);
174 if (Solved(NTiles)) {
175 mvprintw(STATUSLINE, 0,
176 "Well Done !! You did it in %d moves", NMoves);
184 ExitProgram(EXIT_SUCCESS);
188 InvalidMove(int From, int To)
200 if (!Pegs[From].Count)
202 if (Pegs[To].Count &&
203 Pegs[From].Length[Pegs[From].Count - 1] >
204 Pegs[To].Length[Pegs[To].Count - 1])
210 InitTiles(int NTiles)
214 for (Size = NTiles * 2 + 1, SlotNo = 0; Size >= 3; Size -= 2)
215 Pegs[0].Length[SlotNo++] = Size;
217 Pegs[0].Count = NTiles;
225 int Line, peg, SlotNo;
226 char TileBuf[BUFSIZ];
229 mvaddstr(1, 24, "T O W E R S O F H A N O I");
230 mvaddstr(3, 34, "SJR 1990");
231 mvprintw(19, 5, "Moves : %d", NMoves);
233 mvaddstr(BASELINE, 8,
236 for (Line = TOPLINE; Line < BASELINE; Line++) {
237 mvaddch(Line, LEFTPEG, ' ');
238 mvaddch(Line, MIDPEG, ' ');
239 mvaddch(Line, RIGHTPEG, ' ');
241 mvaddch(BASELINE, LEFTPEG, '1');
242 mvaddch(BASELINE, MIDPEG, '2');
243 mvaddch(BASELINE, RIGHTPEG, '3');
247 for (peg = 0; peg < NPEGS; peg++) {
248 for (SlotNo = 0; SlotNo < Pegs[peg].Count; SlotNo++) {
249 memset(TileBuf, ' ', Pegs[peg].Length[SlotNo]);
250 TileBuf[Pegs[peg].Length[SlotNo]] = '\0';
252 attrset(COLOR_PAIR(LENTOIND(Pegs[peg].Length[SlotNo])));
255 mvaddstr(BASELINE - (SlotNo + 1),
256 (int) (PegPos[peg] - Pegs[peg].Length[SlotNo] / 2),
265 GetMove(int *From, int *To)
267 mvaddstr(STATUSLINE, 0, "Next move ('q' to quit) from ");
270 if ((*From = getch()) == 'q')
277 if ((*To = getch()) == 'q')
290 MakeMove(int From, int To)
293 Pegs[To].Length[Pegs[To].Count] = Pegs[From].Length[Pegs[From].Count];
300 AutoMove(int From, int To, int Num)
307 AutoMove(From, OTHER(From, To), Num - 1);
310 AutoMove(OTHER(From, To), To, Num - 1);
318 for (i = 1; i < NPEGS; i++)
319 if (Pegs[i].Count == NumTiles)
327 fprintf(stderr, "Usage: hanoi [<No Of Tiles>] [a]\n");
329 "The 'a' option causes the tower to be solved automatically\n");