DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T s

⟦a840f96a5⟧ TextFile

    Length: 16202 (0x3f4a)
    Types: TextFile
    Names: »solx.c«

Derivation

└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
    └─⟦this⟧ »EUUGD18/General/Sol/solx.c« 

TextFile

#include	<stdio.h>
#include	<ctype.h>
#include	<signal.h>
#ifdef SIGVTALRM
#include	<sys/time.h>
#else
#include	<time.h>
#endif SIGVTALRM
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<sys/ioctl.h>
#ifdef TCGETA
#include	<termio.h>
#define		sgttyb	termio
#endif TCGETA

#define 	TRUE	1
#define		FALSE	0

int	amode;
int	cnt;
int	cheat;
int	deck[53];
int	ace[4][14];
int	run[7][53];
int	hidden[7][8];
char	cmd[40], *cp;
char	*resgb;

extern	char	PC;
extern	char	BC[2];
extern	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;

#ifndef TERMINFO
extern	short	ospeed;
#endif  TERMINFO

int	long_run[7];
int	long_hide[7];
int	long_ace[4];
int	base_run[7];

extern	struct tm	*localtime();
extern	char		*getenv();
extern	long		time();
extern	unsigned int	sleep();

main(n, a)
char **a;
 {
    int i, p, c, r, s;
    long now;
    struct tm *tp;
    int from;
    int split;
    int dest;
    int brkflg;

    amode = 0;
    (void) time(&now);
    tp = localtime(&now);
    srand((unsigned) (getpid() + tp->tm_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;
    for (p = 0; p < 7; p++)
      for (c = p; c < 7; c++)
	put(hidden[c],get(deck));
    put(run[0], get(hidden[0]));
    for (r = 1; r < 7; r++)
      for (i = 0; i < 4; i++)
        put(run[r],get(deck));
    redraw(TRUE);
    for (r = 0; r < 7; r++)
     {
	while (getvalue(peek(run[r])) == 0)
	 {
	    s = getsuit(peek(run[r]));
	    put(ace[s],get(run[r]));
	    if (run[r][0] == -1)
	      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(19, 0);
	    (void) printf("cmd:%d> ", cnt);
	    cleol();
	    (void) 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')
		 {
		    whoops();
		    continue;
		 }
		while (*cp == ' ' || *cp == '\t')
		  cp++;
		for (split = 0; isdigit(*cp); split = split * 10 + *cp++ - '0')
		  ;
		if (split < 1 || split > lstlen(run[from - '1']))
		 {
		    whoops();
		    continue;
		 }
		while (*cp == ' ' || *cp == '\t')
		  cp++;
		if (!(dest = *cp++) || ((dest < '1' || dest > '7') &&
			dest != 'a') || !movecard(from, split, dest, FALSE))
		 {
		    whoops();
		    continue;
		 }
		if (ordered())
		  finish();
	     }
	    break;
	    case 'h':
	    case '?':
	      disphelp();
	    break;
	    case 'r':
	      disprules();
	    break;
	    case 'q':
	      brkflg = 0;
	    break;
	    case 'w':
	     {
		while (*cp == ' ' || *cp == '\t')
		  cp++;
		if ((from = *cp - '1') < 0 || from > 6)
		 {
		    whoops();
		    continue;
		 }
		moveto(21, 0);
		(void) 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]);
		    (void) putchar(' ');
		 }
		(void) printf("\nHit return -");
		(void) fflush(stdout);
		(void) getcx();
		moveto(21, 0);
		cleol();
		(void) putchar('\n');
		cleol();
		cheat++;
	     }
	    break;
	    case 'p':
	     {
		while (*cp == ' ' || *cp == '\t')
		  cp++;
		if ((from = *cp - '1') < 0 || from > 6)
		 {
		    whoops();
		    continue;
		 }
		while (hidden[from][0] != -1)
		  put(run[from], get(hidden[from]));
		redraw(FALSE);
		cheat++;
	     }
	    break;
	    case 's':
	     {
		while (*cp == ' ' || *cp == '\t')
		  cp++;
		if ((from = *cp - '1') < 0 || from > 6)
		 {
		    whoops();
		    continue;
		 }
		shuffle(run[from]);
		long_run[from] = -1;
		redraw(FALSE);
		cheat++;
	     }
	    break;
	    default:
	      whoops();
	    break;
	 }
     }
    moveto(21, 0);
    cleol();
    (void) printf("I see you gave up\n");
    if (cheat > 0)
      (void) printf("    ... even after you cheated %d times!\n", cheat);
    else
      (void) printf("    ... but at least you didn't cheat...congratulations!\n");
    unwind();
 }

unwind()
 {
    moveto(23, 0);
    deinit();
    rawmode(FALSE);
    exit(0);
 }

movecard(from, split, dest, limitmove)
 {
    if (from == dest)
      return(FALSE);
    return(dest == 'a' ? run2ace(from) : run2run(from, split, dest, limitmove));
 }

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);
	tidy(from);
     }
    return(ok);
 }

