home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / linux / old-src / ncurses-1.8.5 / knight.c < prev    next >
C/C++ Source or Header  |  1993-11-27  |  7KB  |  294 lines

  1. /* Knights Tour - a brain game */
  2.  
  3. #include <ncurses.h>
  4. #include <signal.h>
  5.  
  6. #ifdef __386BSD__
  7. #define srand48 srandom
  8. #define lrand48 random
  9. #endif
  10.  
  11. short   board [64];     /* the squares */
  12. char    row, column;    /* input characters */
  13. int     rw,col;         /* numeric equivalent of row and column */
  14. int     curow,curcol;   /* current row and column integers */
  15. int     rdif, cdif;     /* difference between input and current */
  16. int     j;              /* index into board */
  17.  
  18. char    script[]={"'_)//,/(-)/__/__(_<_(__),|/|/_///_/_<//_/_)__o__o'______///_(--_(_)___,(_/,_/__(_\0"};
  19.  
  20. int    ypos[] ={1,0,1,2,3,0,1,2,2,3,3,2,2,3,2,2,3,3,3,3,3,2,3,3,2,4,5,5,
  21.     4,3,3,2,1,2,3,3,3,2,1,3,3,2,3,3,3,2,1,1,0,1,4,4,4,4,4,4,5,6,7,7,
  22.     7,6,6,6,7,7,7,6,6,6,6,7,7,7,6,7,7,6,6,7,7};
  23.  
  24. int    xpos[]={0,1,2,1,0,5,4,3,2,4,6,7,8,8,9,10,10,11,12,13,14,15,15,16,
  25.     16,16,15,14,15,17,18,19,20,20,20,21,22,23,24,23,25,26,27,26,28,
  26.     13,23,25,27,27,2,3,4,5,6,7,4,3,2,1,0,1,2,5,4,5,6,6,7,8,9,8,9,10,
  27.     11,11,12,13,14,14,15};
  28.  
  29. static char *instructions[] = 
  30. {
  31. "     Knight's Tour is a board game for one player.   It is played on",
  32. "an eight by eight board and is based on the allowable moves that a knight",
  33. "can make in the game of chess.  For those who are unfamiliar with the",
  34. "game, a knight may move either on a row or a column but not diagonally.",
  35. "He may move one square in any direction and two squares in a perpendicular",
  36. "direction >or< two squares in any direction and one square in a",
  37. "perpendicular direction.  He may not, of course, move off the board.",
  38. "",
  39. "     At the beginning of a game you will be asked to either choose a",
  40. "starting square or allow the computer to select a random location.",
  41. "Squares are designated by a letter-number combination where the row is",
  42. "specified by a letter A-H and the numbers 1-8 define a column.  Invalid",
  43. "entries are ignored and illegal moves produce a beep at the terminal.",
  44. "",
  45. "     The objective is to visit every square on the board.  When you claim",
  46. "a square a marker is placed on it to show where you've been.  You may",
  47. "not revisit a square that you've landed on before.",
  48. "",
  49. "     After each move the program checks to see if you have any legal",
  50. "moves left.  If not, the game ends and your squares are counted.  If",
  51. "you've made all the squares you win the game.  Otherwise, you are told",
  52. "the number of squares you did make.",
  53. "END"
  54. };
  55.  
  56.  
  57. main ()
  58. {
  59.     init ();
  60.     for (;;)  if (!play ()) {
  61.             endwin ();
  62.             exit (0);
  63.     }
  64. }
  65.  
  66. init ()
  67. {
  68.     char c;
  69.  
  70.     srand48 (getpid());
  71.     initscr ();
  72.     signal (SIGINT,SIG_IGN); 
  73.     cbreak ();              /* immediate char return */
  74.     noecho ();              /* no immediate echo */
  75.     title (1,23);
  76.     mvaddstr (23, 25, "Would you like instructions? ");
  77.     refresh();
  78.     if ((c=toupper (getch())) == 'Y') instruct();
  79.     clear ();
  80. }
  81.  
  82. play ()
  83. {
  84.     drawboard ();           /* clear screen and drawboard */
  85.     for (j = 0; j < 64; j++) board[j]=0;
  86.     getfirst ();            /* get the starting square */
  87.     for (;;) {
  88.         getrc();
  89.         if (evalmove()) {
  90.             putstars ();
  91.             if (!chkmoves()) return (endgame ());
  92.         }
  93.         else beep();
  94.     }
  95. }
  96.  
  97. drawboard ()
  98. {
  99.     erase ();
  100.     dosquares ();
  101.     refresh ();
  102.     mvaddstr (0, 7, "1   2   3   4   5   6   7   8");
  103.     for (j = 0; j < 8; j++) mvaddch (2*j+2, 3, j + 'A');
  104.     refresh ();
  105.     mvaddstr (20,  5, "ROW:");
  106.     mvaddstr (20, 27, "COLUMN:");
  107.     mvaddstr (14, 49, "CURRENT ROW");
  108.     mvaddstr (16, 49, "CURRENT COL");
  109.     mvaddstr (22,  5, "A - H or Q to quit");
  110.     mvaddstr (22, 27, "1 - 8 or ESC to cancel row");
  111.     refresh ();
  112.     title (1,40);
  113. }
  114.  
  115. dosquares ()
  116. {
  117.     mvaddstr (1, 6, "-------------------------------");
  118.     for (j = 1; j < 9; j++){
  119.         mvaddstr (2*j, 5,  "|   |   |   |   |   |   |   |   |");
  120.         mvaddstr (2*j+1, 6, "-------------------------------");
  121.     }
  122. }
  123.  
  124. getfirst ()                             /* get first square */
  125. {
  126.     mvaddstr (23, 25, "(S)elect or (R)andom "); refresh ();
  127.     do {
  128.         row = toupper(getch());
  129.     } while ((row != 'S') && (row != 'R'));
  130.     if (row == 'R') {
  131.         rw = lrand48() % 8;
  132.         col = lrand48() % 8;
  133.         j = 8* rw + col;
  134.         row = rw + 'A';
  135.         column = col + '1';
  136.     }
  137.     else {
  138.         mvaddstr (23, 25, "Enter starting row and column");
  139.         refresh ();
  140.         getrc();                        /* get row and column */
  141.     }
  142.     putstars ();
  143.     move (23, 0);
  144.     clrtobot();
  145. }       
  146.  
  147. getrc ()                                /* get row and column */
  148. {
  149.     noecho ();
  150.     do {
  151.         mvaddstr (20, 35, "  ");
  152.         refresh ();
  153.         do {
  154.             mvaddch (20, 11, ' ');
  155.             move (20, 11);
  156.             refresh ();
  157.             row=toupper(getch());
  158.             if (row == 'Q') {
  159.                 endwin ();
  160.                 exit (1);
  161.             }
  162.         } while ((row < 'A') || (row > 'H'));
  163.         addch (row);
  164.         move (20, 35);
  165.         refresh ();
  166.         do {
  167.             column=getch();
  168.             if (column == '\033') break;
  169.         } while ((column < '1') || (column > '8'));
  170.         if (column != '\033') addch (column);
  171.     } while (column == '\033');
  172.     refresh();
  173.     rw = row - 'A';
  174.     col= column - '1';
  175.     j = 8 * rw + col;
  176. }
  177.  
  178. putstars ()                     /* place the stars, update board & currents */
  179. {
  180.     mvaddch (2*curow+2, 38, ' ');
  181.     mvaddch (2*rw+2, 38, '<');
  182.     mvaddch (18, curcol*4+7, ' ');
  183.     mvaddch (18, col*4+7, '^');
  184.     curow = rw;
  185.     curcol= col;
  186.     mvaddstr (2 * rw + 2, 4*col+6, "***");
  187.     mvaddch (14, 61, row);
  188.     mvaddch (16, 61, column);
  189.     refresh ();
  190.     board[j] = 1;
  191. }
  192.  
  193. evalmove()                      /* convert row and column to integers */
  194.                         /* and evaluate move */
  195. {
  196.     rdif = rw - curow;
  197.     cdif = col - curcol;
  198.     rdif = abs(rw  - curow);
  199.     cdif = abs(col - curcol);
  200.     refresh ();
  201.     if ((rdif == 1) && (cdif == 2)) if (board [j] == 0) return (1);
  202.     if ((rdif == 2) && (cdif == 1)) if (board [j] == 0) return (1);
  203.     return (0);
  204. }
  205.  
  206. chkmoves ()                     /* check to see if valid moves are available */
  207. {
  208.     if (chksqr(2,1))   return (1);
  209.     if (chksqr(2,-1))  return (1);
  210.     if (chksqr(-2,1))  return (1);
  211.     if (chksqr(-2,-1)) return (1);
  212.     if (chksqr(1,2))   return (1);
  213.     if (chksqr(1,-2))  return (1);
  214.     if (chksqr(-1,2))  return (1);
  215.     if (chksqr(-1,-2)) return (1);
  216.     return (0);
  217. }
  218.  
  219. endgame ()                      /* check for filled board or not */
  220. {
  221.     rw = 0;
  222.     for (j = 0; j < 64; j++) if (board[j] != 0) rw+=1;
  223.     if (rw == 64) mvaddstr (20, 20, "Congratulations !! You got 'em all");
  224.         else mvprintw (20, 20, "You have ended up with %2d squares",rw);
  225.     mvaddstr (21, 25, "Play again ? (y/n) ");
  226.     refresh ();
  227.     if ((row=tolower(getch())) == 'y') return (1);
  228.         else return (0);
  229. }
  230.  
  231. #ifndef abs
  232. abs(num)
  233. int    num;
  234. {
  235.     if (num < 0) return (-num);
  236.         else return (num);
  237. }
  238. #endif
  239.  
  240. chksqr (n1, n2)
  241. int    n1,n2;
  242.  
  243. {
  244.     int    r1, c1;
  245.  
  246.     r1 = rw + n1;
  247.     c1 = col + n2;
  248.     if ((r1<0) || (r1>7)) return (0);
  249.     if ((c1<0) || (c1>7)) return (0);
  250.     if (board[r1*8+c1] == 0) return (1);
  251.         else return (0);
  252. }
  253.  
  254. instruct()
  255. {
  256.     int i;
  257.     char    c;
  258.  
  259.     clear ();
  260.     for (i=0;;i++) {
  261.         if ((strcmp(instructions[i],"END"))) mvaddstr (i, 0, instructions[i]);
  262.             else {
  263.                 mvaddstr (23, 25, "Ready to play ? (y/n) ");
  264.                 refresh();
  265.                 if ((c=toupper(getch())) == 'Y') {
  266.                     clear ();
  267.                     return (1);
  268.                 }
  269.                     else {
  270.                         clear ();
  271.                         refresh ();
  272.                         endwin ();
  273.                         exit ();
  274.                     }
  275.             }
  276.     }
  277. }
  278.  
  279. title (y,x)
  280. int    y,x;
  281. {
  282.     char c;
  283.     j = 0;
  284.     do {
  285.         c = script[j];
  286.         if (c == 0) break ;
  287.         mvaddch (ypos[j]+y, xpos[j]+x, c);
  288.         refresh ();
  289.         j++;
  290.     } while (c != 0);
  291. }
  292.  
  293.  
  294.