home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume2
/
dots2
/
person.c
< prev
next >
Wrap
C/C++ Source or Header
|
1987-09-18
|
4KB
|
147 lines
/* PERSON.C */
#include "dots.h"
#define ever (;;)
personmove()
{
char c;
int fd = ((mover == US) ? 0 : sd);
Cursor(xposit, yposit); /* do this so raw mode won't destroy screen */
raw();
noraw(); /* flush type-ahead */
bottom_line();
do {
for ever {
if (read(fd, &c, 1) <= 0)
onintr(SIGPIPE); /* someone terminated */
if (mode == TWOPLAYER)
if (c == '!' || c == 'S' || c == '.' || c == '/' || c == 'Q')
continue;
else if (c != 'r' && fd != sd)
(void) write(sd, &c, 1); /* send info to other player */
if (c == '\n' || c == ' ')
break;
Cursor(xposit, yposit);
switch (c) {
case ESC:
if (mode == TWOPLAYER)
talk();
when '?':
if (mover == US)
readinst(0), drawboard();
if (mode == TWOPLAYER)
if (mover == THEM) {
msg("%s is reading the instructions.", opponent);
if (read(fd, &c, 1) <= 0) /* wait for a char */
onintr(SIGPIPE);
} else
(void) write(sd, &c, sizeof(c));
bottom_line();
when '!':
if (shell() == -1)
perror("shell");
when 'Q':
if (mode != TWOPLAYER)
end_of_game();
else
onintr(SIGPIPE);
when 'S': Save_game();
when 'r': redraw();
when '7': case 'q': case 'y':
if (is_on_board(xposit - 1, yposit - 1))
xposit -= 1, yposit -= 1;
when '8': case 'w': case 'k':
if (is_on_board(xposit, yposit - 2))
yposit -= 2;
when '9': case 'e': case 'u':
if (is_on_board(xposit + 1, yposit - 1))
xposit += 1, yposit -= 1;
when '4': case 'a': case 'h':
if (is_on_board(xposit - 2, yposit))
xposit -= 2;
when '6': case 'd': case 'l':
if (is_on_board(xposit + 2, yposit))
xposit += 2;
when '1': case 'z': case 'b':
if (is_on_board(xposit - 1, yposit + 1))
xposit -= 1, yposit += 1;
when '2': case 'x': case 'j':
if (is_on_board(xposit, yposit + 2))
yposit += 2;
when '3': case 'c': case 'n':
if (is_on_board(xposit + 1, yposit + 1))
xposit += 1, yposit += 1;
when '.': chlevel(c);
}
Cursor(xposit, yposit);
}
if (board[xposit][yposit] == USED && mover == US) {
mvaddstr(LINES - 1, 0, "It's taken. ");
Cursor(xposit, yposit);
}
}
while (board[xposit][yposit] == USED);
return entermove();
}
#define Addch(c) addch(c); if (mode == TWOPLAYER) (void) write(sd, &c, 1)
#define backspace() Addch(back_char); Addch(space); Addch(back_char); refresh()
Getstr(String, length)
char *String;
int length;
{
char garbage, space = ' ', back_char = erase_char;
extern struct sgttyb _tty; /* curses library has this */
int count = 0;
while ((garbage = getchar()) != '\n' && garbage != 4 && garbage != ESC)
if (garbage == _tty.sg_erase && count) {
backspace();
String[count--] = 0;
} else if (garbage == _tty.sg_kill && count)
do {
backspace();
} while (--count);
else if (garbage == '\027' && count) /* ^W */
do {
backspace();
String[count--] = 0;
if (!count || (String[count-1]==' ' && !isspace(String[count])))
break;
} while (count);
else if (count == length)
fputc('\007', stderr);
else if (garbage > 31 && garbage != 127) {
String[count++] = garbage;
Addch(garbage);
refresh();
}
String[count] = 0;
}
talk()
{
char c, dummy[128], cr = '\n';
int nread = 0;
if (mover == US) {
move(LINES - 3, 15);
clrtoeol(), refresh();
Getstr(dummy, COLS - 16);
(void) write(sd, &cr, 1);
} else {
move(LINES - 2, strlen(opponent) + 3 + strlen("Messages: "));
clrtoeol();
refresh();
while ((nread = read(sd, &c, 1)) > 0 && c != cr)
addch(c), refresh();
if (nread == -1)
oops("bad socket read");
}
}