libncurses: Import https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.5.tar.gz changes

Change-Id: I3433d30ca01359fd2e3623ede96b531f0b39cbfa
Signed-off-by: micky387 <mickaelsaibi@free.fr>
diff --git a/test/knight.c b/test/knight.c
index ae9d223..b15c4d2 100644
--- a/test/knight.c
+++ b/test/knight.c
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2012,2013 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,25 +34,26 @@
  * Eric S. Raymond <esr@snark.thyrsus.com> July 22 1995.  Mouse support
  * added September 20th 1995.
  *
- * $Id: knight.c,v 1.36 2013/02/16 19:53:08 tom Exp $
+ * $Id: knight.c,v 1.52 2022/12/04 00:40:11 tom Exp $
  */
 
 #include <test.priv.h>
 
 /* board size */
-#define BDEPTH	8
-#define BWIDTH	8
+#define YLIMIT		8
+#define XLIMIT		8
+#define MAXMOVES	(ylimit * xlimit)
 
 /* where to start the instructions */
-#define INSTRY	2
-#define INSTRX	35
+#define INSTRY		2
+#define INSTRX		35
 
 /* corner of board */
-#define BOARDY	2
-#define BOARDX	0
+#define BOARDY		2
+#define BOARDX		0
 
 /* notification line */
-#define NOTIFYY	21
+#define NOTIFYY		21
 
 /* virtual color values */
 #define TRAIL_COLOR	1
@@ -65,20 +67,30 @@
 #define CYINV(y)	(((y) - 2) / 2)
 
 typedef struct {
-    short x, y;
-} cell;
+    int x, y;
+} HISTORY;
+
+typedef int SQUARES[YLIMIT][XLIMIT];
 
 static WINDOW *boardwin;	/* the board window */
 static WINDOW *helpwin;		/* the help window */
 static WINDOW *msgwin;		/* the message window */
-static cell history[BDEPTH * BWIDTH + 1];	/* choice history */
+
+#if HAVE_USE_DEFAULT_COLORS
+static bool d_option;
+#endif
+
 static chtype minus = '-';	/* possible-move character */
 static chtype oldch;
 static chtype plus = '+';	/* cursor hot-spot character */
 static chtype trail = '#';	/* trail character */
-static int movecount;		/* count of moves so far */
-static int trialcount;		/* count of trials so far */
-static short board[BDEPTH][BWIDTH];	/* the squares */
+
+static int ylimit = YLIMIT;
+static int xlimit = XLIMIT;
+static int maxmoves = (YLIMIT * XLIMIT);
+
+static int count_tries;		/* count of trials so far */
+static int test_test;		/* FIXME */
 /* *INDENT-OFF* */
 static const struct {
     int y;
@@ -93,6 +105,7 @@
     {  1, -2 },
     {  2, -1 },
 };
+#define MAX_OFFSET	(unsigned)SIZEOF(offsets)
 /* *INDENT-ON* */
 
 static void
@@ -104,9 +117,12 @@
     initscr();
     cbreak();			/* immediate char return */
     noecho();			/* no immediate echo */
-    boardwin = newwin(BDEPTH * 2 + 1, BWIDTH * 4 + 1, BOARDY, BOARDX);
+
+    maxmoves = MAXMOVES;
+    boardwin = newwin(ylimit * 2 + 1, xlimit * 4 + 1, BOARDY, BOARDX);
     helpwin = newwin(0, 0, INSTRY, INSTRX);
     msgwin = newwin(1, INSTRX - 1, NOTIFYY, 0);
+
     scrollok(msgwin, TRUE);
     keypad(boardwin, TRUE);
 
@@ -115,7 +131,7 @@
 
 	start_color();
 #if HAVE_USE_DEFAULT_COLORS
-	if (use_default_colors() == OK)
+	if (d_option && (use_default_colors() == OK))
 	    bg = -1;
 #endif
 
@@ -178,7 +194,11 @@
     (void) waddstr(helpwin, "r -- redraw screen       \\|/      \\|/ \n");
     (void) waddstr(helpwin, "bksp -- undo move       h-+-l    4-+-6\n");
     (void) waddstr(helpwin, "a -- autojump            /|\\      /|\\ \n");
