1 /****************************************************************************
2 * Copyright (c) 1998-2013,2014 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.36 2014/08/02 17:24:07 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;
92 static void InitTiles(void);
93 static void DisplayTiles(void);
94 static void MakeMove(int From, int To);
95 static void AutoMove(int From, int To, int Num);
96 static void Usage(void);
97 static int Solved(int NumTiles);
98 static int GetMove(int *From, int *To);
99 static int InvalidMove(int From, int To);
102 main(int argc, char **argv)
106 setlocale(LC_ALL, "");
110 NTiles = DEFAULTTILES;
113 NTiles = atoi(argv[1]);
114 if (NTiles > MAXTILES || NTiles < MINTILES) {
115 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
116 ExitProgram(EXIT_FAILURE);
120 if (strcmp(argv[2], "a")) {
122 ExitProgram(EXIT_FAILURE);
124 NTiles = atoi(argv[1]);
125 if (NTiles > MAXTILES || NTiles < MINTILES) {
126 fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
127 ExitProgram(EXIT_FAILURE);
133 ExitProgram(EXIT_FAILURE);
138 short bg = COLOR_BLACK;
140 #if HAVE_USE_DEFAULT_COLORS
141 if (use_default_colors() == OK)
144 for (i = 0; i < 9; i++)
145 init_pair((short) (i + 1), bg, TileColour[i]);
150 fprintf(stderr, "Min screen length 24 lines\n");
151 ExitProgram(EXIT_FAILURE);
155 leaveok(stdscr, TRUE); /* Attempt to remove cursor */
162 AutoMove(0, 2, NTiles);
163 } while (!Solved(NTiles));
168 if (GetMove(&FromCol, &ToCol))
170 if (InvalidMove(FromCol, ToCol)) {
171 MvAddStr(STATUSLINE, 0, "Invalid Move !!");
176 MakeMove(FromCol, ToCol);
177 if (Solved(NTiles)) {
178 MvPrintw(STATUSLINE, 0,
179 "Well Done !! You did it in %d moves", NMoves);
187 ExitProgram(EXIT_SUCCESS);
191 InvalidMove(int From, int To)
203 if (!Pegs[From].Count)
205 if (Pegs[To].Count &&
206 Pegs[From].Length[Pegs[From].Count - 1] >
207 Pegs[To].Length[Pegs[To].Count - 1])
217 for (Size = NTiles * 2 + 1, SlotNo = 0; Size >= 3; Size -= 2)
218 Pegs[0].Length[SlotNo++] = (size_t) Size;
220 Pegs[0].Count = NTiles;
228 int Line, peg, SlotNo;
229 char TileBuf[BUFSIZ];
232 MvAddStr(1, 24, "T O W E R S O F H A N O I");
233 MvAddStr(3, 34, "SJR 1990");
234 MvPrintw(19, 5, "Moves : %d of %.0f", NMoves, pow(2.0, (float) NTiles) - 1);
235 (void) attrset(A_REVERSE);
236 MvAddStr(BASELINE, 8,
239 for (Line = TOPLINE; Line < BASELINE; Line++) {
240 MvAddCh(Line, LEFTPEG, ' ');
241 MvAddCh(Line, MIDPEG, ' ');
242 MvAddCh(Line, RIGHTPEG, ' ');
244 MvAddCh(BASELINE, LEFTPEG, '1');
245 MvAddCh(BASELINE, MIDPEG, '2');
246 MvAddCh(BASELINE, RIGHTPEG, '3');
247 (void) attrset(A_NORMAL);
250 for (peg = 0; peg < NPEGS; peg++) {
251 for (SlotNo = 0; SlotNo < Pegs[peg].Count; SlotNo++) {
252 size_t len = Pegs[peg].Length[SlotNo];
253 if (len < sizeof(TileBuf) - 1 && len < (size_t) PegPos[peg]) {
254 memset(TileBuf, ' ', len);
257 (void) attrset(AttrArg(COLOR_PAIR(LENTOIND(len)), 0));
259 (void) attrset(A_REVERSE);
260 MvAddStr(BASELINE - (SlotNo + 1),
261 (PegPos[peg] - (int) len / 2),
266 (void) attrset(A_NORMAL);
271 GetMove(int *From, int *To)
273 MvAddStr(STATUSLINE, 0, "Next move ('q' to quit) from ");
276 if ((*From = getch()) == 'q')
283 if ((*To = getch()) == 'q')
297 MakeMove(int From, int To)
300 Pegs[To].Length[Pegs[To].Count] = Pegs[From].Length[Pegs[From].Count];
307 AutoMove(int From, int To, int Num)
313 AutoMove(From, OTHER(From, To), Num - 1);
316 AutoMove(OTHER(From, To), To, Num - 1);
325 for (i = 1; i < NPEGS; i++)
326 if (Pegs[i].Count == NumTiles)
334 fprintf(stderr, "Usage: hanoi [<No Of Tiles>] [a]\n");
336 "The 'a' option causes the tower to be solved automatically\n");