home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume2 / dots2 / person.c < prev    next >
C/C++ Source or Header  |  1987-09-18  |  4KB  |  147 lines

  1. /*    PERSON.C    */
  2.  
  3. #include "dots.h"
  4.  
  5. #define ever (;;)
  6.  
  7. personmove()
  8. {
  9.     char c;
  10.     int fd = ((mover == US) ? 0 : sd);
  11.  
  12.     Cursor(xposit, yposit);    /* do this so raw mode won't destroy screen */
  13.     raw();
  14.     noraw();        /* flush type-ahead */
  15.     bottom_line();
  16.     do  {
  17.     for ever {
  18.         if (read(fd, &c, 1) <= 0)
  19.         onintr(SIGPIPE);    /* someone terminated */
  20.         if (mode == TWOPLAYER)
  21.         if (c == '!' || c == 'S' || c == '.' || c == '/' || c == 'Q')
  22.             continue;
  23.         else if (c != 'r' && fd != sd)
  24.             (void) write(sd, &c, 1);    /* send info to other player */
  25.         if (c == '\n' || c == ' ')
  26.         break;
  27.         Cursor(xposit, yposit);
  28.         switch (c) {
  29.         case ESC:
  30.             if (mode == TWOPLAYER)
  31.                 talk();
  32.  
  33.         when '?':
  34.             if (mover == US)
  35.             readinst(0), drawboard();
  36.             if (mode == TWOPLAYER)
  37.             if (mover == THEM) {
  38.                 msg("%s is reading the instructions.", opponent);
  39.                 if (read(fd, &c, 1) <= 0)    /* wait for a char */
  40.                 onintr(SIGPIPE);
  41.             } else
  42.                 (void) write(sd, &c, sizeof(c));
  43.             bottom_line();
  44.         when '!':
  45.             if (shell() == -1)
  46.             perror("shell");
  47.         when 'Q':
  48.             if (mode != TWOPLAYER)
  49.             end_of_game();
  50.             else
  51.             onintr(SIGPIPE);
  52.         when 'S': Save_game();
  53.         when 'r': redraw();
  54.             when '7': case 'q': case 'y':
  55.             if (is_on_board(xposit - 1, yposit - 1))
  56.                 xposit -= 1, yposit -= 1;
  57.             when '8': case 'w': case 'k':
  58.             if (is_on_board(xposit, yposit - 2))
  59.                 yposit -= 2;
  60.             when '9': case 'e': case 'u':
  61.             if (is_on_board(xposit + 1, yposit - 1))
  62.                 xposit += 1, yposit -= 1;
  63.             when '4': case 'a': case 'h':
  64.             if (is_on_board(xposit - 2, yposit))
  65.                 xposit -= 2;
  66.             when '6': case 'd': case 'l':
  67.             if (is_on_board(xposit + 2, yposit))
  68.                 xposit += 2;
  69.             when '1': case 'z': case 'b':
  70.             if (is_on_board(xposit - 1, yposit + 1))
  71.                 xposit -= 1, yposit += 1;
  72.             when '2': case 'x': case 'j':
  73.             if (is_on_board(xposit, yposit + 2))
  74.                 yposit += 2;
  75.             when '3': case 'c': case 'n':
  76.             if (is_on_board(xposit + 1, yposit + 1))
  77.                 xposit += 1, yposit += 1;
  78.         when '.': chlevel(c);
  79.         }
  80.         Cursor(xposit, yposit);
  81.     }
  82.     if (board[xposit][yposit] == USED && mover == US) {
  83.         mvaddstr(LINES - 1, 0, "It's taken.   ");
  84.         Cursor(xposit, yposit);
  85.     }
  86.     }
  87.     while (board[xposit][yposit] == USED);
  88.     return entermove();
  89. }
  90.  
  91. #define Addch(c)    addch(c); if (mode == TWOPLAYER) (void) write(sd, &c, 1)
  92. #define backspace() Addch(back_char); Addch(space); Addch(back_char); refresh()
  93.  
  94. Getstr(String, length)
  95. char *String;
  96. int length;
  97. {
  98.     char garbage, space = ' ', back_char = erase_char;
  99.     extern struct sgttyb _tty; /* curses library has this */
  100.     int count = 0;
  101.  
  102.     while ((garbage = getchar()) != '\n' && garbage != 4 && garbage != ESC)
  103.     if (garbage == _tty.sg_erase && count) {
  104.         backspace();
  105.         String[count--] = 0;
  106.     } else if (garbage == _tty.sg_kill && count)
  107.         do {
  108.         backspace();
  109.         } while (--count);
  110.     else if (garbage == '\027' && count)    /* ^W */
  111.         do  {
  112.         backspace();
  113.         String[count--] = 0;
  114.         if (!count || (String[count-1]==' ' && !isspace(String[count])))
  115.             break;
  116.         } while (count);
  117.     else if (count == length)
  118.         fputc('\007', stderr);
  119.     else if (garbage > 31 && garbage != 127) {
  120.         String[count++] = garbage;
  121.         Addch(garbage);
  122.         refresh();
  123.     }
  124.     String[count] = 0;
  125. }
  126.  
  127. talk()
  128. {
  129.     char c, dummy[128], cr = '\n';
  130.     int nread = 0;
  131.  
  132.     if (mover == US) {
  133.     move(LINES - 3, 15);
  134.     clrtoeol(), refresh();
  135.     Getstr(dummy, COLS - 16);
  136.     (void) write(sd, &cr, 1);
  137.     } else {
  138.     move(LINES - 2, strlen(opponent) + 3 + strlen("Messages: "));
  139.     clrtoeol();
  140.     refresh();
  141.     while ((nread = read(sd, &c, 1)) > 0 && c != cr)
  142.         addch(c), refresh();
  143.     if (nread == -1)
  144.         oops("bad socket read");
  145.     }
  146. }
  147.