-    (void) waddstr(helpwin, "                        b j n    1 2 3\n");
+    if (ylimit <= 6) {
+	(void) waddstr(helpwin, "R -- solve (slow)       b j n    1 2 3\n");
+    } else {
+	(void) waddstr(helpwin, "                        b j n    1 2 3\n");
+    }
 
     (void) waddstr(helpwin, "\nYou can place your knight on the selected\n");
     (void) waddstr(helpwin, "square with spacebar, Enter, or the keypad\n");
@@ -202,30 +222,33 @@
     wrefresh(helpwin);
 }
 
-static bool
-chksqr(int r1, int c1)
+static inline bool
+isValidYX(int y, int x)
 {
-    if ((r1 < 0) || (r1 > BDEPTH - 1))
-	return (FALSE);
-    if ((c1 < 0) || (c1 > BWIDTH - 1))
-	return (FALSE);
-    return ((!board[r1][c1]) ? TRUE : FALSE);
+    return (y >= 0 && y < ylimit && x >= 0 && x < xlimit) ? TRUE : FALSE;
+}
+
+static inline bool
+isUnusedYX(SQUARES squares, int y, int x)
+{
+    return (isValidYX(y, x) && (!squares[y][x]) ? TRUE : FALSE);
 }
 
 static bool
-chkmoves(int rw, int col)
-/* check to see if valid moves are available */
+boardIsFilled(SQUARES squares, int y, int x)
 {
     unsigned n;
 
-    for (n = 0; n < SIZEOF(offsets); n++)
-	if (chksqr(rw + offsets[n].y, col + offsets[n].x))
-	    return (TRUE);
-    return (FALSE);
+    for (n = 0; n < MAX_OFFSET; n++) {
+	if (isUnusedYX(squares, y + offsets[n].y, x + offsets[n].x)) {
+	    return FALSE;
+	}
+    }
+    return TRUE;
 }
 
 static void
