X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fhanoi.c;h=8f1792d9d88b0279c926e35d25186eb8c8c30d65;hp=16b826fc7b7d783946383d5d5d0298f6ebd0c57e;hb=HEAD;hpb=02f1dee48fe8af6ce054388fba739aa4f975004e diff --git a/test/hanoi.c b/test/hanoi.c index 16b826fc..6d92332f 100644 --- a/test/hanoi.c +++ b/test/hanoi.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 1998-2017,2019 Free Software Foundation, Inc. * + * Copyright 2019-2021,2022 Thomas E. Dickey * + * Copyright 1998-2014,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -41,7 +42,7 @@ * * Date: 05.Nov.90 * - * $Id: hanoi.c,v 1.40 2019/12/14 23:26:09 tom Exp $ + * $Id: hanoi.c,v 1.47 2022/12/04 00:40:11 tom Exp $ */ #include @@ -124,6 +125,15 @@ InitTiles(void) Pegs[2].Count = 0; } +static int +two2n(int n) +{ + int result = 1; + while (n-- > 0) + result *= 2; + return result; +} + static void DisplayTiles(void) { @@ -133,7 +143,7 @@ DisplayTiles(void) 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 of %.0f", NMoves, pow(2.0, (float) NTiles) - 1); + MvPrintw(19, 5, "Moves : %d of %d", NMoves, two2n(NTiles) - 1); (void) attrset(A_REVERSE); MvAddStr(BASELINE, 8, " "); @@ -231,12 +241,13 @@ Solved(int NumTiles) } static void -usage(void) +usage(int ok) { static const char *msg[] = { "Usage: hanoi [options] [[] [a]]" ,"" + ,USAGE_COMMON ,"Options:" #if HAVE_USE_DEFAULT_COLORS ," -d invoke use_default_colors" @@ -249,8 +260,11 @@ usage(void) for (n = 0; n < SIZEOF(msg); n++) fprintf(stderr, "%s\n", msg[n]); - ExitProgram(EXIT_FAILURE); + ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); } +/* *INDENT-OFF* */ +VERSION_COMMON() +/* *INDENT-ON* */ int main(int argc, char **argv) @@ -262,7 +276,7 @@ main(int argc, char **argv) #endif NTiles = DEFAULTTILES; - while ((ch = getopt(argc, argv, "dn:X")) != -1) { + while ((ch = getopt(argc, argv, OPTS_COMMON "dn:X")) != -1) { switch (ch) { #if HAVE_USE_DEFAULT_COLORS case 'd': @@ -275,17 +289,20 @@ main(int argc, char **argv) case 'X': AutoFlag = TRUE; break; + case OPTS_VERSION: + show_version(argv); + ExitProgram(EXIT_SUCCESS); default: - usage(); + usage(ch == OPTS_USAGE); /* NOTREACHED */ } } setlocale(LC_ALL, ""); - switch (ch = (argc - optind)) { + switch (argc - optind) { case 2: if (strcmp(argv[optind + 1], "a")) { - usage(); + usage(FALSE); } AutoFlag = TRUE; /* FALLTHRU */ @@ -295,12 +312,12 @@ main(int argc, char **argv) case 0: break; default: - usage(); + usage(FALSE); } if (NTiles > MAXTILES || NTiles < MINTILES) { fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES); - usage(); + usage(FALSE); } initscr();