+ static const char *msg[] =
+ {
+ "Usage: hanoi [options] [[<No Of Tiles>] [a]]"
+ ,""
+ ,"Options:"
+#if HAVE_USE_DEFAULT_COLORS
+ ," -d invoke use_default_colors"
+#endif
+ ," -n NUM set number of tiles (positional param is deprecated)"
+ ," -X solve automatically (positional \"a\" is deprecated)"
+ };
+ size_t n;
+
+ for (n = 0; n < SIZEOF(msg); n++)
+ fprintf(stderr, "%s\n", msg[n]);
+
+ ExitProgram(EXIT_FAILURE);
+}
+
+int
+main(int argc, char **argv)
+{
+ int ch, FromCol, ToCol;
+
+#if HAVE_USE_DEFAULT_COLORS
+ bool d_option = FALSE;
+#endif
+
+ NTiles = DEFAULTTILES;
+ while ((ch = getopt(argc, argv, "dn:X")) != -1) {
+ switch (ch) {
+#if HAVE_USE_DEFAULT_COLORS
+ case 'd':
+ d_option = TRUE;
+ break;
+#endif
+ case 'n':
+ NTiles = atoi(optarg);
+ break;
+ case 'X':
+ AutoFlag = TRUE;
+ break;
+ default:
+ usage();
+ /* NOTREACHED */
+ }
+ }
+ setlocale(LC_ALL, "");
+
+ switch (argc - optind) {
+ case 2:
+ if (strcmp(argv[optind + 1], "a")) {
+ usage();
+ }
+ AutoFlag = TRUE;
+ /* FALLTHRU */
+ case 1:
+ NTiles = atoi(argv[optind]);
+ /* FALLTHRU */
+ case 0:
+ break;
+ default:
+ usage();
+ }
+
+ if (NTiles > MAXTILES || NTiles < MINTILES) {
+ fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
+ usage();
+ }
+
+ initscr();
+ if (has_colors()) {
+ int i;
+ short bg = COLOR_BLACK;
+ start_color();
+#if HAVE_USE_DEFAULT_COLORS
+ if (d_option && (use_default_colors() == OK))
+ bg = -1;
+#endif
+ for (i = 0; i < 9; i++)
+ init_pair((short) (i + 1), bg, TileColour[i]);
+ }
+ cbreak();
+ if (LINES < 24) {
+ endwin();
+ fprintf(stderr, "Min screen length 24 lines\n");
+ ExitProgram(EXIT_FAILURE);
+ }
+ if (AutoFlag) {
+ curs_set(0);
+ leaveok(stdscr, TRUE); /* Attempt to remove cursor */
+ }
+ InitTiles();
+ 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;
+ }
+ }
+ }
+ stop_curses();
+ ExitProgram(EXIT_SUCCESS);