run2run(from, split, dest, limitmove)
 {
    int fcard, dcard, i, ok;

    if ((fcard = run[from -= '1'][--split]) == -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 && split && chk2run(fcard, run[from][split - 1]))
      return(FALSE);
    if (ok = chk2run(fcard, dcard))
     {
	for (i = split; run[from][i] != -1; i++)
	  put(run[dest], run[from][i]);
	run[from][split] = -1;
	redraw(FALSE);
	tidy(from);
     }
    return(ok);
 }

tidy(from)
 {
    int s;

    while (getvalue(peek(run[from])) == 0)
     {
	s = getsuit(peek(run[from]));
	put(ace[s],get(run[from]));
	redraw(FALSE);
     }
    if (lstlen(hidden[from]) != 0 && lstlen(run[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);
     }
 }

chk2run(fcard,dcard)
 {
    if (dcard == -1 && getvalue(fcard) == 12)
      return(TRUE);
    return(getvalue(dcard) - 1 == getvalue(fcard) &&
				getcolour(dcard) != getcolour(fcard));
 }

ordered()
 {
    int i, j;

    for (i = 0; i < 7; i++)
      if (hidden[i][0] != -1)
	return(FALSE);
      else if (run[i][0] != -1)
	for (j = 1; run[i][j] != -1; j++)
	  if (getvalue(run[i][j]) != getvalue(run[i][j - 1]) - 1 ||
			getcolour(run[i][j]) == getcolour(run[i][j - 1]))
	    return(FALSE);
    return(TRUE);
 }

cardsleft()
 {
    int i;
    for (i = 0; i < 7; i++)
      if (run[i][0] != -1)
	return(TRUE);
    return(FALSE);
 }

finish()
 {
    int i;

    moveto(21, 0);
    (void) printf("\007I'll finish for you now\007");
    (void) fflush(stdout);
    do
      for (i = 0; i < 7; i++)
	while (movecard(i + '1', 0, 'a', FALSE))
	  ;
     while (cardsleft());
    moveto(21, 40);
    cleol();
    (void) printf("\007You WIN\007\n");
    if (cheat > 0)
      (void) printf("    ... but you cheated %d times!", cheat);
    else
      (void) printf("    ... and without cheating ... congratulations!");
    unwind();
 }

autopilot()
 {
    int i, j, k, l;

    moveto(21, 0);
    (void) printf("Going into automatic mode...");
    (void) fflush(stdout);
    amode = TRUE;
    do
     {
	l = 0;
	for (i = 0; i < 7; i++)
	  for (j = 0; j < 7; j++)
	    for (k = 0; k < lstlen(run[i]); k++)
	      l = movecard(i + '1', k, j + '1', TRUE) || l;
	for (i = 0; i < 7; i++)
	  l = movecard(i + '1', 0, 'a', FALSE) || l;
     } while (l);
    moveto(21, 28);
    if (!cardsleft())
     {
	(void) printf("\007YEA...\007");
	(void) fflush(stdout);
	for (i = 0; i < 7; i++)
	  if (movecard(i + '1', 0, 'a', FALSE))
	moveto(21, 34);
	(void) printf("I won!!!!!");
     }
    else
      (void) printf("I couldn't win this time");
    unwind();
 }

redraw(display)
 {
    int i, j;

    if (display)
     {
	clear();
	for (i = 0; i < 7; i++)
	 {
	    long_run[i] = long_hide[i] = base_run[i] = -1;
	    moveto(i + i + 2, 1);
	    (void) putchar(i + '1');
	 }
	moveto(0, 6);
	for (i = 1; i < 21; i++)
	  (void) printf("%3d", i);
	for (i = 0; i < 4; i++)
	  long_ace[i] = -1;
	moveto(17, 1);
	(void) printf("ACES:");
     }
    for (i = 0; i < 7; i++)
     {
	if (long_hide[i] != lstlen(hidden[i]))
	 {
	    moveto(i + i + 2, 4);
	    (void) 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(17, 8 + i * 5);
	    if (ace[i][0] != -1)
	      pcard(peek(ace[i]));
	    else
	      (void) printf("--");
	    long_ace[i] = lstlen(ace[i]);
	 }
     }
    for (i = 0; i < 7; i++)
     {
	if (long_run[i] != lstlen(run[i]) ||
				(long_run[i] == 1 && base_run[i] != run[i][0]))
	 {
	    moveto(i + i +  2, 7);
	    for (j = 0; run[i][j] != -1; j++)
	     {
		if (run[i][j] == -1)
		  (void) printf("  ");
		else
		  pcard(run[i][j]);
		(void) putchar(' ');
	     }
	    while (j++ < long_run[i])
	      (void) printf("   ");
	    long_run[i] = lstlen(run[i]);
	    base_run[i] = run[i][0];
	 }
     }
    (void) fflush(stdout);
    (void) sleep(1);
 }

pcard(card)
 {
    int i;

    if ((i = getcolour(card)) != 0)
      standout();
    (void) 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);
 }

whoops()
 {
    moveto(19, 0);
    (void) printf("\007Invalid Command: '%s'\007", cmd);
    (void) fflush(stdout);
    (void) sleep(4);
 }

