X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fknight.c;h=b15c4d23763e07e5bf201cf84483af30edf120a3;hp=b95996c608178b45eda852376796916560abd2c6;hb=HEAD;hpb=5d8dbcdd9423bf9821db414fd9ec792ccf1f1027 diff --git a/test/knight.c b/test/knight.c index b95996c6..b15c4d23 100644 --- a/test/knight.c +++ b/test/knight.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 1998-2013,2017 Free Software Foundation, Inc. * + * Copyright 2018-2021,2022 Thomas E. Dickey * + * Copyright 1998-2013,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 * @@ -33,7 +34,7 @@ * Eric S. Raymond July 22 1995. Mouse support * added September 20th 1995. * - * $Id: knight.c,v 1.43 2017/09/10 00:13:02 tom Exp $ + * $Id: knight.c,v 1.52 2022/12/04 00:40:11 tom Exp $ */ #include @@ -75,7 +76,9 @@ static WINDOW *boardwin; /* the board window */ static WINDOW *helpwin; /* the help window */ static WINDOW *msgwin; /* the message window */ +#if HAVE_USE_DEFAULT_COLORS static bool d_option; +#endif static chtype minus = '-'; /* possible-move character */ static chtype oldch; @@ -326,21 +329,20 @@ mark_possibles(SQUARES squares, int y, int x, chtype mark) static bool find_next_move(SQUARES squares, HISTORY * doneData, int doneSize, int *y, int *x) { - unsigned j, k; - int found = -1; - int first = -1; - int next = -1; - int oldy, oldx; - int newy, newx; bool result = FALSE; if (doneSize > 1) { - oldy = doneData[doneSize - 1].y; - oldx = doneData[doneSize - 1].x; + unsigned j; + int oldy = doneData[doneSize - 1].y; + int oldx = doneData[doneSize - 1].x; + int found = -1; + int first = -1; + int next = -1; + for (j = 0; j < MAX_OFFSET * 2; j++) { - k = j % MAX_OFFSET; - newy = oldy + offsets[k].y; - newx = oldx + offsets[k].x; + unsigned k = j % MAX_OFFSET; + int newy = oldy + offsets[k].y; + int newx = oldx + offsets[k].x; if (isUnusedYX(squares, newy, newx)) { if (first < 0) first = (int) k; @@ -417,7 +419,7 @@ drawMove(SQUARES squares, int count_moves, chtype tchar, int oldy, int oldx, int } else { cellmove(i, j); if (winch(boardwin) == minus) - waddch(boardwin, count_moves ? ' ' : minus); + waddch(boardwin, ' '); } } } @@ -500,12 +502,11 @@ recurBack(SQUARES squares, int y, int x, int total) int result; if (total < maxmoves) { - int try_x, try_y; unsigned k; for (k = 0; k < MAX_OFFSET; k++) { - try_x = x + offsets[k].x; - try_y = y + offsets[k].y; + int try_x = x + offsets[k].x; + int try_y = y + offsets[k].y; if (isUnusedYX(squares, try_y, try_x)) { ++test_test; squares[try_y][try_x] = total + 1; @@ -594,8 +595,8 @@ play(void) { bool keyhelp; /* TRUE if keystroke help is up */ int i, j, count; - int lastcol = 0; /* last location visited */ - int lastrow = 0; + int lastcol; /* last location visited */ + int lastrow; int ny = 0, nx = 0; int review = 0; /* review history */ int test_size; @@ -620,10 +621,10 @@ play(void) for (i = 0; i < ylimit; i++) { for (j = 0; j < xlimit; j++) { - squares[i][j] = FALSE; unmarkcell(i, j); } } + memset(squares, 0, sizeof(squares)); memset(history, 0, sizeof(history)); history[0].y = history[0].x = -1; history[1].y = history[1].x = -1; @@ -898,12 +899,13 @@ play(void) } static void -usage(void) +usage(int ok) { static const char *msg[] = { "Usage: knight [options]" ,"" + ,USAGE_COMMON ,"Options:" #if HAVE_USE_DEFAULT_COLORS ," -d invoke use_default_colors" @@ -915,15 +917,18 @@ 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[]) { int ch; - while ((ch = getopt(argc, argv, "dn:")) != -1) { + while ((ch = getopt(argc, argv, OPTS_COMMON "dn:")) != -1) { switch (ch) { #if HAVE_USE_DEFAULT_COLORS case 'd': @@ -934,17 +939,20 @@ main(int argc, char *argv[]) ch = atoi(optarg); if (ch < 3 || ch > 8) { fprintf(stderr, "board size %d is outside [3..8]\n", ch); - usage(); + usage(FALSE); } xlimit = ylimit = ch; break; + case OPTS_VERSION: + show_version(argv); + ExitProgram(EXIT_SUCCESS); default: - usage(); + usage(ch == OPTS_USAGE); /* NOTREACHED */ } } if (optind < argc) - usage(); + usage(FALSE); init_program();