aboutsummaryrefslogtreecommitdiff
path: root/contrib/ncurses/test/hanoi.c
diff options
context:
space:
mode:
authorPeter Wemm <peter@FreeBSD.org>2000-10-11 07:31:01 +0000
committerPeter Wemm <peter@FreeBSD.org>2000-10-11 07:31:01 +0000
commit18259542b2f8fa7e3f76f4bb0dd37995dfd424aa (patch)
tree9ec00b41242bbe63b83c0ac5316c539466d9f9de /contrib/ncurses/test/hanoi.c
parent15589c42fa2774d2f8ee650f4f31eb8d3a861316 (diff)
downloadsrc-18259542b2f8fa7e3f76f4bb0dd37995dfd424aa.tar.gz
src-18259542b2f8fa7e3f76f4bb0dd37995dfd424aa.zip
Import ncurses-5.1-20001009 onto the vendor branch
Notes
Notes: svn path=/vendor/ncurses/dist/; revision=66963
Diffstat (limited to 'contrib/ncurses/test/hanoi.c')
-rw-r--r--contrib/ncurses/test/hanoi.c375
1 files changed, 190 insertions, 185 deletions
diff --git a/contrib/ncurses/test/hanoi.c b/contrib/ncurses/test/hanoi.c
index 23a864624b01..0b032f8318ca 100644
--- a/contrib/ncurses/test/hanoi.c
+++ b/contrib/ncurses/test/hanoi.c
@@ -14,7 +14,7 @@
*
* Date: 05.Nov.90
*
- * $Id: hanoi.c,v 1.18 2000/04/01 20:01:08 tom Exp $
+ * $Id: hanoi.c,v 1.19 2000/09/02 18:51:16 tom Exp $
*/
#include <test.priv.h>
@@ -36,22 +36,24 @@
#define OTHER(a,b) (3-((a)+(b)))
struct Peg {
- size_t Length[MAXTILES];
- int Count;
+ size_t Length[MAXTILES];
+ int Count;
};
static struct Peg Pegs[NPEGS];
-static int PegPos[] = { LEFTPEG, MIDPEG, RIGHTPEG };
-static int TileColour[] = {
- COLOR_GREEN, /* Length 3 */
- COLOR_MAGENTA, /* Length 5 */
- COLOR_RED, /* Length 7 */
- COLOR_BLUE, /* Length 9 */
- COLOR_CYAN, /* Length 11 */
- COLOR_YELLOW, /* Length 13 */
- COLOR_GREEN, /* Length 15 */
- COLOR_MAGENTA, /* Length 17 */
- COLOR_RED, /* Length 19 */
+static int PegPos[] =
+{LEFTPEG, MIDPEG, RIGHTPEG};
+static int TileColour[] =
+{
+ COLOR_GREEN, /* Length 3 */
+ COLOR_MAGENTA, /* Length 5 */
+ COLOR_RED, /* Length 7 */
+ COLOR_BLUE, /* Length 9 */
+ COLOR_CYAN, /* Length 11 */
+ COLOR_YELLOW, /* Length 13 */
+ COLOR_GREEN, /* Length 15 */
+ COLOR_MAGENTA, /* Length 17 */
+ COLOR_RED, /* Length 19 */
};
static int NMoves = 0;
@@ -67,232 +69,235 @@ static int InvalidMove(int From, int To);
int
main(int argc, char **argv)
{
-int NTiles, FromCol, ToCol;
-unsigned char AutoFlag = 0;
+ int NTiles, FromCol, ToCol;
+ unsigned char AutoFlag = 0;
- switch(argc) {
- case 1:
- NTiles = DEFAULTTILES;
- break;
- case 2:
- NTiles = atoi(argv[1]);
- if (NTiles > MAXTILES || NTiles < MINTILES) {
- fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
- return EXIT_FAILURE;
- }
- break;
- case 3:
- if (strcmp(argv[2], "a")) {
- Usage();
- return EXIT_FAILURE;
- }
- NTiles = atoi(argv[1]);
- if (NTiles > MAXTILES || NTiles < MINTILES) {
- fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
- return EXIT_FAILURE;
- }
- AutoFlag = TRUE;
- break;
- default:
- Usage();
- return EXIT_FAILURE;
+ switch (argc) {
+ case 1:
+ NTiles = DEFAULTTILES;
+ break;
+ case 2:
+ NTiles = atoi(argv[1]);
+ if (NTiles > MAXTILES || NTiles < MINTILES) {
+ fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
+ return EXIT_FAILURE;
+ }
+ break;
+ case 3:
+ if (strcmp(argv[2], "a")) {
+ Usage();
+ return EXIT_FAILURE;
}
+ NTiles = atoi(argv[1]);
+ if (NTiles > MAXTILES || NTiles < MINTILES) {
+ fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
+ return EXIT_FAILURE;
+ }
+ AutoFlag = TRUE;
+ break;
+ default:
+ Usage();
+ return EXIT_FAILURE;
+ }
#ifdef TRACE
- trace(TRACE_MAXIMUM);
+ trace(TRACE_MAXIMUM);
#endif
- initscr();
- if (has_colors()) {
- int i;
- int bg = COLOR_BLACK;
- start_color();
-#ifdef HAVE_USE_DEFAULT_COLORS
- if (use_default_colors() == OK)
- bg = -1;
+ initscr();
+ if (has_colors()) {
+ int i;
+ int bg = COLOR_BLACK;
+ start_color();
+#if HAVE_USE_DEFAULT_COLORS
+ if (use_default_colors() == OK)
+ bg = -1;
#endif
- for (i = 0; i < 9; i++)
- init_pair(i+1, bg, TileColour[i]);
- }
- cbreak();
- if (LINES < 24) {
- endwin();
- fprintf(stderr, "Min screen length 24 lines\n");
- return EXIT_FAILURE;
- }
- if(AutoFlag) {
- curs_set(0);
- leaveok(stdscr, TRUE); /* Attempt to remove cursor */
- }
- InitTiles(NTiles);
- DisplayTiles();
- if(AutoFlag) {
- do {
- noecho();
- AutoMove(0, 2, NTiles);
- } while(!Solved(NTiles));
- sleep(2);
- } else {
- echo();
- for(;;) {
- if(GetMove(&FromCol, &ToCol))
- break;
- if(InvalidMove(FromCol, ToCol)) {
- mvaddstr(STATUSLINE, 0, "Invalid Move !!");
- refresh();
- beep();
- continue;
- }
- MakeMove(FromCol, ToCol);
- if(Solved(NTiles)) {
- mvprintw(STATUSLINE, 0, "Well Done !! You did it in %d moves", NMoves);
- refresh();
- sleep(5);
- break;
- }
- }
- }
+ for (i = 0; i < 9; i++)
+ init_pair(i + 1, bg, TileColour[i]);
+ }
+ cbreak();
+ if (LINES < 24) {
endwin();
- return EXIT_SUCCESS;
+ fprintf(stderr, "Min screen length 24 lines\n");
+ return EXIT_FAILURE;
+ }
+ if (AutoFlag) {
+ curs_set(0);
+ leaveok(stdscr, TRUE); /* Attempt to remove cursor */
+ }
+ InitTiles(NTiles);
+ DisplayTiles();
+ if (AutoFlag) {
+ do {
+ noecho();
+ AutoMove(0, 2, NTiles);
+ } while (!Solved(NTiles));
+ sleep(2);
+ } else {
+ echo();
+ for (;;) {
+ if (GetMove(&FromCol, &ToCol))
+ break;
+ if (InvalidMove(FromCol, ToCol)) {
+ mvaddstr(STATUSLINE, 0, "Invalid Move !!");
+ refresh();
+ beep();
+ continue;
+ }
+ MakeMove(FromCol, ToCol);
+ if (Solved(NTiles)) {
+ mvprintw(STATUSLINE, 0,
+ "Well Done !! You did it in %d moves", NMoves);
+ refresh();
+ sleep(5);
+ break;
+ }
+ }
+ }
+ endwin();
+ return EXIT_SUCCESS;
}
static int
InvalidMove(int From, int To)
{
- if(From >= NPEGS)
- return TRUE;
- if(From < 0)
- return TRUE;
- if(To >= NPEGS)
- return TRUE;
- if(To < 0)
- return TRUE;
- if(From == To)
- return TRUE;
- if(!Pegs[From].Count)
- return TRUE;
- if(Pegs[To].Count &&
- Pegs[From].Length[Pegs[From].Count-1] >
- Pegs[To].Length[Pegs[To].Count-1])
- return TRUE;
- return FALSE;
+ if (From >= NPEGS)
+ return TRUE;
+ if (From < 0)
+ return TRUE;
+ if (To >= NPEGS)
+ return TRUE;
+ if (To < 0)
+ return TRUE;
+ if (From == To)
+ return TRUE;
+ if (!Pegs[From].Count)
+ return TRUE;
+ if (Pegs[To].Count &&
+ Pegs[From].Length[Pegs[From].Count - 1] >
+ Pegs[To].Length[Pegs[To].Count - 1])
+ return TRUE;
+ return FALSE;
}
static void
InitTiles(int NTiles)
{
- int Size, SlotNo;
+ int Size, SlotNo;
- for(Size=NTiles*2+1, SlotNo=0; Size>=3; Size-=2)
- Pegs[0].Length[SlotNo++] = Size;
+ for (Size = NTiles * 2 + 1, SlotNo = 0; Size >= 3; Size -= 2)
+ Pegs[0].Length[SlotNo++] = Size;
- Pegs[0].Count = NTiles;
- Pegs[1].Count = 0;
- Pegs[2].Count = 0;
+ Pegs[0].Count = NTiles;
+ Pegs[1].Count = 0;
+ Pegs[2].Count = 0;
}
static void
DisplayTiles(void)
{
- int Line, peg, SlotNo;
- char TileBuf[BUFSIZ];
+ int Line, peg, SlotNo;
+ char TileBuf[BUFSIZ];
- erase();
- mvaddstr(1, 24, "T O W E R S O F H A N O I");
- mvaddstr(3, 34, "SJR 1990");
- mvprintw(19, 5, "Moves : %d", NMoves);
- attrset(A_REVERSE);
- mvaddstr(BASELINE, 8, " ");
+ erase();
+ mvaddstr(1, 24, "T O W E R S O F H A N O I");
+ mvaddstr(3, 34, "SJR 1990");
+ mvprintw(19, 5, "Moves : %d", NMoves);
+ attrset(A_REVERSE);
+ mvaddstr(BASELINE, 8,
+ " ");
- for(Line=TOPLINE; Line<BASELINE; Line++) {
- mvaddch(Line, LEFTPEG, ' ');
- mvaddch(Line, MIDPEG, ' ');
- mvaddch(Line, RIGHTPEG, ' ');
- }
- mvaddch(BASELINE, LEFTPEG, '1');
- mvaddch(BASELINE, MIDPEG, '2');
- mvaddch(BASELINE, RIGHTPEG, '3');
- attrset(A_NORMAL);
+ for (Line = TOPLINE; Line < BASELINE; Line++) {
+ mvaddch(Line, LEFTPEG, ' ');
+ mvaddch(Line, MIDPEG, ' ');
+ mvaddch(Line, RIGHTPEG, ' ');
+ }
+ mvaddch(BASELINE, LEFTPEG, '1');
+ mvaddch(BASELINE, MIDPEG, '2');
+ mvaddch(BASELINE, RIGHTPEG, '3');
+ attrset(A_NORMAL);
- /* Draw tiles */
- for(peg=0; peg<NPEGS; peg++) {
- for(SlotNo=0; SlotNo<Pegs[peg].Count; SlotNo++) {
- memset(TileBuf, ' ', Pegs[peg].Length[SlotNo]);
- TileBuf[Pegs[peg].Length[SlotNo]] = '\0';
- if (has_colors())
- attrset(COLOR_PAIR(LENTOIND(Pegs[peg].Length[SlotNo])));
- else
- attrset(A_REVERSE);
- mvaddstr(BASELINE-(SlotNo+1),
- (int)(PegPos[peg] - Pegs[peg].Length[SlotNo]/2),
- TileBuf);
- }
+ /* Draw tiles */
+ for (peg = 0; peg < NPEGS; peg++) {
+ for (SlotNo = 0; SlotNo < Pegs[peg].Count; SlotNo++) {
+ memset(TileBuf, ' ', Pegs[peg].Length[SlotNo]);
+ TileBuf[Pegs[peg].Length[SlotNo]] = '\0';
+ if (has_colors())
+ attrset(COLOR_PAIR(LENTOIND(Pegs[peg].Length[SlotNo])));
+ else
+ attrset(A_REVERSE);
+ mvaddstr(BASELINE - (SlotNo + 1),
+ (int) (PegPos[peg] - Pegs[peg].Length[SlotNo] / 2),
+ TileBuf);
}
- attrset(A_NORMAL);
- refresh();
+ }
+ attrset(A_NORMAL);
+ refresh();
}
static int
GetMove(int *From, int *To)
{
- mvaddstr(STATUSLINE, 0, "Next move ('q' to quit) from ");
- clrtoeol();
- refresh();
- if((*From = getch()) == 'q')
- return TRUE;
- *From -= ('0'+1);
- addstr(" to ");
- clrtoeol();
- refresh();
+ mvaddstr(STATUSLINE, 0, "Next move ('q' to quit) from ");
+ clrtoeol();
+ refresh();
+ if ((*From = getch()) == 'q')
+ return TRUE;
+ *From -= ('0' + 1);
+ addstr(" to ");
+ clrtoeol();
+ refresh();
- if((*To = getch()) == 'q')
- return TRUE;
- *To -= ('0'+1);
- refresh();
- napms(500);
+ if ((*To = getch()) == 'q')
+ return TRUE;
+ *To -= ('0' + 1);
+ refresh();
+ napms(500);
- move(STATUSLINE, 0);
- clrtoeol();
- refresh();
- return FALSE;
+ move(STATUSLINE, 0);
+ clrtoeol();
+ refresh();
+ return FALSE;
}
static void
MakeMove(int From, int To)
{
- Pegs[From].Count--;
- Pegs[To].Length[Pegs[To].Count] = Pegs[From].Length[Pegs[From].Count];
- Pegs[To].Count++;
- NMoves++;
- DisplayTiles();
+ Pegs[From].Count--;
+ Pegs[To].Length[Pegs[To].Count] = Pegs[From].Length[Pegs[From].Count];
+ Pegs[To].Count++;
+ NMoves++;
+ DisplayTiles();
}
static void
AutoMove(int From, int To, int Num)
{
- if(Num == 1) {
- MakeMove(From, To);
- napms(500);
- return;
- }
- AutoMove(From, OTHER(From, To), Num-1);
+ if (Num == 1) {
MakeMove(From, To);
napms(500);
- AutoMove(OTHER(From, To), To, Num-1);
+ return;
+ }
+ AutoMove(From, OTHER(From, To), Num - 1);
+ MakeMove(From, To);
+ napms(500);
+ AutoMove(OTHER(From, To), To, Num - 1);
}
static int
Solved(int NumTiles)
{
- int i;
+ int i;
- for(i = 1; i < NPEGS; i++)
- if (Pegs[i].Count == NumTiles)
- return TRUE;
- return FALSE;
+ for (i = 1; i < NPEGS; i++)
+ if (Pegs[i].Count == NumTiles)
+ return TRUE;
+ return FALSE;
}
static void
Usage()
{
- fprintf(stderr, "Usage: hanoi [<No Of Tiles>] [a]\n");
- fprintf(stderr, "The 'a' option causes the tower to be solved automatically\n");
+ fprintf(stderr, "Usage: hanoi [<No Of Tiles>] [a]\n");
+ fprintf(stderr,
+ "The 'a' option causes the tower to be solved automatically\n");
}