disphelp()
 {
    clear();
    (void) printf("\
Commands: m [1-7] [pos] [1-7a] : move cards or runs\n\
          a                    : turn on the auto pilot\n\
          s [1-7]              : shuffle the cards in a run (cheat!)\n\
          p [1-7]              : put a hidden pile onto the run (cheat!)\n\
          w [1-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");
    (void) printf("\
Moving:   There are three parameters in the m command: the first is the\n\
          run to be moved from, the second is the card number to split\n\
          the run at (check numbers along the top), and the third is\n\
	  the destination run (or 'a' if an ace pile). Since only one card\n\
	  can be moved to an ace at a time, the pos paramter is ignored\n\
	  in this case. To give a couple of examples 'm 6 1 4 would move\n\
	  all cards from run 6 to run 4, and m 3 5 7 would split run 3\n\
	  before the fifth card (leaving 4) and move the rest to run 7.\n\n\
Cheating: Commands that allow cheating are available but they will count\n\
          against you in your next life!\n\n\nHit Return -");
    (void) fflush(stdout);
    (void) getcx();
    redraw(TRUE);
 }

disprules()
 {
    clear();
    (void) 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 uncovered in runs or from a pile of hidden cards. Once a\n\
          card is placed in an ace pile it can't be removed.\n\n");
    (void) printf("\
          When moving runs, they can be split anywhere, but the top card\n\
	  in the run moved must be placed on a card one higher (i.e. 5 on\n\
	  a 6) and always on a card of the opposite color.\n\n");
    (void) 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\nHit Return -");
    (void) fflush(stdout);
    (void) getcx();
    redraw(TRUE);
 }

getst(getbuf)
char *getbuf;
 {
    int ch;
    int nc;

    *(resgb = getbuf) = nc = 0;
    for (;;)
     {
	if ((((ch = getcx()) >= ' ' && ch <= '~') || ch == '\t') && nc < 40)
	 {
	    *getbuf++ = ch;
	    *getbuf = 0;
	    nc++;
	    (void) putchar(ch);
	    (void) fflush(stdout);
	 }
	else if (ch == '\b')
	 {
	    if (nc--)
	     {
	        *--getbuf = 0;
		(void) printf("\b \b");
		(void) 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);
 }

#ifdef SIGTSTP

restart()
 {
    setsig(TRUE);
    rawmode(TRUE);
    redraw(TRUE);
    moveto(19, 0);
    (void) printf("cmd:%d> ", cnt);
    cleol();
    (void) printf("%s", resgb);
    (void) fflush(stdout);
 }

#endif SIGTSTP

setsig(on)
 {
    (void) signal(SIGINT, on ? SIG_IGN : SIG_DFL);
    (void) signal(SIGQUIT, on ? SIG_IGN : SIG_DFL);
    (void) signal(SIGTERM, on ? SIG_IGN : SIG_DFL);
#ifdef SIGTSTP
    (void) signal(SIGTSTP, on ? SIG_IGN : SIG_DFL);
    (void) signal(SIGCONT, on ? restart : SIG_DFL);
#endif
 }

stop()
 {
    setsig(FALSE);
    rawmode(FALSE);
#ifdef SIGTSTP
    (void) signal(SIGCONT, restart);
    kill(getpid(), SIGTSTP);
#else
    (void) system("eval exec ${SHELL-/bin/sh}");
#endif SIGTSTP
 }

getcx()
 {
    char ch;

#ifdef SIGTSTP
    (void) signal(SIGTSTP, stop);
#else
    for (;;) {
#endif SIGTSTP
    (void) read(0, &ch, 1);
#ifdef SIGTSTP
    (void) signal(SIGTSTP, SIG_IGN);
#else
        if (ch != '\032')
            break;
        stop();
    }
#endif SIGTSTP
    return(ch);
 }

rawmode(on)
 {
    struct sgttyb s;
    static struct sgttyb saveterm;
    static int saved = FALSE;

    if (on)
     {
#ifdef TCGETA
     	(void) ioctl(0, TCGETA, &s);
#else
	(void) ioctl(0, TIOCGETP, &s);
#endif TCGETA
	if (saved == FALSE)
	 {
	    saveterm = s;
	    saved = TRUE;
	 }
#ifdef TCGETA
#ifndef TERMINFO
     	ospeed = s.c_cflag & CBAUD;
#endif  TERMINFO
     	s.c_lflag &= ~(ECHO|ICANON);
     	s.c_oflag &= ~TAB3;
     	s.c_cc[VMIN] = 1;
     	s.c_cc[VTIME] = 1;
#else
	ospeed = s.sg_ospeed;
	s.sg_flags |= CBREAK;
	s.sg_flags &= ~(ECHO|XTABS);
#endif TCGETA
     }
    else
      s = saveterm;
#ifdef TCGETA
    (void) ioctl(0, TCSETAW, &s);
#else
    (void) ioctl(0, TIOCSETN, &s);
#endif
 }

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;
    if (!tgetflag("bs"))
    	*BC = *(sc_bs = tgetstr("bc", &sp));
    else {
    	*BC = '\b';
    	sc_bs = BC;
    }
    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;
 {
    (void) 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);
 }