#include <err.h>
#include "back.h"
#include "backlocal.h"
#define MVPAUSE 5
extern const char *const instruct[];
const char *const helpm[] = {
"Enter a space or newline to roll, or",
" R to reprint the board\tD to double",
" S to save the game\tQ to quit",
0
};
const char *const contin[] = {
"(Type a newline to continue.)",
"",
0
};
static const char rules[] = "\nDo you want the rules of the game?";
static const char noteach[] = "Teachgammon not available!\n\007";
static const char need[] = "Do you need instructions for this program?";
static const char askcol[] =
"Enter 'r' to play red, 'w' to play white, 'b' to play both:";
static const char rollr[] = "Red rolls a ";
static const char rollw[] = ". White rolls a ";
static const char rstart[] = ". Red starts.\n";
static const char wstart[] = ". White starts.\n";
static const char toobad1[] = "Too bad, ";
static const char unable[] = " is unable to use that roll.\n";
static const char toobad2[] = ". Too bad, ";
static const char cantmv[] = " can't move.\n";
static const char bgammon[] = "Backgammon! ";
static const char gammon[] = "Gammon! ";
static const char again[] = ".\nWould you like to play again?";
static const char svpromt[] = "Would you like to save this game?";
int
main (int argc, char **argv)
{
int i,l;
char c;
signal(SIGINT, getout);
begscr = 0;
getarg(argc, argv);
initcurses();
if ((rfl = rflag)) {
if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
err(1, "pledge");
wrboard();
if (cturn == 0)
rflag = 0;
} else {
if (pledge("stdio rpath wpath cpath tty exec", NULL) == -1)
err(1, "pledge");
rscore = wscore = 0;
if (aflag) {
addstr(rules);
if (yorn(0)) {
endwin();
execl(TEACH, "teachgammon", (char *)NULL);
err(1, "%s", noteach);
} else {
addstr(need);
if (yorn(0)) {
clear();
text(instruct);
}
}
}
if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
err(1, "pledge");
init();
if (pnum == 2) {
printw("\n%s", askcol);
while (pnum == 2) {
c = readc();
switch (c) {
case 'R':
pnum = -1;
break;
case 'W':
pnum = 1;
break;
case 'B':
pnum = 0;
break;
case 'P':
iroll = 1;
addstr("\nDice controlled!\n");
addstr(askcol);
break;
default:
beep();
}
}
}
wrboard();
move(18, 0);
}
begscr = 17;
for (;;) {
if ((!rflag) || raflag)
roll();
if (!rflag) {
move(17, 0);
while (D0 == D1)
roll();
printw("%s%d%s%d", rollr, D0, rollw, D1);
if (D0 > D1) {
addstr(rstart);
cturn = 1;
} else {
addstr(wstart);
cturn = -1;
}
}
if (cturn == 1) {
home = 25;
bar = 0;
inptr = &in[1];
inopp = &in[0];
offptr = &off[1];
offopp = &off[0];
Colorptr = &color[1];
colorptr = &color[3];
colen = 3;
} else {
home = 0;
bar = 25;
inptr = &in[0];
inopp = &in[1];
offptr = &off[0];
offopp = &off[1];
Colorptr = &color[0];
colorptr = &color[2];
colen = 5;
}
if (!(rflag && raflag)) {
if (cturn == pnum)
domove(0);
else {
mvlim = movallow();
move(cturn == -1 ? 18 : 19, 0);
proll();
getmove();
}
}
move(17, 0);
clrtoeol();
begscr = 18;
rflag = 0;
while (cturn == 1 || cturn == -1) {
moveplayers();
if (cturn == pnum) {
domove(1);
if (cturn == -2 || cturn == 2)
break;
if (*offopp == 15) {
cturn *= -2;
break;
}
continue;
}
if (hflag) {
move(20, 0);
clrtobot();
hflag = 1;
}
if (dflag && dlast != cturn && gvalue < 64) {
move(cturn == -1 ? 18: 19, 0);
addstr(*Colorptr);
c = readc();
switch (c) {
case 'R':
wrboard();
break;
case 'S':
raflag = 1;
save(1);
break;
case 'Q':
quit();
break;
case 'D':
dble();
break;
case ' ':
case '\n':
roll();
printw(" rolls %d %d. ", D0, D1);
if ((mvlim = movallow()) == 0) {
printw("%s%s%s", toobad1, *colorptr, unable);
if (pnum) {
moveplayers();
sleep(MVPAUSE);
}
nexturn();
break;
}
getmove();
hflag = 1;
break;
default:
move(20, 0);
text(helpm);
move(cturn == -1 ? 18 : 19, 0);
hflag = 0;
}
} else {
roll();
move(cturn == -1 ? 18: 19, 0);
proll();
if ((mvlim = movallow()) == 0) {
printw("%s%s%s", toobad2, *colorptr, cantmv);
moveplayers();
sleep(MVPAUSE);
nexturn();
continue;
}
getmove();
}
}
if (cturn == 0)
break;
cturn /= -2;
moveplayers();
mflag = 0;
l = bar + 7 * cturn;
for (i = bar; i != l; i += cturn)
if (board[i] * cturn)
mflag++;
move(20, 0);
if (*offopp == 15) {
if (mflag) {
addstr(bgammon);
gvalue *= 3;
}
else if (*offptr <= 0) {
addstr(gammon);
gvalue *= 2;
}
}
if (cturn == -1) {
addstr("Red wins ");
rscore += gvalue;
} else {
addstr("White wins ");
wscore += gvalue;
}
printw("%d point%s.\n", gvalue, (gvalue > 1) ? "s":"");
wrscore();
addstr(again);
if ((i = yorn('S')) == 0)
break;
init();
if (i == 2) {
addstr(" Save.\n");
cturn = 0;
save(0);
}
wrboard();
}
if (rfl && cturn) {
addstr(svpromt);
if (yorn(0)) {
init();
cturn = 0;
save(0);
}
}
getout(0);
}