X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fknight.c;h=ae9d223441bfbc0ab3d1e14f0fcf1e550e72f53c;hp=0e091f46dfa50e54fdcde54120e04180bad4c0c2;hb=17d9459c7aecedecbfc59b8ba5d29279a01e9003;hpb=46722468f47c2b77b3987729b4bcf2321cccfd01 diff --git a/test/knight.c b/test/knight.c index 0e091f46..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,11 +33,9 @@ * Eric S. Raymond July 22 1995. Mouse support * added September 20th 1995. * - * $Id: knight.c,v 1.25 2002/06/29 23:32:18 tom Exp $ + * $Id: knight.c,v 1.36 2013/02/16 19:53:08 tom Exp $ */ -#include - #include /* board size */ @@ -94,17 +119,20 @@ init_program(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; } @@ -130,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 @@ -156,8 +184,8 @@ help2(void) (void) waddstr(helpwin, "square with spacebar, Enter, or the keypad\n"); (void) waddstr(helpwin, "center key. Use F/B to review the path.\n"); - (void) mvwaddstr(helpwin, NOTIFYY - INSTRY, 0, - "Press `?' to go to game explanation"); + MvWAddStr(helpwin, NOTIFYY - INSTRY, 0, + "Press `?' to go to game explanation"); } static void @@ -201,7 +229,7 @@ dosquares(void) { int i, j; - mvaddstr(0, 20, "KNIGHT'S MOVE -- a logical solitaire"); + MvAddStr(0, 20, "KNIGHT'S MOVE -- a logical solitaire"); move(BOARDY, BOARDX); waddch(boardwin, ACS_ULCORNER); @@ -275,15 +303,16 @@ mark_possibles(int prow, int pcol, chtype mark) } } -static void +static bool find_next_move(int *y, int *x) { unsigned j, k; int found = -1; int first = -1; - int next = 0; + int next = -1; int oldy, oldx; int newy, newx; + bool result = FALSE; if (movecount > 1) { oldy = history[movecount - 1].y; @@ -294,12 +323,12 @@ find_next_move(int *y, int *x) newx = oldx + offsets[k].x; if (chksqr(newy, newx)) { if (first < 0) - first = k; + first = (int) k; if (newy == *y && newx == *x) { - found = k; + found = (int) k; } else if (found >= 0) { - next = k; + next = (int) k; break; } } @@ -310,9 +339,27 @@ find_next_move(int *y, int *x) *y = oldy + offsets[next].y; *x = oldx + offsets[next].x; } - } else { - beep(); + 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 @@ -358,7 +405,7 @@ drawmove(chtype tchar, int oldy, int oldx, int row, int column) mark_possibles(oldy, oldx, ' '); } - if (row != -1 && column != -1) { + if (row >= 0 && column >= 0) { markcell(trail, row, column); mark_possibles(row, column, minus); board[row][column] = TRUE; @@ -533,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; @@ -551,6 +599,24 @@ 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': @@ -561,8 +627,8 @@ play(void) 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++; trialcount++; @@ -589,8 +655,8 @@ play(void) ny = history[movecount].y; nx = history[movecount].x; if (nx < 0 || ny < 0) { - ny = lastrow; - nx = lastcol; + ny = (lastrow >= 0) ? lastrow : 0; + nx = (lastcol >= 0) ? lastcol : 0; } movecount = 0; board[ny][nx] = FALSE; @@ -627,7 +693,10 @@ play(void) case 'a': nx = col; ny = rw; - find_next_move(&ny, &nx); + if (find_next_move(&ny, &nx)) + count_next_moves(ny, nx); + else + beep(); break; case 'F':