NetBSD/games/backgammon/common_source/subs.c

512 lines
8.8 KiB
C
Raw Normal View History

/* $NetBSD: subs.c,v 1.13 1999/10/04 23:26:59 lukem Exp $ */
1995-03-21 18:03:38 +03:00
1993-03-21 12:45:37 +03:00
/*
1995-03-21 18:03:38 +03:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1995-03-21 18:03:38 +03:00
#if 0
static char sccsid[] = "@(#)subs.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: subs.c,v 1.13 1999/10/04 23:26:59 lukem Exp $");
1995-03-21 18:03:38 +03:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include "back.h"
int buffnum;
char outbuff[BUFSIZ];
1993-03-21 12:45:37 +03:00
static const char plred[] = "Player is red, computer is white.";
static const char plwhite[] = "Player is white, computer is red.";
static const char nocomp[] = "(No computer play.)";
1993-03-21 12:45:37 +03:00
const char *const descr[] = {
1993-03-21 12:45:37 +03:00
"Usage: backgammon [-] [n r w b pr pw pb t3a]\n",
"\t-\tgets this list\n\tn\tdon't ask for rules or instructions",
"\tr\tplayer is red (implies n)\n\tw\tplayer is white (implies n)",
"\tb\ttwo players, red and white (implies n)",
"\tpr\tprint the board before red's turn",
"\tpw\tprint the board before white's turn",
"\tpb\tprint the board before both player's turn",
"\tterm\tterminal is a term",
"\tsfile\trecover saved game from file",
0
};
void
errexit(s)
const char *s;
1993-03-21 12:45:37 +03:00
{
write(2, "\n", 1);
perror(s);
getout(0);
1993-03-21 12:45:37 +03:00
}
int
addbuf(c)
int c;
1993-03-21 12:45:37 +03:00
{
buffnum++;
if (buffnum == BUFSIZ) {
if (write(1, outbuff, BUFSIZ) != BUFSIZ)
errexit("addbuf (write):");
1993-03-21 12:45:37 +03:00
buffnum = 0;
}
outbuff[buffnum] = c;
return (0);
1993-03-21 12:45:37 +03:00
}
void
buflush()
{
1993-03-21 12:45:37 +03:00
if (buffnum < 0)
return;
buffnum++;
if (write(1, outbuff, buffnum) != buffnum)
errexit("buflush (write):");
1993-03-21 12:45:37 +03:00
buffnum = -1;
}
int
readc()
{
char c;
1993-03-21 12:45:37 +03:00
if (tflag) {
1993-03-21 12:45:37 +03:00
cline();
newpos();
}
buflush();
if (read(0, &c, 1) != 1)
errexit("readc");
1993-03-21 12:45:37 +03:00
#ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
if (c == '\177')
getout(0);
1993-03-21 12:45:37 +03:00
#endif
if (c == '\033' || c == '\015')
return ('\n');
if (cflag)
return (c);
if (c == '\014')
return ('R');
if (c >= 'a' && c <= 'z')
return (c & 0137);
return (c);
}
void
writec(c)
char c;
1993-03-21 12:45:37 +03:00
{
if (tflag)
fancyc(c);
1993-03-21 12:45:37 +03:00
else
addbuf(c);
1993-03-21 12:45:37 +03:00
}
void
writel(l)
const char *l;
1993-03-21 12:45:37 +03:00
{
#ifdef DEBUG
const char *s;
1993-03-21 12:45:37 +03:00
if (trace == NULL)
trace = fopen("bgtrace", "w");
fprintf(trace, "writel: \"");
1993-03-21 12:45:37 +03:00
for (s = l; *s; s++) {
if (*s < ' ' || *s == '\177')
fprintf(trace, "^%c", (*s) ^ 0100);
1993-03-21 12:45:37 +03:00
else
putc(*s, trace);
1993-03-21 12:45:37 +03:00
}
fprintf(trace, "\"\n");
fflush(trace);
1993-03-21 12:45:37 +03:00
#endif
while (*l)
writec(*l++);
1993-03-21 12:45:37 +03:00
}
void
proll()
{
1993-03-21 12:45:37 +03:00
if (d0)
swap;
if (cturn == 1)
writel("Red's roll: ");
1993-03-21 12:45:37 +03:00
else
writel("White's roll: ");
writec(D0 + '0');
writec('\040');
writec(D1 + '0');
1993-03-21 12:45:37 +03:00
if (tflag)
cline();
}
void
wrint(n)
int n;
1993-03-21 12:45:37 +03:00
{
int i, j, t;
1993-03-21 12:45:37 +03:00
for (i = 4; i > 0; i--) {
1993-03-21 12:45:37 +03:00
t = 1;
for (j = 0; j < i; j++)
1993-03-21 12:45:37 +03:00
t *= 10;
if (n > t - 1)
writec((n / t) % 10 + '0');
1993-03-21 12:45:37 +03:00
}
writec(n % 10 + '0');
1993-03-21 12:45:37 +03:00
}
void
gwrite()
{
int r, c;
1993-03-21 12:45:37 +03:00
r = c = 0;
if (tflag) {
1993-03-21 12:45:37 +03:00
r = curr;
c = curc;
curmove(16, 0);
1993-03-21 12:45:37 +03:00
}
if (gvalue > 1) {
writel("Game value: ");
wrint(gvalue);
writel(". ");
1993-03-21 12:45:37 +03:00
if (dlast == -1)
writel(color[0]);
1993-03-21 12:45:37 +03:00
else
writel(color[1]);
writel(" doubled last.");
} else {
switch (pnum) {
case -1: /* player is red */
writel(plred);
1993-03-21 12:45:37 +03:00
break;
case 0: /* player is both colors */
writel(nocomp);
1993-03-21 12:45:37 +03:00
break;
case 1: /* player is white */
writel(plwhite);
1993-03-21 12:45:37 +03:00
}
}
if (rscore || wscore) {
writel(" ");
1993-03-21 12:45:37 +03:00
wrscore();
}
if (tflag) {
1993-03-21 12:45:37 +03:00
cline();
curmove(r, c);
1993-03-21 12:45:37 +03:00
}
}
int
quit()
{
1993-03-21 12:45:37 +03:00
if (tflag) {
curmove(20, 0);
1993-03-21 12:45:37 +03:00
clend();
} else
writec('\n');
writel("Are you sure you want to quit?");
if (yorn(0)) {
if (rfl) {
writel("Would you like to save this game?");
1993-03-21 12:45:37 +03:00
if (yorn(0))
save(0);
}
cturn = 0;
return (1);
}
return (0);
}
int
yorn(special)
char special; /* special response */
1993-03-21 12:45:37 +03:00
{
char c;
int i;
1993-03-21 12:45:37 +03:00
i = 1;
while ((c = readc()) != 'Y' && c != 'N') {
1993-03-21 12:45:37 +03:00
if (special && c == special)
return (2);
if (i) {
if (special) {
writel(" (Y, N, or ");
writec(special);
writec(')');
1993-03-21 12:45:37 +03:00
} else
writel(" (Y or N)");
1993-03-21 12:45:37 +03:00
i = 0;
} else
writec('\007');
1993-03-21 12:45:37 +03:00
}
if (c == 'Y')
writel(" Yes.\n");
1993-03-21 12:45:37 +03:00
else
writel(" No.\n");
1993-03-21 12:45:37 +03:00
if (tflag)
buflush();
return (c == 'Y');
}
void
wrhit(i)
int i;
1993-03-21 12:45:37 +03:00
{
writel("Blot hit on ");
wrint(i);
writec('.');
writec('\n');
1993-03-21 12:45:37 +03:00
}
void
nexturn()
{
int c;
1993-03-21 12:45:37 +03:00
cturn = -cturn;
c = cturn / abs(cturn);
1993-03-21 12:45:37 +03:00
home = bar;
bar = 25 - bar;
1993-03-21 12:45:37 +03:00
offptr += c;
offopp -= c;
inptr += c;
inopp -= c;
Colorptr += c;
colorptr += c;
}
void
getarg(arg)
char ***arg;
1993-03-21 12:45:37 +03:00
{
char **s;
1993-03-21 12:45:37 +03:00
/* process arguments here. dashes are ignored, nbrw are ignored if
* the game is being recovered */
1993-03-21 12:45:37 +03:00
s = *arg;
while (*s && s[0][0] == '-') {
switch (s[0][1]) {
1993-03-21 12:45:37 +03:00
/* don't ask if rules or instructions needed */
1993-03-21 12:45:37 +03:00
case 'n':
if (rflag)
break;
aflag = 0;
args[acnt++] = 'n';
break;
1998-09-11 17:06:30 +04:00
/* player is both red and white */
1993-03-21 12:45:37 +03:00
case 'b':
if (rflag)
break;
pnum = 0;
aflag = 0;
args[acnt++] = 'b';
break;
/* player is red */
1993-03-21 12:45:37 +03:00
case 'r':
if (rflag)
break;
pnum = -1;
aflag = 0;
args[acnt++] = 'r';
break;
/* player is white */
1993-03-21 12:45:37 +03:00
case 'w':
if (rflag)
break;
pnum = 1;
aflag = 0;
args[acnt++] = 'w';
break;
/* print board after move according to following
* character */
1993-03-21 12:45:37 +03:00
case 'p':
if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
break;
args[acnt++] = 'p';
args[acnt++] = s[0][2];
if (s[0][2] == 'r')
bflag = 1;
if (s[0][2] == 'w')
bflag = -1;
if (s[0][2] == 'b')
bflag = 0;
break;
case 't':
if (s[0][2] == '\0') { /* get terminal caps */
s++;
tflag = getcaps(*s);
1993-03-21 12:45:37 +03:00
} else
tflag = getcaps(&s[0][2]);
1993-03-21 12:45:37 +03:00
break;
case 's':
s++;
/* recover file */
if (s[0] == NULL) {
writel("No save file named\n");
getout(0);
} else
recover(s[0]);
1993-03-21 12:45:37 +03:00
break;
}
s++;
}
if (s[0] != 0)
recover(s[0]);
}
void
init()
{
int i;
1993-03-21 12:45:37 +03:00
for (i = 0; i < 26;)
board[i++] = 0;
board[1] = 2;
board[6] = board[13] = -5;
board[8] = -3;
board[12] = board[19] = 5;
board[17] = 3;
board[24] = -2;
off[0] = off[1] = -15;
in[0] = in[1] = 5;
gvalue = 1;
dlast = 0;
}
void
wrscore()
{
writel("Score: ");
writel(color[1]);
writec(' ');
wrint(rscore);
writel(", ");
writel(color[0]);
writec(' ');
wrint(wscore);
1993-03-21 12:45:37 +03:00
}
void
fixtty(t)
struct termios *t;
1993-03-21 12:45:37 +03:00
{
if (tflag)
newpos();
buflush();
if (tcsetattr(0, TCSADRAIN, t) < 0)
1993-03-21 12:45:37 +03:00
errexit("fixtty");
}
void
getout(dummy)
int dummy __attribute__((__unused__));
{
1993-03-21 12:45:37 +03:00
/* go to bottom of screen */
if (tflag) {
curmove(23, 0);
1993-03-21 12:45:37 +03:00
cline();
} else
writec('\n');
1993-03-21 12:45:37 +03:00
/* fix terminal status */
fixtty(&old);
exit(0);
1993-03-21 12:45:37 +03:00
}
void
roll()
{
char c;
int row;
int col;
row = col = 0;
if (iroll) {
if (tflag) {
1993-03-21 12:45:37 +03:00
row = curr;
col = curc;
curmove(17, 0);
1993-03-21 12:45:37 +03:00
} else
writec('\n');
writel("ROLL: ");
1993-03-21 12:45:37 +03:00
c = readc();
if (c != '\n') {
1993-03-21 12:45:37 +03:00
while (c < '1' || c > '6')
c = readc();
D0 = c - '0';
writec(' ');
writec(c);
1993-03-21 12:45:37 +03:00
c = readc();
while (c < '1' || c > '6')
c = readc();
D1 = c - '0';
writec(' ');
writec(c);
if (tflag) {
curmove(17, 0);
1993-03-21 12:45:37 +03:00
cline();
curmove(row, col);
1993-03-21 12:45:37 +03:00
} else
writec('\n');
1993-03-21 12:45:37 +03:00
return;
}
if (tflag) {
curmove(17, 0);
1993-03-21 12:45:37 +03:00
cline();
curmove(row, col);
1993-03-21 12:45:37 +03:00
} else
writec('\n');
1993-03-21 12:45:37 +03:00
}
D0 = rnum(6) + 1;
D1 = rnum(6) + 1;
1993-03-21 12:45:37 +03:00
d0 = 0;
}