]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/knight.c
fe3101198776b365590c73176df9f4c64b425a66
[ncurses.git] / test / knight.c
1 /*
2  * Knight's Tour - a brain game
3  *
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.
8  *
9  * $Id: knight.c,v 1.14 1997/08/20 16:22:38 hjl Exp $
10  */
11
12 #include <test.priv.h>
13
14 #include <ctype.h>
15 #include <signal.h>
16 #include <string.h>
17
18 /* board size */
19 #define BDEPTH  8
20 #define BWIDTH  8
21
22 /* where to start the instructions */
23 #define INSTRY  2
24 #define INSTRX  35
25
26 /* corner of board */
27 #define BOARDY  2
28 #define BOARDX  0
29
30 /* notification line */
31 #define NOTIFYY 21
32
33 /* virtual color values */
34 #define TRAIL_COLOR     1
35 #define PLUS_COLOR      2
36 #define MINUS_COLOR     3
37
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)
43
44 typedef struct
45 {
46     short       x, y;
47 }
48 cell;
49
50 static short    board[BDEPTH][BWIDTH];  /* the squares */
51 static int      rw,col;                 /* current row and column */
52 static int      lastrow,lastcol;        /* last location visited */
53 static cell     history[BDEPTH*BWIDTH]; /* choice history */
54 static int      movecount;              /* count of moves so far */
55 static WINDOW   *boardwin;              /* the board window */
56 static WINDOW   *helpwin;               /* the help window */
57 static WINDOW   *msgwin;                /* the message window */
58 static chtype   trail = '#';            /* trail character */
59 static chtype   plus = '+';             /* cursor hot-spot character */
60 static chtype   minus = '-';            /* possible-move character */
61 static chtype   oldch;
62
63 static void init(void);
64 static void play(void);
65 static void dosquares(void);
66 static void drawmove(char, int, int, int, int);
67 static bool evalmove(int, int);
68 static bool chkmoves(void);
69 static bool chksqr(int, int);
70 static int  iabs(int);
71
72 int main(
73         int argc GCC_UNUSED,
74         char *argv[] GCC_UNUSED)
75 {
76     init();
77
78     play();
79
80     endwin();
81     return EXIT_SUCCESS;
82 }
83
84 static void init (void)
85 {
86     srand ((unsigned)getpid());
87     initscr ();
88     cbreak ();                  /* immediate char return */
89     noecho ();                  /* no immediate echo */
90     boardwin = newwin(BDEPTH * 2 + 1, BWIDTH * 4 + 1, BOARDY, BOARDX);
91     helpwin = newwin(0, 0, INSTRY, INSTRX);
92     msgwin = newwin(1, INSTRX-1, NOTIFYY, 0);
93     scrollok(msgwin, TRUE);
94     keypad(boardwin, TRUE);
95
96     if (has_colors())
97     {
98         int bg = COLOR_BLACK;
99
100         start_color();
101 #ifdef NCURSES_VERSION
102         if (use_default_colors() == OK)
103             bg = -1;
104 #endif
105
106         (void) init_pair(TRAIL_COLOR, COLOR_CYAN,  bg);
107         (void) init_pair(PLUS_COLOR,  COLOR_RED,   bg);
108         (void) init_pair(MINUS_COLOR, COLOR_GREEN, bg);
109
110         trail |= COLOR_PAIR(TRAIL_COLOR);
111         plus  |= COLOR_PAIR(PLUS_COLOR);
112         minus |= COLOR_PAIR(MINUS_COLOR);
113     }
114
115 #ifdef NCURSES_MOUSE_VERSION
116     (void) mousemask(BUTTON1_CLICKED, (mmask_t *)NULL);
117 #endif /* NCURSES_MOUSE_VERSION*/
118
119     oldch = minus;
120 }
121
122 static void help1(void)
123 /* game explanation -- initial help screen */
124 {
125     (void)waddstr(helpwin, "Knight's move is a solitaire puzzle.  Your\n");
126     (void)waddstr(helpwin, "objective is to visit each square of the  \n");
127     (void)waddstr(helpwin, "chessboard exactly once by making knight's\n");
128     (void)waddstr(helpwin, "moves (one square right or left followed  \n");
129     (void)waddstr(helpwin, "by two squares up or down, or two squares \n");
130     (void)waddstr(helpwin, "right or left followed by one square up or\n");
131     (void)waddstr(helpwin, "down).  You may start anywhere.\n\n");
132
133     (void)waddstr(helpwin, "Use arrow keys to move the cursor around.\n");
134     (void)waddstr(helpwin, "When you want to move your knight to the \n");
135     (void)waddstr(helpwin, "cursor location, press <space> or Enter.\n");
136     (void)waddstr(helpwin, "Illegal moves will be rejected with an  \n");
137     (void)waddstr(helpwin, "audible beep.\n\n");
138     (void)waddstr(helpwin, "The program will detect if you solve the\n");
139     (void)waddstr(helpwin, "puzzle; also inform you when you run out\n");
140     (void)waddstr(helpwin, "of legal moves.\n\n");
141
142     (void)mvwaddstr(helpwin, NOTIFYY-INSTRY, 0,
143                     "Press `?' to go to keystroke help."); 
144 }
145
146 static void help2(void)
147 /* keystroke help screen */
148 {
149     (void)waddstr(helpwin, "Possible moves are shown with `-'.\n\n");
150
151     (void)waddstr(helpwin, "You can move around with the arrow keys or\n");
152     (void)waddstr(helpwin, "with the rogue/hack movement keys.  Other\n");
153     (void)waddstr(helpwin, "commands allow you to undo moves or redraw.\n");
154     (void)waddstr(helpwin, "Your mouse may work; try left-button to\n");
155     (void)waddstr(helpwin, "move to the square under the pointer.\n\n");
156
157     (void)waddstr(helpwin, "x,q -- exit             y k u    7 8 9\n");
158     (void)waddstr(helpwin, "r -- redraw screen       \\|/      \\|/ \n");
159     (void)waddstr(helpwin, "u -- undo move          h-+-l    4-+-6\n");
160     (void)waddstr(helpwin, "                         /|\\      /|\\ \n");
161     (void)waddstr(helpwin, "                        b j n    1 2 3\n");
162
163     (void)waddstr(helpwin,"\nYou can place your knight on the selected\n");
164     (void)waddstr(helpwin, "square with spacebar, Enter, or the keypad\n");
165     (void)waddstr(helpwin, "center key.  You can quit with `x' or `q'.\n");
166
167     (void)mvwaddstr(helpwin, NOTIFYY-INSTRY, 0,
168                     "Press `?' to go to game explanation"); 
169 }
170
171 static void play (void)
172 /* play the game */
173 {
174     bool                keyhelp; /* TRUE if keystroke help is up */
175     int c, ny = 0, nx = 0;
176     int i, j, count;
177
178     do {
179            /* clear screen and draw board */
180            werase(boardwin);
181            werase(helpwin);
182            werase(msgwin);
183            dosquares();
184            help1();
185            wnoutrefresh(stdscr);
186            wnoutrefresh(helpwin);
187            wnoutrefresh(msgwin);
188            wnoutrefresh(boardwin);
189            doupdate();
190
191            for (i = 0; i < BDEPTH; i++)
192                for (j = 0; j < BWIDTH; j++)
193                {
194                    board[i][j] = FALSE;
195                    cellmove(i, j);
196                    waddch(boardwin, minus);
197                }
198            memset(history, '\0', sizeof(history));
199            history[0].y = history[0].x = -1;
200            lastrow = lastcol = -2;
201            movecount = 1;
202            keyhelp = FALSE;
203
204            for (;;)
205            {
206                if (rw != lastrow || col != lastcol)
207                {
208                    if (lastrow >= 0 && lastcol >= 0)
209                    {
210                        cellmove(lastrow, lastcol);
211                        if (board[lastrow][lastcol])
212                            waddch(boardwin, trail);
213                        else
214                            waddch(boardwin, oldch);
215                    }
216
217                    cellmove(rw, col);
218                    oldch = winch(boardwin);
219
220                    lastrow = rw;
221                    lastcol= col;
222                }
223                cellmove(rw, col);
224                waddch(boardwin, plus);
225                cellmove(rw, col);
226
227                wrefresh(msgwin);
228
229                c = wgetch(boardwin);
230
231                werase(msgwin);
232
233                switch (c)
234                {
235                case 'k': case '8':
236                case KEY_UP:
237                    ny = rw+BDEPTH-1; nx = col;
238                    break;
239                case 'j': case '2':
240                case KEY_DOWN:
241                    ny = rw+1;        nx = col;
242                    break;
243                case 'h': case '4':
244                case KEY_LEFT:
245                    ny = rw;          nx = col+BWIDTH-1;
246                    break;
247                case 'l': case '6':
248                case KEY_RIGHT:
249                    ny = rw;          nx = col+1;
250                    break;
251                case 'y': case '7':
252                case KEY_A1:
253                    ny = rw+BDEPTH-1; nx = col+BWIDTH-1;
254                    break;
255                case 'b': case '1':
256                case KEY_C1:
257                    ny = rw+1;        nx = col+BWIDTH-1;
258                    break;
259                case 'u': case '9':
260                case KEY_A3:
261                    ny = rw+BDEPTH-1; nx = col+1;
262                    break;
263                case 'n': case '3':
264                case KEY_C3:
265                    ny = rw+1;        nx = col+1;
266                    break;
267
268 #ifdef NCURSES_MOUSE_VERSION
269                case KEY_MOUSE:
270                    {
271                        MEVENT   myevent;
272
273                        getmouse(&myevent);
274                        if (myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
275                            && myevent.x >= CX(0) && myevent.x <= CX(BWIDTH))
276                        {
277                            nx = CXINV(myevent.x);
278                            ny = CYINV(myevent.y);
279                            ungetch('\n');
280                            break;
281                        }
282                        else
283                        {
284                            beep();
285                            continue;
286                        }
287                    }
288 #endif /* NCURSES_MOUSE_VERSION */
289
290                case KEY_B2:
291                case '\n':
292                case ' ':
293                    if (evalmove(rw, col))
294                    {
295                        drawmove(trail,
296                                 history[movecount-1].y, history[movecount-1].x,
297                                 rw, col);
298                        history[movecount].y = rw; 
299                        history[movecount].x = col; 
300                        movecount++;
301
302                        if (!chkmoves()) 
303                            goto dropout;
304                    }
305                    else
306                        beep();
307                    break;
308
309                case KEY_REDO:
310                case '\f':
311                case 'r':
312                    clearok(curscr, TRUE);
313                    wnoutrefresh(stdscr);
314                    wnoutrefresh(boardwin);
315                    wnoutrefresh(msgwin);
316                    wnoutrefresh(helpwin);
317                    doupdate();
318                    break;
319
320                case KEY_UNDO:
321                case KEY_BACKSPACE:
322                case '\b':
323                    if (movecount == 1)
324                    {
325                        ny = lastrow;
326                        nx = lastcol;
327                        waddstr(msgwin, "\nNo previous move.");
328                        beep();
329                    }
330                    else
331                    {
332                        int oldy = history[movecount-1].y;
333                        int oldx = history[movecount-1].x;
334
335                        board[oldy][oldx] = FALSE;
336                        --movecount;
337                        ny = history[movecount-1].y;
338                        nx = history[movecount-1].x;
339                        drawmove(' ', oldy, oldx, ny, nx);
340
341                        /* avoid problems if we just changed the current cell */
342                        cellmove(lastrow, lastcol);
343                        oldch = winch(boardwin);
344                    }
345                    break;
346
347                case 'q':
348                case 'x':
349                    goto dropout;
350
351                case '?':
352                    werase(helpwin);
353                    if (keyhelp)
354                    {
355                        help1();
356                        keyhelp = FALSE;
357                    }
358                    else
359                    {
360                        help2();
361                        keyhelp = TRUE;
362                    }
363                    wrefresh(helpwin);
364                    break;
365
366                default:
367                    beep();
368                    break;
369                }
370
371                col = nx % BWIDTH;
372                rw = ny % BDEPTH;
373            }
374
375        dropout:
376            count = 0;
377            for (i = 0; i < BDEPTH; i++)
378                for (j = 0; j < BWIDTH; j++)
379                    if (board[i][j] != 0)
380                        count += 1;
381            if (count == (BWIDTH * BDEPTH))
382                wprintw(msgwin, "\nYou won.  Care to try again? ");
383            else
384                wprintw(msgwin, "\n%d squares filled.  Try again? ", count);
385        } while
386            (tolower(wgetch(msgwin)) == 'y');
387 }
388
389 static void dosquares (void)
390 {
391     int i, j;
392
393     mvaddstr(0, 20, "KNIGHT'S MOVE -- a logical solitaire");
394
395     move(BOARDY,BOARDX);
396     waddch(boardwin, ACS_ULCORNER);
397     for (j = 0; j < 7; j++)
398     {
399         waddch(boardwin, ACS_HLINE);
400         waddch(boardwin, ACS_HLINE);
401         waddch(boardwin, ACS_HLINE);
402         waddch(boardwin, ACS_TTEE);
403     }
404     waddch(boardwin, ACS_HLINE);
405     waddch(boardwin, ACS_HLINE);
406     waddch(boardwin, ACS_HLINE);
407     waddch(boardwin, ACS_URCORNER);
408
409     for (i = 1; i < BDEPTH; i++)
410     {
411         move(BOARDY + i * 2 - 1, BOARDX);
412         waddch(boardwin, ACS_VLINE); 
413         for (j = 0; j < BWIDTH; j++)
414         {
415             waddch(boardwin, ' ');
416             waddch(boardwin, ' ');
417             waddch(boardwin, ' ');
418             waddch(boardwin, ACS_VLINE);
419         }
420         move(BOARDY + i * 2, BOARDX);
421         waddch(boardwin, ACS_LTEE); 
422         for (j = 0; j < BWIDTH - 1; j++)
423         {
424             waddch(boardwin, ACS_HLINE);
425             waddch(boardwin, ACS_HLINE);
426             waddch(boardwin, ACS_HLINE);
427             waddch(boardwin, ACS_PLUS);
428         }
429         waddch(boardwin, ACS_HLINE);
430         waddch(boardwin, ACS_HLINE);
431         waddch(boardwin, ACS_HLINE);
432         waddch(boardwin, ACS_RTEE);
433     }
434
435     move(BOARDY + i * 2 - 1, BOARDX);
436     waddch(boardwin, ACS_VLINE);
437     for (j = 0; j < BWIDTH; j++)
438     {
439         waddch(boardwin, ' ');
440         waddch(boardwin, ' ');
441         waddch(boardwin, ' ');
442         waddch(boardwin, ACS_VLINE);
443     }
444
445     move(BOARDY + i * 2, BOARDX);
446     waddch(boardwin, ACS_LLCORNER);
447     for (j = 0; j < BWIDTH - 1; j++)
448     {
449         waddch(boardwin, ACS_HLINE);
450         waddch(boardwin, ACS_HLINE);
451         waddch(boardwin, ACS_HLINE);
452         waddch(boardwin, ACS_BTEE);
453     }
454     waddch(boardwin, ACS_HLINE);
455     waddch(boardwin, ACS_HLINE);
456     waddch(boardwin, ACS_HLINE);
457     waddch(boardwin, ACS_LRCORNER);
458 }
459
460 static void mark_possibles(int prow, int pcol, chtype mark)
461 {
462     if (chksqr(prow+2,pcol+1)){cellmove(prow+2,pcol+1);waddch(boardwin,mark);};
463     if (chksqr(prow+2,pcol-1)){cellmove(prow+2,pcol-1);waddch(boardwin,mark);};
464     if (chksqr(prow-2,pcol+1)){cellmove(prow-2,pcol+1);waddch(boardwin,mark);};
465     if (chksqr(prow-2,pcol-1)){cellmove(prow-2,pcol-1);waddch(boardwin,mark);};
466     if (chksqr(prow+1,pcol+2)){cellmove(prow+1,pcol+2);waddch(boardwin,mark);};
467     if (chksqr(prow+1,pcol-2)){cellmove(prow+1,pcol-2);waddch(boardwin,mark);};
468     if (chksqr(prow-1,pcol+2)){cellmove(prow-1,pcol+2);waddch(boardwin,mark);};
469     if (chksqr(prow-1,pcol-2)){cellmove(prow-1,pcol-2);waddch(boardwin,mark);};
470 }
471
472 static void drawmove(char tchar, int oldy, int oldx, int row, int column)
473 /* place the stars, update board & currents */
474 {
475     if (movecount <= 1)
476     {
477         int i, j;
478
479         for (i = 0; i < BDEPTH; i++)
480             for (j = 0; j < BWIDTH; j++)
481             {
482                 cellmove(i, j);
483                 if (winch(boardwin) == minus)
484                     waddch(boardwin, movecount ? ' ' : minus);
485             }
486     }
487     else
488     {
489         cellmove(oldy, oldx);
490         waddch(boardwin, '\b');
491         waddch(boardwin, tchar);
492         waddch(boardwin, tchar);
493         waddch(boardwin, tchar);
494         mark_possibles(oldy, oldx, ' ');
495     }
496
497     if (row != -1 && column != -1)
498     {
499         cellmove(row, column);
500         waddch(boardwin, '\b');
501         waddch(boardwin, trail);
502         waddch(boardwin, trail);
503         waddch(boardwin, trail);
504         mark_possibles(row, column, minus);
505         board[row][column] = TRUE;
506     }
507
508     wprintw(msgwin, "\nMove %d", movecount);
509 }
510
511 static bool evalmove(int row, int column)
512 /* evaluate move */
513 {
514     if (movecount == 1)
515         return(TRUE);
516     else if (board[row][column] == TRUE)
517     {
518         waddstr(msgwin, "\nYou've already been there.");
519         return(FALSE);
520     }
521     else
522     {
523         int     rdif = iabs(row  - history[movecount-1].y);
524         int     cdif = iabs(column - history[movecount-1].x);
525
526         if (!((rdif == 1) && (cdif == 2)) && !((rdif == 2) && (cdif == 1)))
527         {
528             waddstr(msgwin, "\nThat's not a legal knight's move.");
529             return(FALSE);
530         }
531     }
532
533     return(TRUE);
534 }
535
536 static bool chkmoves (void)
537 /* check to see if valid moves are available */
538 {
539     if (chksqr(rw+2,col+1)) return(TRUE);
540     if (chksqr(rw+2,col-1)) return(TRUE);
541     if (chksqr(rw-2,col+1)) return(TRUE);
542     if (chksqr(rw-2,col-1)) return(TRUE);
543     if (chksqr(rw+1,col+2)) return(TRUE);
544     if (chksqr(rw+1,col-2)) return(TRUE);
545     if (chksqr(rw-1,col+2)) return(TRUE);
546     if (chksqr(rw-1,col-2)) return(TRUE);
547     return (FALSE);
548 }
549
550 static int iabs(int num)
551 {
552         if (num < 0) return (-num);
553                 else return (num);
554 }
555
556 static bool chksqr (int r1, int c1)
557 {
558     if ((r1 < 0) || (r1 > BDEPTH - 1))
559         return(FALSE);
560     if ((c1 < 0) || (c1 > BWIDTH - 1))
561         return(FALSE);
562     return ((!board[r1][c1]) ? TRUE : FALSE);
563 }
564
565 /* knight.c ends here */