home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume1
/
sol
/
sol.c
< prev
next >
Wrap
C/C++ Source or Header
|
1987-07-20
|
17KB
|
879 lines
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#define TRUE 1
#define FALSE 0
int amode;
int cnt;
int cheat;
int deck[53];
int over[53];
int ace[4][14];
int run[7][14];
int hidden[7][8];
char cmd[40], *cp;
char *resgb;
char PC;
char BC[2];
char UP[2];
char *sc_bs;
char *sc_move;
char *sc_clear;
char *sc_cleol;
char *sc_init;
char *sc_deinit;
char *sc_s_in;
char *sc_s_out;
short ospeed;
main(n, a)
char **a;
{
int i, p, c, r, s;
struct timeval tp;
struct timezone tzp;
int from;
int dest;
int brkflg;
amode = 0;
gettimeofday(&tp, &tzp);
srand(getpid() + tp.tv_sec);
rawmode(TRUE);
setsig(TRUE);
getterm();
init();
for (i = 0; i < 52; i++)
deck[i] = i;
deck[52] = -1;
shuffle(deck);
for (i = 0; i < 4; i++)
ace[i][0] = -1;
for (i = 0; i < 7; i++)
hidden[i][0] = run[i][0] = -1;
over[0] = -1;
for (p = 0; p < 7; p++)
for (c = p; c < 7; c++)
put(hidden[c],get(deck));
for (r = 0; r < 7; r++)
put(run[r],get(hidden[r]));
redraw(TRUE);
for (r = 0; r < 7; r++)
{
while (getvalue(run[r][0]) == 0)
{
s = getsuit(run[r][0]);
put(ace[s],get(run[r]));
put(run[r],get(hidden[r]));
redraw(FALSE);
}
}
cnt = cheat = 0;
if (n > 1 && !strcmp(a[1], "auto"))
autopilot();
brkflg = 1;
while (brkflg)
{
cnt++;
do
{
moveto(17, 0);
printf("cmd:%d> ", cnt);
cleol();
fflush(stdout);
} while (!getst(cp = cmd));
while (*cp == ' ' || *cp == '\t')
cp++;
switch (*cp++)
{
case 'a':
autopilot();
break;
case 'm':
{
while (*cp == ' ' || *cp == '\t')
cp++;
if (!(from = *cp++) || ((from < '1' || from > '7') &&
from != 'd'))
{
whoops();
continue;
}
while (*cp == ' ' || *cp == '\t')
cp++;
if (!(dest = *cp++) || ((dest < '1' || dest > '7') &&
dest != 'a') || !movecard(from, dest, FALSE))
{
whoops();
continue;
}
if (cardsleft() == 0)
finish();
}
break;
case 't':
case 0:
thumb();
break;
case 'h':
case '?':
disphelp();
break;
case 'r':
disprules();
break;
case 'q':
brkflg = 0;
break;
case 's':
{
while (peek(over) != -1)
put(deck, get(over));
shuffle(deck);
redraw(FALSE);
cheat++;
}
break;
case 'p':
{
while (*cp == ' ' || *cp == '\t')
cp++;
if (!(from = *cp++) || from < '1' || from > '7')
{
whoops();
continue;
}
if (hidden[from -= '1'][0] != -1)
{
while (hidden[from][0] != -1)
put(deck, get(hidden[from]));
redraw(FALSE);
cheat++;
}
else
whoops();
}
break;
case 'd':
{
moveto(19, 0);
printf("%d cards in deck:\n", lstlen(deck) + lstlen(over));
i = lstlen(deck);
while (--i != -1)
{
pcard(deck[i]);
putchar(' ');
}
putchar('\n');
for (i = 0; over[i] != -1; i++)
{
pcard(over[i]);
putchar(' ');
}
printf("\nHit return -");
fflush(stdout);
getcx();
moveto(19, 0);
for (i = 0; i < 4; i++)
{
cleol();
putchar('\n');
}
cheat++;
}
break;
case 'w':
{
while (*cp == ' ' || *cp == '\t')
cp++;
if (!(from = *cp++) || from < '1' || from > '7')
{
whoops();
continue;
}
moveto(19, 0);
from -= '1';
printf("%d cards hidden under run %c:\n", lstlen(hidden[from]),
from + '1');
for (i = 0; hidden[from][i] != -1; i++)
{
pcard(hidden[from][i]);
putchar(' ');
}
printf("\nHit return -");
fflush(stdout);
getcx();
moveto(19,0);
for (i = 0; i < 4; i++)
{
cleol();
putchar('\n');
}
cheat++;
}
break;
default:
whoops();
break;
}
}
moveto(19, 0);
cleol();
printf("I see you gave up\n");
if (cheat > 0)
printf(" ... even after you cheated %d times!\n", cheat);
else
printf(" ... but at least you didn't cheat...congratulations!\n");
unwind();
}
unwind()
{
moveto(23, 0);
deinit();
rawmode(FALSE);
exit(0);
}
movecard(from,dest,limitmove)
{
if (from == dest)
return(FALSE);
if (from == 'd')
return(dest == 'a' ? deck2ace() : deck2run(dest));
else
return(dest == 'a' ? run2ace(from) : run2run(from,dest,limitmove));
}
deck2run(dest)
{
int fcard, dcard, s, ok;
if ((fcard = peek(over)) == -1)
return(FALSE);
dcard = peek(run[dest -= '1']);
if (ok = chk2run(fcard,dcard))
{
put(run[dest],get(over));
redraw(FALSE);
while (getvalue(peek(over)) == 0)
{
s = getsuit(peek(over));
put(ace[s],get(over));
redraw(FALSE);
}
}
return(ok);
}
deck2ace()
{
int fcard, s, ok;
if (ok = (fcard = peek(over)) != -1 &&
getvalue(peek(ace[s = getsuit(fcard)])) == getvalue(fcard) - 1)
{
put(ace[s], get(over));
redraw(FALSE);
while (getvalue(peek(over)) == 0)
{
s = getsuit(peek(over));
put(ace[s],get(over));
redraw(FALSE);
}
}
return(ok);
}
run2ace(from)
{
int fcard, s, ok;
fcard = peek(run[from -= '1']);
if (ok = getvalue(peek(ace[s = getsuit(fcard)])) == getvalue(fcard) - 1)
{
put(ace[s], get(run[from]));
redraw(FALSE);
if (run[from][0] == -1 && hidden[from][0] >= 0)
{
while (getvalue(peek(hidden[from])) == 0)
{
s = getsuit(peek(hidden[from]));
put(ace[s],get(hidden[from]));
redraw(FALSE);
}
put(run[from],get(hidden[from]));
redraw(FALSE);
}
}
return(ok);
}
run2run(from,dest,limitmove)
{
int fcard, dcard, i, ok, s;
if ((fcard = run[from -= '1'][0]) == -1)
return(FALSE);
dcard = peek(run[dest -= '1']);
if (amode > 0 && dcard == -1 && getvalue(fcard) == 12 &&
lstlen(hidden[from]) == 0)
return(FALSE);
if (amode > 0 && !limitmove && lstlen(hidden[from]) == 0)
return(FALSE);
if (ok = chk2run(fcard,dcard))
{
for (i = 0; run[from][i] != -1; i++)
put(run[dest], run[from][i]);
run[from][0] = -1;
redraw(FALSE);
if (lstlen(hidden[from]) > 0)
{
while (getvalue(peek(hidden[from])) == 0)
{
s = getsuit(peek(hidden[from]));
put(ace[s],get(hidden[from]));
redraw(FALSE);
}
put(run[from],get(hidden[from]));
redraw(FALSE);
}
}
return(ok);
}
chk2run(fcard,dcard)
{
if (dcard == -1 && getvalue(fcard) == 12)
return(TRUE);
return(getvalue(dcard) - 1 == getvalue(fcard) &&
getcolour(dcard) != getcolour(fcard));
}
finish()
{
int i, k;
moveto(19,0);
printf("\007I'll finish for you now\007");
fflush(stdout);
do
{
k = FALSE;
for (i = 0; i < 7; i++)
k = movecard(i + '1', 'a', FALSE) || k;
} while (k);
moveto(20,0);
cleol();
printf("\007You WIN\007\n");
if (cheat > 0)
printf(" ... but you cheated %d times!", cheat);
else
printf(" ... and without cheating ... congratulations!");
unwind();
}
autopilot()
{
int i, j, k;
int iter;
moveto(19,0);
printf("Going into automatic mode...");
fflush(stdout);
amode = TRUE;
while (cardsleft() > 0)
{
k = FALSE;
for (i = 0; i < 7; i++)
for (j = 0; j < 7; j++)
k = movecard(i + '1', j + '1', TRUE) || k;
for (i = 0; i < 7; i++)
{
k = movecard(i + '1', 'a', FALSE) || k;
k = movecard('d', i + '1', FALSE) || k;
}
k = movecard('d', 'a', FALSE) || k;
if (!k)
{
if (iter--)
thumb();
else
break;
}
else
iter = lstlen(over) + lstlen(deck);
}
do
{
k = FALSE;
for (i = 0; i < 7; i++)
k = movecard(i + '1', 'a', FALSE) || k;
} while (k);
moveto(19, 28);
if (cardsleft() == 0)
{
printf("\007YEA...\007");
fflush(stdout);
for (i = 0; i < 7; i++)
if (movecard(i + '1', 'a', FALSE));
moveto(19, 34);
printf("I won!!!!!");
}
else
{
printf("I couldn't win this time");
moveto(20, 0);
printf("%d cards in deck", lstlen(deck) + lstlen(over));
}
unwind();
}
redraw(display)
{
static long_run[7];
static long_hide[7];
static long_ace[4];
static base_run[7];
int i, j, k;
if (display)
{
clear();
for (i = 0; i < 7; i++)
{
long_run[i] = long_hide[i] = base_run[i] = -1;
moveto(0, 8 + i * 5);
putchar(i + '1');
}
for (i = 0; i < 4; i++)
long_ace[i] = -1;
moveto(0,56);
printf("ACES");
moveto(6,56);
printf("DECK");
}
for (i = 0; i < 7; i++)
{
if (long_hide[i] != lstlen(hidden[i]))
{
moveto(2, 8 + i * 5);
putchar(lstlen(hidden[i]) ? lstlen(hidden[i]) + '0' : ' ');
long_hide[i] = lstlen(hidden[i]);
}
}
for (i = 0; i < 4; i++)
{
if (long_ace[i] != lstlen(ace[i]))
{
moveto(2, 49 + i * 5);
if (ace[i][0] != -1)
pcard(peek(ace[i]));
else
printf("--");
long_ace[i] = lstlen(ace[i]);
}
}
moveto(8, 55);
printf(lstlen(deck) ? "## " : " ");
if (lstlen(over))
pcard(peek(over));
else
printf(" ");
for (i = 0; i < 7; i++)
{
if (long_run[i] != lstlen(run[i]) ||
(long_run[i] == 1 && base_run[i] != run[i][0]))
{
for (j = 0; run[i][j] != -1; j++)
{
moveto(j + 4, 7 + i * 5);
if (run[i][j] == -1)
printf(" ");
else
pcard(run[i][j]);
}
while (j < long_run[i])
{
moveto(j++ + 4, 7 + i * 5);
printf(" ");
}
long_run[i] = lstlen(run[i]);
base_run[i] = run[i][0];
}
}
fflush(stdout);
sleep(1);
}
thumb()
{
int i, s;
if (deck[0] == -1)
{
if (over[0] == -1)
return;
while (over[0] != -1)
put(deck, get(over));
}
for (i = 0; i < 3; i++)
if (deck[0] != -1)
put(over,get(deck));
redraw(FALSE);
while (getvalue(peek(over)) == 0)
{
s = getsuit(peek(over));
put(ace[s], get(over));
redraw(FALSE);
}
if (over[0] == -1 && deck[0] != -1)
thumb();
}
pcard(card)
{
int i;
if (i = getcolour(card))
standout();
printf("%c%c", "A23456789TJQK"[getvalue(card)], "HDSC"[getsuit(card)]);
if (i)
standend();
}
getvalue(card)
{
return(card % 13);
}
getsuit(card)
{
return(card / 13);
}
getcolour(card)
{
return(card / 26);
}
cardsleft()
{
int i, t;
t = lstlen(deck) + lstlen(over);
for (i = 0; i < 7; i++)
t += lstlen(hidden[i]);
return(t);
}
whoops()
{
moveto(17,0);
printf("\007Invalid Command: '%s'\007", cmd);
fflush(stdout);
sleep(4);
}
disphelp()
{
clear();
printf("\
Commands: t or RETURN : thumb the deck 3 cards at a time\n\
m [d1-7] [1-7a] : move cards or runs\n\
a : turn on the auto pilot (in case you get stuck)\n\
s : shuffle the deck (cheat!)\n\
p [2-7] : put a hidden pile into the deck (cheat!)\n\
d : print the cards in the deck (cheat!)\n\
w [2-7] : print the cards in a hidden pile (cheat!)\n\
h or ? : print this command summary\n\
r : print the rules of the game\n\
q : quit\n\n");
printf("\
Moving: 1-7, 'd', or 'a' select the source and destination for a move.\n\
Valid moves are from a run to a run, from the deck to a run,\n\
from a run to an ace pile, and from the deck to an ace pile.\n\n\
Cheating: Commands that allow cheating are available but they will count\n\
against you in your next life!\n\n\nHit Return -");
fflush(stdout);
getcx();
redraw(TRUE);
}
disprules()
{
clear();
printf("\
Object: The object of this game is to get all of the cards in each suit\n\
in order on the proper ace pile.\n\n\
Rules: Cards are played on the ace piles in ascending order: A,2,...,K. \n\
All aces are automatically placed in the correct aces pile as\n\
they're found in the deck or in a pile of hidden cards. Once a\n\
card is placed in an ace pile it can't be removed.\n\n");
printf("\
Cards must be played in descending order: K,Q,..,2, on the seven\n\
runs which are initially dealt. They must always be played on a\n\
card of the opposite color. Runs must always be moved as a\n\
whole, unless you're moving the lowest card on a run to the\n\
correct ace pile.\n\n");
printf("\
Whenever a whole run is moved, the top hidden card is turned\n\
over, thus becoming the beginning of a new run. If there are no\n\
hidden cards left, a space is created which can only be filled by\n\
a king.\n\n\
The rest of the deck is thumbed 3 cards at a time, until you spot a\n\
valid move. Whenever the bottom of the deck is reached, the cards\n\
are turned over and you can continue thumbing.\n\n\nHit Return -");
fflush(stdout);
getcx();
redraw(TRUE);
}
getst(getbuf, pc)
char *getbuf;
{
int ch;
int nc;
*(resgb = getbuf) = nc = 0;
for (;;)
{
if ((((ch = getcx()) >= ' ' && ch <= '~') || ch == '\t') && nc < 40)
{
*getbuf++ = ch;
*getbuf = 0;
nc++;
putchar(ch);
fflush(stdout);
}
else if (ch == '\b')
{
if (nc--)
{
*--getbuf = 0;
printf("\b \b");
fflush(stdout);
}
else
return(FALSE);
}
else if (ch == 'r' & 0x1f)
redraw(TRUE);
else if (ch == '\n')
return(TRUE);
}
}
shuffle(cards)
int *cards;
{
int i, j, k, t;
for (i = 0; cards[i] != -1; i++)
;
if (i)
{
for (j = 0; j < i; j++)
{
do
k = rnd(i);
while (k == j);
t = cards[j];
cards[j] = cards[k];
cards[k] = t;
}
}
}
rnd(n)
{
return(((rand() >> 12 | rand() >> 24) & (unsigned) ~0 >> 1) % n);
}
lstlen(list)
int *list;
{
int i;
for (i = 0; list[i] != -1; i++)
;
return(i);
}
restart()
{
setsig(TRUE);
rawmode(TRUE);
redraw(TRUE);
moveto(17, 0);
printf("cmd:%d> ", cnt);
cleol();
printf("%s", resgb);
fflush(stdout);
}
setsig(on)
{
signal(SIGINT, on ? SIG_IGN : SIG_DFL);
signal(SIGQUIT, on ? SIG_IGN : SIG_DFL);
signal(SIGTERM, on ? SIG_IGN : SIG_DFL);
signal(SIGTSTP, on ? SIG_IGN : SIG_DFL);
signal(SIGCONT, on ? restart : SIG_DFL);
}
stop()
{
setsig(FALSE);
rawmode(FALSE);
signal(SIGCONT, restart);
kill(getpid(), SIGTSTP);
}
getcx()
{
char ch;
signal(SIGTSTP, stop);
read(0, &ch, 1);
signal(SIGTSTP, SIG_IGN);
return(ch);
}
rawmode(on)
{
struct sgttyb s;
static struct sgttyb saveterm;
static int saved = FALSE;
if (on)
{
ioctl(0, TIOCGETP, &s);
if (saved == FALSE)
{
saveterm = s;
saved = TRUE;
}
ospeed = s.sg_ospeed;
s.sg_flags |= CBREAK;
s.sg_flags &= ~(ECHO|XTABS);
}
else
s = saveterm;
ioctl(0, TIOCSETN, &s);
}
getterm()
{
char termbuf[1024];
char *sp;
static char sbuf[150];
char *tgetstr();
tgetent(termbuf, getenv("TERM"));
sp = sbuf;
PC = *(tgetstr("pc", &sp));
*UP = *(tgetstr("pc", &sp));
UP[1] = 0;
*BC = (sc_bs = tgetstr("bs", &sp)) ? *sc_bs : '\b';
BC[1] = 0;
sc_init = tgetstr("ti", &sp);
sc_deinit = tgetstr("te", &sp);
sc_cleol = tgetstr("ce", &sp);
sc_clear = tgetstr("cl", &sp);
sc_move = tgetstr("cm", &sp);
sc_s_in = tgetstr("so", &sp);
sc_s_out = tgetstr("se", &sp);
}
xputc(c)
register c;
{
putchar(c);
}
init()
{
tputs(sc_init, 24, xputc);
}
deinit()
{
tputs(sc_deinit, 24, xputc);
}
clear()
{
tputs(sc_clear, 24, xputc);
}
cleol()
{
tputs(sc_cleol, 1, xputc);
}
standout()
{
tputs(sc_s_in, 1, xputc);
}
standend()
{
tputs(sc_s_out, 1, xputc);
}
moveto(y, x)
{
char *tgoto();
tputs(tgoto(sc_move, x, y), 1, xputc);
}
put(dest, item)
int *dest;
{
if (item == -1)
return;
while (*dest != -1)
dest++;
*dest++ = item;
*dest = -1;
}
get(source)
int *source;
{
int i;
for (i = 0; *source != -1; i++, source++)
;
if (i)
{
i = *--source;
*source = -1;
return(i);
}
return(-1);
}
peek(source)
int *source;
{
int i;
for (i = 0; source[i] != -1; i++)
;
return(i ? source[i - 1] : -1);
}