-dosquares(void)
+drawBoard(void)
 {
     int i, j;
 
@@ -233,7 +256,7 @@
 
     move(BOARDY, BOARDX);
     waddch(boardwin, ACS_ULCORNER);
-    for (j = 0; j < 7; j++) {
+    for (j = 0; j < (ylimit - 1); j++) {
 	waddch(boardwin, ACS_HLINE);
 	waddch(boardwin, ACS_HLINE);
 	waddch(boardwin, ACS_HLINE);
@@ -244,10 +267,10 @@
     waddch(boardwin, ACS_HLINE);
     waddch(boardwin, ACS_URCORNER);
 
-    for (i = 1; i < BDEPTH; i++) {
+    for (i = 1; i < ylimit; i++) {
 	move(BOARDY + i * 2 - 1, BOARDX);
 	waddch(boardwin, ACS_VLINE);
-	for (j = 0; j < BWIDTH; j++) {
+	for (j = 0; j < xlimit; j++) {
 	    waddch(boardwin, ' ');
 	    waddch(boardwin, ' ');
 	    waddch(boardwin, ' ');
@@ -255,7 +278,7 @@
 	}
 	move(BOARDY + i * 2, BOARDX);
 	waddch(boardwin, ACS_LTEE);
-	for (j = 0; j < BWIDTH - 1; j++) {
+	for (j = 0; j < xlimit - 1; j++) {
 	    waddch(boardwin, ACS_HLINE);
 	    waddch(boardwin, ACS_HLINE);
 	    waddch(boardwin, ACS_HLINE);
@@ -269,7 +292,7 @@
 
     move(BOARDY + i * 2 - 1, BOARDX);
     waddch(boardwin, ACS_VLINE);
-    for (j = 0; j < BWIDTH; j++) {
+    for (j = 0; j < xlimit; j++) {
 	waddch(boardwin, ' ');
 	waddch(boardwin, ' ');
 	waddch(boardwin, ' ');
@@ -278,7 +301,7 @@
 
     move(BOARDY + i * 2, BOARDX);
     waddch(boardwin, ACS_LLCORNER);
-    for (j = 0; j < BWIDTH - 1; j++) {
+    for (j = 0; j < xlimit - 1; j++) {
 	waddch(boardwin, ACS_HLINE);
 	waddch(boardwin, ACS_HLINE);
 	waddch(boardwin, ACS_HLINE);
@@ -291,37 +314,36 @@
 }
 
 static void
-mark_possibles(int prow, int pcol, chtype mark)
+mark_possibles(SQUARES squares, int y, int x, chtype mark)
 {
     unsigned n;
 
-    for (n = 0; n < SIZEOF(offsets); n++) {
-	if (chksqr(prow + offsets[n].y, pcol + offsets[n].x)) {
-	    cellmove(prow + offsets[n].y, pcol + offsets[n].x);
+    for (n = 0; n < MAX_OFFSET; n++) {
+	if (isUnusedYX(squares, y + offsets[n].y, x + offsets[n].x)) {
+	    cellmove(y + offsets[n].y, x + offsets[n].x);
 	    waddch(boardwin, mark);
 	}
     }
 }
 
 static bool
-find_next_move(int *y, int *x)
+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 (movecount > 1) {
-	oldy = history[movecount - 1].y;
-	oldx = history[movecount - 1].x;
-	for (j = 0; j < SIZEOF(offsets) * 2; j++) {
-	    k = j % SIZEOF(offsets);
-	    newy = oldy + offsets[k].y;
-	    newx = oldx + offsets[k].x;
-	    if (chksqr(newy, newx)) {
+    if (doneSize > 1) {
+	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++) {
+	    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;
 		if (newy == *y
@@ -345,16 +367,16 @@
 }
 
 static void
-count_next_moves(int y, int x)
+count_next_moves(SQUARES squares, int count_moves, int y, int x)
 {
     int count = 0;
     unsigned j;
 
-    wprintw(msgwin, "\nMove %d", movecount);
-    for (j = 0; j < SIZEOF(offsets); j++) {
+    wprintw(msgwin, "\nMove %d", count_moves);
+    for (j = 0; j < MAX_OFFSET; j++) {
 	int newy = y + offsets[j].y;
 	int newx = x + offsets[j].x;
-	if (chksqr(newy, newx)) {
+	if (isUnusedYX(squares, newy, newx)) {
 	    ++count;
 	}
     }
@@ -383,37 +405,38 @@
 }
 
 static void
-drawmove(chtype tchar, int oldy, int oldx, int row, int column)
+drawMove(SQUARES squares, int count_moves, chtype tchar, int oldy, int oldx, int
+	 row, int column)
 /* place the stars, update board & currents */
 {
-    if (movecount <= 1) {
+    if (count_moves <= 1) {
 	int i, j;
 
-	for (i = 0; i < BDEPTH; i++) {
-	    for (j = 0; j < BWIDTH; j++) {
-		if (movecount == 0) {
+	for (i = 0; i < ylimit; i++) {
+	    for (j = 0; j < xlimit; j++) {
+		if (count_moves == 0) {
 		    unmarkcell(i, j);
 		} else {
 		    cellmove(i, j);
 		    if (winch(boardwin) == minus)
-			waddch(boardwin, movecount ? ' ' : minus);
+			waddch(boardwin, ' ');
 		}
 	    }
 	}
     } else {
 	markcell(tchar, oldy, oldx);
-	mark_possibles(oldy, oldx, ' ');
+	mark_possibles(squares, oldy, oldx, ' ');
     }
 
     if (row >= 0 && column >= 0) {
 	markcell(trail, row, column);
-	mark_possibles(row, column, minus);
-	board[row][column] = TRUE;
+	mark_possibles(squares, row, column, minus);
+	squares[row][column] = TRUE;
     }
 
-    wprintw(msgwin, "\nMove %d", movecount);
-    if (trialcount != movecount)
-	wprintw(msgwin, " (%d tries)", trialcount);
+    wprintw(msgwin, "\nMove %d", count_moves);
+    if (count_tries != count_moves)
+	wprintw(msgwin, " (%d tries)", count_tries);
     wclrtoeol(msgwin);
 }
 
@@ -427,17 +450,16 @@
 }
 
 static bool
-evalmove(int row, int column)
-/* evaluate move */
+evaluate_move(SQUARES squares, HISTORY * doneData, int doneSize, int row, int column)
 {
-    if (movecount == 1)
+    if (doneSize <= 1)
 	return (TRUE);
-    else if (board[row][column] == TRUE) {
+    else if (squares[row][column] == TRUE) {
 	waddstr(msgwin, "\nYou've already been there.");
 	return (FALSE);
     } else {
-	int rdif = iabs(row - history[movecount - 1].y);
-	int cdif = iabs(column - history[movecount - 1].x);
+	int rdif = iabs(row - doneData[doneSize - 1].y);
+	int cdif = iabs(column - doneData[doneSize - 1].x);
 
 	if (!((rdif == 1) && (cdif == 2)) && !((rdif == 2) && (cdif == 1))) {
 	    waddstr(msgwin, "\nThat's not a legal knight's move.");
@@ -449,15 +471,18 @@
 }
 
 static int
-completed(void)
+completed(SQUARES squares)
 {
     int i, j, count = 0;
 
-    for (i = 0; i < BDEPTH; i++)
-	for (j = 0; j < BWIDTH; j++)
-	    if (board[i][j] != 0)
+    for (i = 0; i < ylimit; i++) {
+	for (j = 0; j < xlimit; j++) {
+	    if (squares[i][j] != 0) {
 		count += 1;
-    return (count == (BWIDTH * BDEPTH) ? -1 : count);
+	    }
+	}
+    }
+    return ((count == maxmoves) ? -1 : count);
 }
 
 static void
@@ -467,24 +492,126 @@
     beep();
 }
 
+/* Recursively try all possible moves, starting from (y,x) */
+static int
+recurBack(SQUARES squares, int y, int x, int total)
+{
+    int longest = total;
+    int best_x = x;
+    int best_y = y;
+    int result;
+
+    if (total < maxmoves) {
+	unsigned k;
+
+	for (k = 0; k < MAX_OFFSET; k++) {
+	    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;
+		result = recurBack(squares, try_y, try_x, total + 1);
+		if (result > longest) {
+		    longest = result;
+		    best_x = try_x;
+		    best_y = try_y;
+		}
+		if (result >= maxmoves)
+		    break;
+		squares[try_y][try_x] = 0;	/* allow retry... */
+	    }
+	}
+    }
+
+    result = total;
+    if (longest > total) {
+	result = longest;
+	squares[best_y][best_x] = total + 1;
+	(void) recurBack(squares, best_y, best_x, total + 1);
+	if (result < maxmoves)
+	    squares[best_y][best_x] = 0;
+    }
+
+    return result;
+}
+
+/*
+ * Solve the Knight Tour problem using backtracking, returning the length of
+ * the resulting solution.  If this is invoked from a point where the remaining
+ * choices cannot complete the tour, the result will fall short.
+ */
+static int
+useBacktracking(SQUARES result, HISTORY * doneData, int doneSize)
+{
+    int y = 0, x = 0, n;
+    SQUARES squares;
+    int total;
+    int actual = doneSize - 1;
+
+    memset(squares, 0, sizeof(squares));
+    for (n = 1; n <= actual; ++n) {
+	y = doneData[n].y;
+	x = doneData[n].x;
+	squares[y][x] = n;
+    }
+
+    total = recurBack(squares, y, x, actual);
+    if (total > actual) {
+	for (y = 0; y < ylimit; ++y) {
+	    for (x = 0; x < xlimit; ++x) {
+		result[y][x] = squares[y][x];
+		if ((n = squares[y][x]) != 0) {
+		    doneData[n].y = y;
+		    doneData[n].x = x;
+		}
+	    }
+	}
+    }
+    return total;
+}
+
+static int
+reviewHistory(HISTORY * history, int count_moves, int review, int *ny, int *nx)
+{
+    if (review < 0) {
+	beep();
+	review = 0;
+    } else if (review > count_moves - 2) {
+	beep();
+	review = count_moves - 2;
+    } else {
+	*ny = history[count_moves - review - 1].y;
+	*nx = history[count_moves - review - 1].x;
+	wprintw(msgwin, "\nReview %d:%d.", count_moves - review - 1,
+		count_moves - 1);
+	wrefresh(msgwin);
+    }
+    return review;
+}
+
 static void
 play(void)
 /* play the game */
 {
     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;
     int rw = 0, col = 0;	/* current row and column */
 
     do {
+	SQUARES squares;
+	HISTORY history[(YLIMIT * XLIMIT) + 1];
+	int count_moves = 0;	/* count of moves so far */
+
 	/* clear screen and draw board */
 	werase(boardwin);
 	werase(helpwin);
 	werase(msgwin);
-	dosquares();
+	drawBoard();
 	help1();
 	wnoutrefresh(stdscr);
 	wnoutrefresh(helpwin);
@@ -492,19 +619,18 @@
 	wnoutrefresh(boardwin);
 	doupdate();
 
-	movecount = 0;
-	for (i = 0; i < BDEPTH; i++) {
-	    for (j = 0; j < BWIDTH; j++) {
-		board[i][j] = FALSE;
+	for (i = 0; i < ylimit; i++) {
+	    for (j = 0; j < xlimit; j++) {
 		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;
 	lastrow = lastcol = -2;
-	movecount = 1;
-	trialcount = 1;
+	count_moves = 1;
+	count_tries = 1;
 	keyhelp = FALSE;
 	show_help(&keyhelp);
 
@@ -512,7 +638,7 @@
 	    if (rw != lastrow || col != lastcol) {
 		if (lastrow >= 0 && lastcol >= 0) {
 		    cellmove(lastrow, lastcol);
-		    if (board[lastrow][lastcol])
+		    if (squares[lastrow][lastcol])
 			waddch(boardwin, trail);
 		    else
 			waddch(boardwin, oldch);
@@ -534,7 +660,7 @@
 	    case 'k':
 	    case '8':
 	    case KEY_UP:
-		ny = rw + BDEPTH - 1;
+		ny = rw + ylimit - 1;
 		nx = col;
 		break;
 	    case 'j':
@@ -547,7 +673,7 @@
 	    case '4':
 	    case KEY_LEFT:
 		ny = rw;
-		nx = col + BWIDTH - 1;
+		nx = col + xlimit - 1;
 		break;
 	    case 'l':
 	    case '6':
@@ -558,19 +684,19 @@
 	    case 'y':
 	    case '7':
 	    case KEY_A1:
-		ny = rw + BDEPTH - 1;
-		nx = col + BWIDTH - 1;
+		ny = rw + ylimit - 1;
+		nx = col + xlimit - 1;
 		break;
 	    case 'b':
 	    case '1':
 	    case KEY_C1:
 		ny = rw + 1;
-		nx = col + BWIDTH - 1;
+		nx = col + xlimit - 1;
 		break;
 	    case 'u':
 	    case '9':
 	    case KEY_A3:
-		ny = rw + BDEPTH - 1;
+		ny = rw + ylimit - 1;
 		nx = col + 1;
 		break;
 	    case 'n':
@@ -587,8 +713,8 @@
 		    MEVENT myevent;
 
 		    getmouse(&myevent);
-		    if (myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
-			&& myevent.x >= CX(0) && myevent.x <= CX(BWIDTH)) {
+		    if (myevent.y >= CY(0) && myevent.y <= CY(ylimit)
+			&& myevent.x >= CX(0) && myevent.x <= CX(xlimit)) {
 			nx = CXINV(myevent.x);
 			ny = CYINV(myevent.y);
 			ungetch('\n');
@@ -605,8 +731,8 @@
 		    request_mouse_pos();
 		    test_y = MOUSE_Y_POS + 0;
 		    test_x = MOUSE_X_POS + 1;
-		    if (test_y >= CY(0) && test_y <= CY(BDEPTH)
-			&& test_x >= CX(0) && test_x <= CX(BWIDTH)) {
+		    if (test_y >= CY(0) && test_y <= CY(ylimit)
+			&& test_x >= CX(0) && test_x <= CX(xlimit)) {
 			ny = CYINV(test_y);
 			nx = CXINV(test_x);
 			wmove(helpwin, 0, 0);
@@ -622,18 +748,20 @@
 	    case '\n':
 	    case ' ':
 		review = 0;
-		if (evalmove(rw, col)) {
-		    drawmove(trail,
-			     history[movecount - 1].y,
-			     history[movecount - 1].x,
+		if (evaluate_move(squares, history, count_moves, rw, col)) {
+		    drawMove(squares,
+			     count_moves,
+			     trail,
+			     history[count_moves - 1].y,
+			     history[count_moves - 1].x,
 			     rw, col);
-		    history[movecount].y = (short) rw;
-		    history[movecount].x = (short) col;
-		    movecount++;
-		    trialcount++;
+		    history[count_moves].y = (short) rw;
+		    history[count_moves].x = (short) col;
+		    count_moves++;
+		    count_tries++;
 
-		    if (!chkmoves(rw, col)) {
-			if (completed() < 0) {
+		    if (boardIsFilled(squares, rw, col)) {
+			if (completed(squares) < 0) {
 			    waddstr(msgwin, "\nYou won.");
 			} else {
 			    waddstr(msgwin,
@@ -649,40 +777,40 @@
 	    case KEY_BACKSPACE:
 	    case '\b':
 		review = 0;
-		if (movecount <= 0) {
+		if (count_moves <= 0) {
 		    no_previous_move();
-		} else if (movecount <= 1) {
-		    ny = history[movecount].y;
-		    nx = history[movecount].x;
+		} else if (count_moves <= 1) {
+		    ny = history[count_moves].y;
+		    nx = history[count_moves].x;
 		    if (nx < 0 || ny < 0) {
 			ny = (lastrow >= 0) ? lastrow : 0;
 			nx = (lastcol >= 0) ? lastcol : 0;
 		    }
-		    movecount = 0;
-		    board[ny][nx] = FALSE;
+		    count_moves = 0;
+		    squares[ny][nx] = FALSE;
 		    oldch = minus;
-		    drawmove(' ', ny, nx, -1, -1);
-		    movecount = 1;
-		    trialcount = 1;
+		    drawMove(squares, count_moves, ' ', ny, nx, -1, -1);
+		    count_moves = 1;
+		    count_tries = 1;
 		    no_previous_move();
 		} else {
-		    int oldy = history[movecount - 1].y;
-		    int oldx = history[movecount - 1].x;
+		    int oldy = history[count_moves - 1].y;
+		    int oldx = history[count_moves - 1].x;
 
-		    if (!board[rw][col]) {
+		    if (!squares[rw][col]) {
 			cellmove(rw, col);
 			waddch(boardwin, ' ');
 		    }
 
-		    board[oldy][oldx] = FALSE;
-		    --movecount;
-		    ny = history[movecount - 1].y;
-		    nx = history[movecount - 1].x;
+		    squares[oldy][oldx] = FALSE;
+		    --count_moves;
+		    ny = history[count_moves - 1].y;
+		    nx = history[count_moves - 1].x;
 		    if (nx < 0 || ny < 0) {
 			ny = oldy;
 			nx = oldx;
 		    }
-		    drawmove(' ', oldy, oldx, ny, nx);
+		    drawMove(squares, count_moves, ' ', oldy, oldx, ny, nx);
 
 		    /* avoid problems if we just changed the current cell */
 		    cellmove(lastrow, lastcol);
@@ -693,32 +821,44 @@
 	    case 'a':
 		nx = col;
 		ny = rw;
-		if (find_next_move(&ny, &nx))
-		    count_next_moves(ny, nx);
+		if (find_next_move(squares, history, count_moves, &ny, &nx))
+		    count_next_moves(squares, count_moves, ny, nx);
 		else
 		    beep();
 		break;
 
 	    case 'F':
-		if (review > 0) {
-		    review--;
-		    ny = history[movecount - review - 1].y;
-		    nx = history[movecount - review - 1].x;
-		} else {
-		    beep();
-		}
+		review = reviewHistory(history, count_moves, review - 1,
+				       &ny, &nx);
 		break;
 
 	    case 'B':
-		if (review < movecount - 2) {
-		    review++;
-		    ny = history[movecount - review - 1].y;
-		    nx = history[movecount - review - 1].x;
-		} else {
-		    beep();
-		}
+		review = reviewHistory(history, count_moves, review + 1,
+				       &ny, &nx);
 		break;
 
+	    case 'R':
+		if (ylimit <= 6) {
+		    wprintw(msgwin, "\nworking...");
+		    wrefresh(msgwin);
+		    test_test = 0;
+		    test_size = useBacktracking(squares, history, count_moves);
+		    wprintw(msgwin, "\nOk %d:%d (%d tests)",
+			    test_size, maxmoves, test_test);
+		    review = 0;
+		    while (count_moves <= test_size) {
+			markcell(trail,
+				 ny = history[count_moves].y,
+				 nx = history[count_moves].x);
+			count_moves++;
+		    }
+		} else {
+		    wprintw(msgwin, "\nBoard is too large.");
+		}
+		wrefresh(msgwin);
+		break;
+
+#if HAVE_CURSCR
 	    case KEY_REDO:
 	    case '\f':
 	    case 'r':
@@ -729,12 +869,13 @@
 		wnoutrefresh(helpwin);
 		doupdate();
 		break;
+#endif
 
 	    case 'q':
 	    case 'x':
 		goto dropout;
 
-	    case '?':
+	    case HELP_KEY_1:
 		show_help(&keyhelp);
 		break;
 
@@ -743,12 +884,12 @@
 		break;
 	    }
 
-	    col = nx % BWIDTH;
-	    rw = ny % BDEPTH;
+	    col = nx % xlimit;
+	    rw = ny % ylimit;
 	}
 
       dropout:
-	if ((count = completed()) < 0)
+	if ((count = completed(squares)) < 0)
 	    wprintw(msgwin, "\nYou won.  Care to try again? ");
 	else
 	    wprintw(msgwin, "\n%d squares filled.  Try again? ", count);
@@ -757,9 +898,62 @@
 	(tolower(wgetch(msgwin)) == 'y');
 }
 
-int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+static void
+usage(int ok)
 {
+    static const char *msg[] =
+    {
+	"Usage: knight [options]"
+	,""
+	,USAGE_COMMON
+	,"Options:"
+#if HAVE_USE_DEFAULT_COLORS
+	," -d       invoke use_default_colors"
+#endif
+	," -n NUM   set board-size to NUM*NUM (default 8x8)"
+    };
+    size_t n;
+
+    for (n = 0; n < SIZEOF(msg); n++)
+	fprintf(stderr, "%s\n", msg[n]);
+
+    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, OPTS_COMMON "dn:")) != -1) {
+	switch (ch) {
+#if HAVE_USE_DEFAULT_COLORS
+	case 'd':
+	    d_option = TRUE;
+	    break;
+#endif
+	case 'n':
+	    ch = atoi(optarg);
+	    if (ch < 3 || ch > 8) {
+		fprintf(stderr, "board size %d is outside [3..8]\n", ch);
+		usage(FALSE);
+	    }
+	    xlimit = ylimit = ch;
+	    break;
+	case OPTS_VERSION:
+	    show_version(argv);
+	    ExitProgram(EXIT_SUCCESS);
+	default:
+	    usage(ch == OPTS_USAGE);
+	    /* NOTREACHED */
+	}
+    }
+    if (optind < argc)
+	usage(FALSE);
+
     init_program();
 
     play();
@@ -767,5 +961,3 @@
     endwin();
     ExitProgram(EXIT_SUCCESS);
 }
-
-/* knight.c ends here */