X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fknight.c;h=ae9d223441bfbc0ab3d1e14f0fcf1e550e72f53c;hp=e759d42689af65104062458c34ac17efe762e3dd;hb=0948e2c7ac34642a1f8a3a85000933bcbb258cff;hpb=c633e5103a29a38532cf1925257b91cea33fd090 diff --git a/test/knight.c b/test/knight.c index e759d426..ae9d2234 100644 --- a/test/knight.c +++ b/test/knight.c @@ -1,3 +1,30 @@ +/**************************************************************************** + * Copyright (c) 1998-2012,2013 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 * + * "Software"), to deal in the Software without restriction, including * + * without limitation the rights to use, copy, modify, merge, publish, * + * distribute, distribute with modifications, sublicense, and/or sell * + * copies of the Software, and to permit persons to whom the Software is * + * furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included * + * in all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * + * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * + * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + * * + * Except as contained in this notice, the name(s) of the above copyright * + * holders shall not be used in advertising or otherwise to promote the * + * sale, use or other dealings in this Software without prior written * + * authorization. * + ****************************************************************************/ /* * Knight's Tour - a brain game * @@ -6,15 +33,11 @@ * Eric S. Raymond July 22 1995. Mouse support * added September 20th 1995. * - * $Id: knight.c,v 1.18 2000/10/15 18:36:34 Brian.Raiter Exp $ + * $Id: knight.c,v 1.36 2013/02/16 19:53:08 tom Exp $ */ #include -#include -#include -#include - /* board size */ #define BDEPTH 8 #define BWIDTH 8 @@ -35,8 +58,8 @@ #define PLUS_COLOR 2 #define MINUS_COLOR 3 -#define CX(x) (2 + 4 * (x)) -#define CY(y) (1 + 2 * (y)) +#define CX(x) (2 + 4 * (x)) +#define CY(y) (1 + 2 * (y)) #define cellmove(y, x) wmove(boardwin, CY(y), CX(x)) #define CXINV(x) (((x) - 1) / 4) #define CYINV(y) (((y) - 2) / 2) @@ -45,42 +68,38 @@ typedef struct { short x, y; } cell; -static short board[BDEPTH][BWIDTH]; /* the squares */ -static int rw, col; /* current row and column */ -static int lastrow, lastcol; /* last location visited */ -static cell history[BDEPTH * BWIDTH + 1]; /* choice history */ -static int movecount; /* count of moves so far */ static WINDOW *boardwin; /* the board window */ static WINDOW *helpwin; /* the help window */ static WINDOW *msgwin; /* the message window */ -static chtype trail = '#'; /* trail character */ -static chtype plus = '+'; /* cursor hot-spot character */ +static cell history[BDEPTH * BWIDTH + 1]; /* choice history */ static chtype minus = '-'; /* possible-move character */ static chtype oldch; - -static void init(void); -static void play(void); -static void dosquares(void); -static void drawmove(char, int, int, int, int); -static bool evalmove(int, int); -static bool chkmoves(void); -static bool chksqr(int, int); -static int iabs(int); - -int -main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) -{ - init(); - - play(); - - endwin(); - return EXIT_SUCCESS; -} +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 */ +/* *INDENT-OFF* */ +static const struct { + int y; + int x; +} offsets[] = { + { 2, 1 }, + { 1, 2 }, + { -1, 2 }, + { -2, 1 }, + { -2, -1 }, + { -1, -2 }, + { 1, -2 }, + { 2, -1 }, +}; +/* *INDENT-ON* */ static void -init(void) +init_program(void) { + setlocale(LC_ALL, ""); + srand((unsigned) getpid()); initscr(); cbreak(); /* immediate char return */ @@ -100,17 +119,20 @@ init(void) bg = -1; #endif - (void) init_pair(TRAIL_COLOR, COLOR_CYAN, bg); - (void) init_pair(PLUS_COLOR, COLOR_RED, bg); - (void) init_pair(MINUS_COLOR, COLOR_GREEN, bg); + (void) init_pair(TRAIL_COLOR, (short) COLOR_CYAN, (short) bg); + (void) init_pair(PLUS_COLOR, (short) COLOR_RED, (short) bg); + (void) init_pair(MINUS_COLOR, (short) COLOR_GREEN, (short) bg); - trail |= COLOR_PAIR(TRAIL_COLOR); - plus |= COLOR_PAIR(PLUS_COLOR); - minus |= COLOR_PAIR(MINUS_COLOR); + trail |= (chtype) COLOR_PAIR(TRAIL_COLOR); + plus |= (chtype) COLOR_PAIR(PLUS_COLOR); + minus |= (chtype) COLOR_PAIR(MINUS_COLOR); } #ifdef NCURSES_MOUSE_VERSION (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL); #endif /* NCURSES_MOUSE_VERSION */ +#if defined(PDCURSES) + mouse_set(BUTTON1_RELEASED); +#endif oldch = minus; } @@ -136,8 +158,8 @@ help1(void) (void) waddstr(helpwin, "puzzle; also inform you when you run out\n"); (void) waddstr(helpwin, "of legal moves.\n\n"); - (void) mvwaddstr(helpwin, NOTIFYY - INSTRY, 0, - "Press `?' to go to keystroke help."); + MvWAddStr(helpwin, NOTIFYY - INSTRY, 0, + "Press `?' to go to keystroke help."); } static void @@ -155,15 +177,294 @@ help2(void) (void) waddstr(helpwin, "x,q -- exit y k u 7 8 9\n"); (void) waddstr(helpwin, "r -- redraw screen \\|/ \\|/ \n"); (void) waddstr(helpwin, "bksp -- undo move h-+-l 4-+-6\n"); - (void) waddstr(helpwin, " /|\\ /|\\ \n"); + (void) waddstr(helpwin, "a -- autojump /|\\ /|\\ \n"); (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"); - (void) waddstr(helpwin, "center key. You can quit with `x' or `q'.\n"); + (void) waddstr(helpwin, "center key. Use F/B to review the path.\n"); + + MvWAddStr(helpwin, NOTIFYY - INSTRY, 0, + "Press `?' to go to game explanation"); +} + +static void +show_help(bool * keyhelp) +{ + werase(helpwin); + if (*keyhelp) { + help1(); + *keyhelp = FALSE; + } else { + help2(); + *keyhelp = TRUE; + } + wrefresh(helpwin); +} + +static bool +chksqr(int r1, int c1) +{ + if ((r1 < 0) || (r1 > BDEPTH - 1)) + return (FALSE); + if ((c1 < 0) || (c1 > BWIDTH - 1)) + return (FALSE); + return ((!board[r1][c1]) ? TRUE : FALSE); +} + +static bool +chkmoves(int rw, int col) +/* check to see if valid moves are available */ +{ + unsigned n; + + for (n = 0; n < SIZEOF(offsets); n++) + if (chksqr(rw + offsets[n].y, col + offsets[n].x)) + return (TRUE); + return (FALSE); +} + +static void +dosquares(void) +{ + int i, j; + + MvAddStr(0, 20, "KNIGHT'S MOVE -- a logical solitaire"); + + move(BOARDY, BOARDX); + waddch(boardwin, ACS_ULCORNER); + for (j = 0; j < 7; j++) { + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_TTEE); + } + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_URCORNER); + + for (i = 1; i < BDEPTH; i++) { + move(BOARDY + i * 2 - 1, BOARDX); + waddch(boardwin, ACS_VLINE); + for (j = 0; j < BWIDTH; j++) { + waddch(boardwin, ' '); + waddch(boardwin, ' '); + waddch(boardwin, ' '); + waddch(boardwin, ACS_VLINE); + } + move(BOARDY + i * 2, BOARDX); + waddch(boardwin, ACS_LTEE); + for (j = 0; j < BWIDTH - 1; j++) { + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_PLUS); + } + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_RTEE); + } + + move(BOARDY + i * 2 - 1, BOARDX); + waddch(boardwin, ACS_VLINE); + for (j = 0; j < BWIDTH; j++) { + waddch(boardwin, ' '); + waddch(boardwin, ' '); + waddch(boardwin, ' '); + waddch(boardwin, ACS_VLINE); + } + + move(BOARDY + i * 2, BOARDX); + waddch(boardwin, ACS_LLCORNER); + for (j = 0; j < BWIDTH - 1; j++) { + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_BTEE); + } + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_HLINE); + waddch(boardwin, ACS_LRCORNER); +} + +static void +mark_possibles(int prow, int pcol, 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); + waddch(boardwin, mark); + } + } +} + +static bool +find_next_move(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 (first < 0) + first = (int) k; + if (newy == *y + && newx == *x) { + found = (int) k; + } else if (found >= 0) { + next = (int) k; + break; + } + } + } + if (found < 0) + next = first; + if (next >= 0) { + *y = oldy + offsets[next].y; + *x = oldx + offsets[next].x; + } + result = TRUE; + } + return result; +} + +static void +count_next_moves(int y, int x) +{ + int count = 0; + unsigned j; + + wprintw(msgwin, "\nMove %d", movecount); + for (j = 0; j < SIZEOF(offsets); j++) { + int newy = y + offsets[j].y; + int newx = x + offsets[j].x; + if (chksqr(newy, newx)) { + ++count; + } + } + wprintw(msgwin, ", gives %d choices", count); + wclrtoeol(msgwin); +} + +static void +unmarkcell(int row, int column) +{ + cellmove(row, column); + waddch(boardwin, '\b'); + waddch(boardwin, ' '); + waddch(boardwin, minus); + waddch(boardwin, ' '); +} + +static void +markcell(chtype tchar, int row, int column) +{ + cellmove(row, column); + waddch(boardwin, '\b'); + waddch(boardwin, tchar); + waddch(boardwin, tchar); + waddch(boardwin, tchar); +} + +static void +drawmove(chtype tchar, int oldy, int oldx, int row, int column) +/* place the stars, update board & currents */ +{ + if (movecount <= 1) { + int i, j; - (void) mvwaddstr(helpwin, NOTIFYY - INSTRY, 0, - "Press `?' to go to game explanation"); + for (i = 0; i < BDEPTH; i++) { + for (j = 0; j < BWIDTH; j++) { + if (movecount == 0) { + unmarkcell(i, j); + } else { + cellmove(i, j); + if (winch(boardwin) == minus) + waddch(boardwin, movecount ? ' ' : minus); + } + } + } + } else { + markcell(tchar, oldy, oldx); + mark_possibles(oldy, oldx, ' '); + } + + if (row >= 0 && column >= 0) { + markcell(trail, row, column); + mark_possibles(row, column, minus); + board[row][column] = TRUE; + } + + wprintw(msgwin, "\nMove %d", movecount); + if (trialcount != movecount) + wprintw(msgwin, " (%d tries)", trialcount); + wclrtoeol(msgwin); +} + +static int +iabs(int num) +{ + if (num < 0) + return (-num); + else + return (num); +} + +static bool +evalmove(int row, int column) +/* evaluate move */ +{ + if (movecount == 1) + return (TRUE); + else if (board[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); + + if (!((rdif == 1) && (cdif == 2)) && !((rdif == 2) && (cdif == 1))) { + waddstr(msgwin, "\nThat's not a legal knight's move."); + return (FALSE); + } + } + + return (TRUE); +} + +static int +completed(void) +{ + int i, j, count = 0; + + for (i = 0; i < BDEPTH; i++) + for (j = 0; j < BWIDTH; j++) + if (board[i][j] != 0) + count += 1; + return (count == (BWIDTH * BDEPTH) ? -1 : count); +} + +static void +no_previous_move(void) +{ + waddstr(msgwin, "\nNo previous move."); + beep(); } static void @@ -171,8 +472,12 @@ play(void) /* play the game */ { bool keyhelp; /* TRUE if keystroke help is up */ - int c, ny = 0, nx = 0; int i, j, count; + int lastcol = 0; /* last location visited */ + int lastrow = 0; + int ny = 0, nx = 0; + int review = 0; /* review history */ + int rw = 0, col = 0; /* current row and column */ do { /* clear screen and draw board */ @@ -187,17 +492,21 @@ play(void) wnoutrefresh(boardwin); doupdate(); - for (i = 0; i < BDEPTH; i++) + movecount = 0; + for (i = 0; i < BDEPTH; i++) { for (j = 0; j < BWIDTH; j++) { board[i][j] = FALSE; - cellmove(i, j); - waddch(boardwin, minus); + unmarkcell(i, j); } - memset(history, '\0', sizeof(history)); + } + 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; keyhelp = FALSE; + show_help(&keyhelp); for (;;) { if (rw != lastrow || col != lastcol) { @@ -221,11 +530,7 @@ play(void) wrefresh(msgwin); - c = wgetch(boardwin); - - werase(msgwin); - - switch (c) { + switch (wgetch(boardwin)) { case 'k': case '8': case KEY_UP: @@ -275,8 +580,9 @@ play(void) nx = col + 1; break; -#ifdef NCURSES_MOUSE_VERSION +#ifdef KEY_MOUSE case KEY_MOUSE: +#ifdef NCURSES_MOUSE_VERSION { MEVENT myevent; @@ -293,52 +599,89 @@ play(void) } } #endif /* NCURSES_MOUSE_VERSION */ +#ifdef PDCURSES + { + int test_y, test_x; + 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)) { + ny = CYINV(test_y); + nx = CXINV(test_x); + wmove(helpwin, 0, 0); + wrefresh(helpwin); + ungetch('\n'); + } + break; + } +#endif /* PDCURSES */ +#endif /* KEY_MOUSE */ case KEY_B2: case '\n': case ' ': + review = 0; if (evalmove(rw, col)) { drawmove(trail, history[movecount - 1].y, history[movecount - 1].x, rw, col); - history[movecount].y = rw; - history[movecount].x = col; + history[movecount].y = (short) rw; + history[movecount].x = (short) col; movecount++; - - if (!chkmoves()) - goto dropout; - } else + trialcount++; + + if (!chkmoves(rw, col)) { + if (completed() < 0) { + waddstr(msgwin, "\nYou won."); + } else { + waddstr(msgwin, + "\nNo further moves are possible."); + } + } + } else { beep(); - break; - - case KEY_REDO: - case '\f': - case 'r': - clearok(curscr, TRUE); - wnoutrefresh(stdscr); - wnoutrefresh(boardwin); - wnoutrefresh(msgwin); - wnoutrefresh(helpwin); - doupdate(); + } break; case KEY_UNDO: case KEY_BACKSPACE: case '\b': - if (movecount == 1) { - ny = lastrow; - nx = lastcol; - waddstr(msgwin, "\nNo previous move."); - beep(); + review = 0; + if (movecount <= 0) { + no_previous_move(); + } else if (movecount <= 1) { + ny = history[movecount].y; + nx = history[movecount].x; + if (nx < 0 || ny < 0) { + ny = (lastrow >= 0) ? lastrow : 0; + nx = (lastcol >= 0) ? lastcol : 0; + } + movecount = 0; + board[ny][nx] = FALSE; + oldch = minus; + drawmove(' ', ny, nx, -1, -1); + movecount = 1; + trialcount = 1; + no_previous_move(); } else { int oldy = history[movecount - 1].y; int oldx = history[movecount - 1].x; + if (!board[rw][col]) { + cellmove(rw, col); + waddch(boardwin, ' '); + } + board[oldy][oldx] = FALSE; --movecount; ny = history[movecount - 1].y; nx = history[movecount - 1].x; + if (nx < 0 || ny < 0) { + ny = oldy; + nx = oldx; + } drawmove(' ', oldy, oldx, ny, nx); /* avoid problems if we just changed the current cell */ @@ -347,20 +690,52 @@ play(void) } break; + case 'a': + nx = col; + ny = rw; + if (find_next_move(&ny, &nx)) + count_next_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(); + } + break; + + case 'B': + if (review < movecount - 2) { + review++; + ny = history[movecount - review - 1].y; + nx = history[movecount - review - 1].x; + } else { + beep(); + } + break; + + case KEY_REDO: + case '\f': + case 'r': + clearok(curscr, TRUE); + wnoutrefresh(stdscr); + wnoutrefresh(boardwin); + wnoutrefresh(msgwin); + wnoutrefresh(helpwin); + doupdate(); + break; + case 'q': case 'x': goto dropout; case '?': - werase(helpwin); - if (keyhelp) { - help1(); - keyhelp = FALSE; - } else { - help2(); - keyhelp = TRUE; - } - wrefresh(helpwin); + show_help(&keyhelp); break; default: @@ -373,219 +748,24 @@ play(void) } dropout: - count = 0; - for (i = 0; i < BDEPTH; i++) - for (j = 0; j < BWIDTH; j++) - if (board[i][j] != 0) - count += 1; - if (count == (BWIDTH * BDEPTH)) + if ((count = completed()) < 0) wprintw(msgwin, "\nYou won. Care to try again? "); else wprintw(msgwin, "\n%d squares filled. Try again? ", count); + wclrtoeol(msgwin); } while (tolower(wgetch(msgwin)) == 'y'); } -static void -dosquares(void) -{ - int i, j; - - mvaddstr(0, 20, "KNIGHT'S MOVE -- a logical solitaire"); - - move(BOARDY, BOARDX); - waddch(boardwin, ACS_ULCORNER); - for (j = 0; j < 7; j++) { - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_TTEE); - } - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_URCORNER); - - for (i = 1; i < BDEPTH; i++) { - move(BOARDY + i * 2 - 1, BOARDX); - waddch(boardwin, ACS_VLINE); - for (j = 0; j < BWIDTH; j++) { - waddch(boardwin, ' '); - waddch(boardwin, ' '); - waddch(boardwin, ' '); - waddch(boardwin, ACS_VLINE); - } - move(BOARDY + i * 2, BOARDX); - waddch(boardwin, ACS_LTEE); - for (j = 0; j < BWIDTH - 1; j++) { - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_PLUS); - } - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_RTEE); - } - - move(BOARDY + i * 2 - 1, BOARDX); - waddch(boardwin, ACS_VLINE); - for (j = 0; j < BWIDTH; j++) { - waddch(boardwin, ' '); - waddch(boardwin, ' '); - waddch(boardwin, ' '); - waddch(boardwin, ACS_VLINE); - } - - move(BOARDY + i * 2, BOARDX); - waddch(boardwin, ACS_LLCORNER); - for (j = 0; j < BWIDTH - 1; j++) { - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_BTEE); - } - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_HLINE); - waddch(boardwin, ACS_LRCORNER); -} - -static void -mark_possibles(int prow, int pcol, chtype mark) -{ - if (chksqr(prow + 2, pcol + 1)) { - cellmove(prow + 2, pcol + 1); - waddch(boardwin, mark); - }; - if (chksqr(prow + 2, pcol - 1)) { - cellmove(prow + 2, pcol - 1); - waddch(boardwin, mark); - }; - if (chksqr(prow - 2, pcol + 1)) { - cellmove(prow - 2, pcol + 1); - waddch(boardwin, mark); - }; - if (chksqr(prow - 2, pcol - 1)) { - cellmove(prow - 2, pcol - 1); - waddch(boardwin, mark); - }; - if (chksqr(prow + 1, pcol + 2)) { - cellmove(prow + 1, pcol + 2); - waddch(boardwin, mark); - }; - if (chksqr(prow + 1, pcol - 2)) { - cellmove(prow + 1, pcol - 2); - waddch(boardwin, mark); - }; - if (chksqr(prow - 1, pcol + 2)) { - cellmove(prow - 1, pcol + 2); - waddch(boardwin, mark); - }; - if (chksqr(prow - 1, pcol - 2)) { - cellmove(prow - 1, pcol - 2); - waddch(boardwin, mark); - }; -} - -static void -drawmove(char tchar, int oldy, int oldx, int row, int column) -/* place the stars, update board & currents */ -{ - if (movecount <= 1) { - int i, j; - - for (i = 0; i < BDEPTH; i++) - for (j = 0; j < BWIDTH; j++) { - cellmove(i, j); - if (winch(boardwin) == minus) - waddch(boardwin, movecount ? ' ' : minus); - } - } else { - cellmove(oldy, oldx); - waddch(boardwin, '\b'); - waddch(boardwin, tchar); - waddch(boardwin, tchar); - waddch(boardwin, tchar); - mark_possibles(oldy, oldx, ' '); - } - - if (row != -1 && column != -1) { - cellmove(row, column); - waddch(boardwin, '\b'); - waddch(boardwin, trail); - waddch(boardwin, trail); - waddch(boardwin, trail); - mark_possibles(row, column, minus); - board[row][column] = TRUE; - } - - wprintw(msgwin, "\nMove %d", movecount); -} - -static bool -evalmove(int row, int column) -/* evaluate move */ -{ - if (movecount == 1) - return (TRUE); - else if (board[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); - - if (!((rdif == 1) && (cdif == 2)) && !((rdif == 2) && (cdif == 1))) { - waddstr(msgwin, "\nThat's not a legal knight's move."); - return (FALSE); - } - } - - return (TRUE); -} - -static bool -chkmoves(void) -/* check to see if valid moves are available */ +int +main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) { - if (chksqr(rw + 2, col + 1)) - return (TRUE); - if (chksqr(rw + 2, col - 1)) - return (TRUE); - if (chksqr(rw - 2, col + 1)) - return (TRUE); - if (chksqr(rw - 2, col - 1)) - return (TRUE); - if (chksqr(rw + 1, col + 2)) - return (TRUE); - if (chksqr(rw + 1, col - 2)) - return (TRUE); - if (chksqr(rw - 1, col + 2)) - return (TRUE); - if (chksqr(rw - 1, col - 2)) - return (TRUE); - return (FALSE); -} + init_program(); -static int -iabs(int num) -{ - if (num < 0) - return (-num); - else - return (num); -} + play(); -static bool -chksqr(int r1, int c1) -{ - if ((r1 < 0) || (r1 > BDEPTH - 1)) - return (FALSE); - if ((c1 < 0) || (c1 > BWIDTH - 1)) - return (FALSE); - return ((!board[r1][c1]) ? TRUE : FALSE); + endwin(); + ExitProgram(EXIT_SUCCESS); } /* knight.c ends here */