2 * Knight's Tour - a brain game
4 * The original of this game was anonymous. It had an unbelievably bogus
5 * interface, you actually had to enter square coordinates! Redesign by
6 * Eric S. Raymond <esr@snark.thyrsus.com> July 22 1995. Mouse support
7 * added September 20th 1995.
9 * $Id: knight.c,v 1.20 2000/10/29 00:51:51 tom Exp $
12 #include <test.priv.h>
22 /* where to start the instructions */
30 /* notification line */
33 /* virtual color values */
38 #define CX(x) (2 + 4 * (x))
39 #define CY(y) (1 + 2 * (y))
40 #define cellmove(y, x) wmove(boardwin, CY(y), CX(x))
41 #define CXINV(x) (((x) - 1) / 4)
42 #define CYINV(y) (((y) - 2) / 2)
48 static WINDOW *boardwin; /* the board window */
49 static WINDOW *helpwin; /* the help window */
50 static WINDOW *msgwin; /* the message window */
51 static cell history[BDEPTH * BWIDTH + 1]; /* choice history */
52 static chtype minus = '-'; /* possible-move character */
54 static chtype plus = '+'; /* cursor hot-spot character */
55 static chtype trail = '#'; /* trail character */
56 static int movecount; /* count of moves so far */
57 static int trialcount; /* count of trials so far */
58 static short board[BDEPTH][BWIDTH]; /* the squares */
78 srand((unsigned) getpid());
80 cbreak(); /* immediate char return */
81 noecho(); /* no immediate echo */
82 boardwin = newwin(BDEPTH * 2 + 1, BWIDTH * 4 + 1, BOARDY, BOARDX);
83 helpwin = newwin(0, 0, INSTRY, INSTRX);
84 msgwin = newwin(1, INSTRX - 1, NOTIFYY, 0);
85 scrollok(msgwin, TRUE);
86 keypad(boardwin, TRUE);
92 #if HAVE_USE_DEFAULT_COLORS
93 if (use_default_colors() == OK)
97 (void) init_pair(TRAIL_COLOR, COLOR_CYAN, bg);
98 (void) init_pair(PLUS_COLOR, COLOR_RED, bg);
99 (void) init_pair(MINUS_COLOR, COLOR_GREEN, bg);
101 trail |= COLOR_PAIR(TRAIL_COLOR);
102 plus |= COLOR_PAIR(PLUS_COLOR);
103 minus |= COLOR_PAIR(MINUS_COLOR);
105 #ifdef NCURSES_MOUSE_VERSION
106 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
107 #endif /* NCURSES_MOUSE_VERSION */
114 /* game explanation -- initial help screen */
116 (void) waddstr(helpwin, "Knight's move is a solitaire puzzle. Your\n");
117 (void) waddstr(helpwin, "objective is to visit each square of the \n");
118 (void) waddstr(helpwin, "chessboard exactly once by making knight's\n");
119 (void) waddstr(helpwin, "moves (one square right or left followed \n");
120 (void) waddstr(helpwin, "by two squares up or down, or two squares \n");
121 (void) waddstr(helpwin, "right or left followed by one square up or\n");
122 (void) waddstr(helpwin, "down). You may start anywhere.\n\n");
124 (void) waddstr(helpwin, "Use arrow keys to move the cursor around.\n");
125 (void) waddstr(helpwin, "When you want to move your knight to the \n");
126 (void) waddstr(helpwin, "cursor location, press <space> or Enter.\n");
127 (void) waddstr(helpwin, "Illegal moves will be rejected with an \n");
128 (void) waddstr(helpwin, "audible beep.\n\n");
129 (void) waddstr(helpwin, "The program will detect if you solve the\n");
130 (void) waddstr(helpwin, "puzzle; also inform you when you run out\n");
131 (void) waddstr(helpwin, "of legal moves.\n\n");
133 (void) mvwaddstr(helpwin, NOTIFYY - INSTRY, 0,
134 "Press `?' to go to keystroke help.");
139 /* keystroke help screen */
141 (void) waddstr(helpwin, "Possible moves are shown with `-'.\n\n");
143 (void) waddstr(helpwin, "You can move around with the arrow keys or\n");
144 (void) waddstr(helpwin, "with the rogue/hack movement keys. Other\n");
145 (void) waddstr(helpwin, "commands allow you to undo moves or redraw.\n");
146 (void) waddstr(helpwin, "Your mouse may work; try left-button to\n");
147 (void) waddstr(helpwin, "move to the square under the pointer.\n\n");
149 (void) waddstr(helpwin, "x,q -- exit y k u 7 8 9\n");
150 (void) waddstr(helpwin, "r -- redraw screen \\|/ \\|/ \n");
151 (void) waddstr(helpwin, "bksp -- undo move h-+-l 4-+-6\n");
152 (void) waddstr(helpwin, "a -- autojump /|\\ /|\\ \n");
153 (void) waddstr(helpwin, " b j n 1 2 3\n");
155 (void) waddstr(helpwin, "\nYou can place your knight on the selected\n");
156 (void) waddstr(helpwin, "square with spacebar, Enter, or the keypad\n");
157 (void) waddstr(helpwin, "center key. Use F/B to review the path.\n");
159 (void) mvwaddstr(helpwin, NOTIFYY - INSTRY, 0,
160 "Press `?' to go to game explanation");
164 show_help(bool * keyhelp)
178 chksqr(int r1, int c1)
180 if ((r1 < 0) || (r1 > BDEPTH - 1))
182 if ((c1 < 0) || (c1 > BWIDTH - 1))
184 return ((!board[r1][c1]) ? TRUE : FALSE);
188 chkmoves(int rw, int col)
189 /* check to see if valid moves are available */
193 for (n = 0; n < SIZEOF(offsets); n++)
194 if (chksqr(rw + offsets[n].y, col + offsets[n].x))
204 mvaddstr(0, 20, "KNIGHT'S MOVE -- a logical solitaire");
206 move(BOARDY, BOARDX);
207 waddch(boardwin, ACS_ULCORNER);
208 for (j = 0; j < 7; j++) {
209 waddch(boardwin, ACS_HLINE);
210 waddch(boardwin, ACS_HLINE);
211 waddch(boardwin, ACS_HLINE);
212 waddch(boardwin, ACS_TTEE);
214 waddch(boardwin, ACS_HLINE);
215 waddch(boardwin, ACS_HLINE);
216 waddch(boardwin, ACS_HLINE);
217 waddch(boardwin, ACS_URCORNER);
219 for (i = 1; i < BDEPTH; i++) {
220 move(BOARDY + i * 2 - 1, BOARDX);
221 waddch(boardwin, ACS_VLINE);
222 for (j = 0; j < BWIDTH; j++) {
223 waddch(boardwin, ' ');
224 waddch(boardwin, ' ');
225 waddch(boardwin, ' ');
226 waddch(boardwin, ACS_VLINE);
228 move(BOARDY + i * 2, BOARDX);
229 waddch(boardwin, ACS_LTEE);
230 for (j = 0; j < BWIDTH - 1; j++) {
231 waddch(boardwin, ACS_HLINE);
232 waddch(boardwin, ACS_HLINE);
233 waddch(boardwin, ACS_HLINE);
234 waddch(boardwin, ACS_PLUS);
236 waddch(boardwin, ACS_HLINE);
237 waddch(boardwin, ACS_HLINE);
238 waddch(boardwin, ACS_HLINE);
239 waddch(boardwin, ACS_RTEE);
242 move(BOARDY + i * 2 - 1, BOARDX);
243 waddch(boardwin, ACS_VLINE);
244 for (j = 0; j < BWIDTH; j++) {
245 waddch(boardwin, ' ');
246 waddch(boardwin, ' ');
247 waddch(boardwin, ' ');
248 waddch(boardwin, ACS_VLINE);
251 move(BOARDY + i * 2, BOARDX);
252 waddch(boardwin, ACS_LLCORNER);
253 for (j = 0; j < BWIDTH - 1; j++) {
254 waddch(boardwin, ACS_HLINE);
255 waddch(boardwin, ACS_HLINE);
256 waddch(boardwin, ACS_HLINE);
257 waddch(boardwin, ACS_BTEE);
259 waddch(boardwin, ACS_HLINE);
260 waddch(boardwin, ACS_HLINE);
261 waddch(boardwin, ACS_HLINE);
262 waddch(boardwin, ACS_LRCORNER);
266 mark_possibles(int prow, int pcol, chtype mark)
270 for (n = 0; n < SIZEOF(offsets); n++) {
271 if (chksqr(prow + offsets[n].y, pcol + offsets[n].x)) {
272 cellmove(prow + offsets[n].y, pcol + offsets[n].x);
273 waddch(boardwin, mark);
279 find_next_move(int *y, int *x)
289 oldy = history[movecount - 1].y;
290 oldx = history[movecount - 1].x;
291 for (j = 0; j < SIZEOF(offsets) * 2; j++) {
292 k = j % SIZEOF(offsets);
293 newy = oldy + offsets[k].y;
294 newx = oldx + offsets[k].x;
295 if (chksqr(newy, newx)) {
301 } else if (found >= 0) {
310 *y = oldy + offsets[next].y;
311 *x = oldx + offsets[next].x;
319 unmarkcell(int row, int column)
321 cellmove(row, column);
322 waddch(boardwin, '\b');
323 waddch(boardwin, ' ');
324 waddch(boardwin, minus);
325 waddch(boardwin, ' ');
329 markcell(chtype tchar, int row, int column)
331 cellmove(row, column);
332 waddch(boardwin, '\b');
333 waddch(boardwin, tchar);
334 waddch(boardwin, tchar);
335 waddch(boardwin, tchar);
339 drawmove(chtype tchar, int oldy, int oldx, int row, int column)
340 /* place the stars, update board & currents */
342 if (movecount <= 1) {
345 for (i = 0; i < BDEPTH; i++) {
346 for (j = 0; j < BWIDTH; j++) {
347 if (movecount == 0) {
351 if (winch(boardwin) == minus)
352 waddch(boardwin, movecount ? ' ' : minus);
357 markcell(tchar, oldy, oldx);
358 mark_possibles(oldy, oldx, ' ');
361 if (row != -1 && column != -1) {
362 markcell(trail, row, column);
363 mark_possibles(row, column, minus);
364 board[row][column] = TRUE;
367 wprintw(msgwin, "\nMove %d", movecount);
368 if (trialcount != movecount)
369 wprintw(msgwin, " (%d tries)", trialcount);
383 evalmove(int row, int column)
388 else if (board[row][column] == TRUE) {
389 waddstr(msgwin, "\nYou've already been there.");
392 int rdif = iabs(row - history[movecount - 1].y);
393 int cdif = iabs(column - history[movecount - 1].x);
395 if (!((rdif == 1) && (cdif == 2)) && !((rdif == 2) && (cdif == 1))) {
396 waddstr(msgwin, "\nThat's not a legal knight's move.");
409 for (i = 0; i < BDEPTH; i++)
410 for (j = 0; j < BWIDTH; j++)
411 if (board[i][j] != 0)
413 return (count == (BWIDTH * BDEPTH) ? -1 : count);
417 no_previous_move(void)
419 waddstr(msgwin, "\nNo previous move.");
427 bool keyhelp; /* TRUE if keystroke help is up */
429 int lastcol = 0; /* last location visited */
432 int review = 0; /* review history */
433 int rw = 0, col = 0; /* current row and column */
436 /* clear screen and draw board */
442 wnoutrefresh(stdscr);
443 wnoutrefresh(helpwin);
444 wnoutrefresh(msgwin);
445 wnoutrefresh(boardwin);
449 for (i = 0; i < BDEPTH; i++) {
450 for (j = 0; j < BWIDTH; j++) {
455 memset(history, 0, sizeof(history));
456 history[0].y = history[0].x = -1;
457 history[1].y = history[1].x = -1;
458 lastrow = lastcol = -2;
465 if (rw != lastrow || col != lastcol) {
466 if (lastrow >= 0 && lastcol >= 0) {
467 cellmove(lastrow, lastcol);
468 if (board[lastrow][lastcol])
469 waddch(boardwin, trail);
471 waddch(boardwin, oldch);
475 oldch = winch(boardwin);
481 waddch(boardwin, plus);
486 switch (wgetch(boardwin)) {
490 ny = rw + BDEPTH - 1;
503 nx = col + BWIDTH - 1;
514 ny = rw + BDEPTH - 1;
515 nx = col + BWIDTH - 1;
521 nx = col + BWIDTH - 1;
526 ny = rw + BDEPTH - 1;
536 #ifdef NCURSES_MOUSE_VERSION
542 if (myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
543 && myevent.x >= CX(0) && myevent.x <= CX(BWIDTH)) {
544 nx = CXINV(myevent.x);
545 ny = CYINV(myevent.y);
553 #endif /* NCURSES_MOUSE_VERSION */
559 if (evalmove(rw, col)) {
561 history[movecount - 1].y,
562 history[movecount - 1].x,
564 history[movecount].y = rw;
565 history[movecount].x = col;
569 if (!chkmoves(rw, col)) {
570 if (completed() < 0) {
571 waddstr(msgwin, "\nYou won.");
574 "\nNo further moves are possible.");
586 if (movecount <= 0) {
588 } else if (movecount <= 1) {
589 ny = history[movecount].y;
590 nx = history[movecount].x;
591 if (nx < 0 || ny < 0) {
596 board[ny][nx] = FALSE;
598 drawmove(' ', ny, nx, -1, -1);
603 int oldy = history[movecount - 1].y;
604 int oldx = history[movecount - 1].x;
606 if (!board[rw][col]) {
608 waddch(boardwin, ' ');
611 board[oldy][oldx] = FALSE;
613 ny = history[movecount - 1].y;
614 nx = history[movecount - 1].x;
615 if (nx < 0 || ny < 0) {
619 drawmove(' ', oldy, oldx, ny, nx);
621 /* avoid problems if we just changed the current cell */
622 cellmove(lastrow, lastcol);
623 oldch = winch(boardwin);
630 find_next_move(&ny, &nx);
636 ny = history[movecount - review - 1].y;
637 nx = history[movecount - review - 1].x;
644 if (review < movecount - 2) {
646 ny = history[movecount - review - 1].y;
647 nx = history[movecount - review - 1].x;
656 clearok(curscr, TRUE);
657 wnoutrefresh(stdscr);
658 wnoutrefresh(boardwin);
659 wnoutrefresh(msgwin);
660 wnoutrefresh(helpwin);
682 if ((count = completed()) < 0)
683 wprintw(msgwin, "\nYou won. Care to try again? ");
685 wprintw(msgwin, "\n%d squares filled. Try again? ", count);
688 (tolower(wgetch(msgwin)) == 'y');
692 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
702 /* knight.c ends here */