|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T s
Length: 18166 (0x46f6) Types: TextFile Names: »sol.c«
└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987 └─⟦this⟧ »EUUGD18/General/Sol/sol.c«
#include <stdio.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 over[53]; int ace[4][14]; int run[7][14]; 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 extern struct tm *localtime(); extern char *getenv(); extern unsigned int sleep(); extern long time(); main(n, a) char **a; { int i, p, c, r, s; long now; struct tm *tp; int from; 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; 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); (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') && 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); (void) printf("%d cards in deck:\n", lstlen(deck) + lstlen(over)); i = lstlen(deck); while (--i != -1) { pcard(deck[i]); (void) putchar(' '); } (void) putchar('\n'); for (i = 0; over[i] != -1; i++) { pcard(over[i]); (void) putchar(' '); } (void) printf("\nHit return -"); (void) fflush(stdout); (void) getcx(); moveto(19, 0); for (i = 0; i < 4; i++) { cleol(); (void) 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'; (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(19,0); for (i = 0; i < 4; i++) { cleol(); (void) putchar('\n'); } cheat++; } break; default: whoops(); break; } } moveto(19, 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,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); (void) printf("\007I'll finish for you now\007"); (void) fflush(stdout); do { k = FALSE; for (i = 0; i < 7; i++) k = movecard(i + '1', 'a', FALSE) || k; } while (k); moveto(20,0); 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; int iter; moveto(19,0); (void) printf("Going into automatic mode..."); (void) fflush(stdout); amode = TRUE; while (cardsleft() > 0) { k = FALSE; iter = 0; 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) { (void) printf("\007YEA...\007"); (void) fflush(stdout); for (i = 0; i < 7; i++) if (movecard(i + '1', 'a', FALSE)); moveto(19, 34); (void) printf("I won!!!!!"); } else { (void) printf("I couldn't win this time"); moveto(20, 0); (void) 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; if (display) { clear(); for (i = 0; i < 7; i++) { long_run[i] = long_hide[i] = base_run[i] = -1; moveto(0, 8 + i * 5); (void) putchar(i + '1'); } for (i = 0; i < 4; i++) long_ace[i] = -1; moveto(0,56); (void) printf("ACES"); moveto(6,56); (void) printf("DECK"); } for (i = 0; i < 7; i++) { if (long_hide[i] != lstlen(hidden[i])) { moveto(2, 8 + i * 5); (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(2, 49 + i * 5); if (ace[i][0] != -1) pcard(peek(ace[i])); else (void) printf("--"); long_ace[i] = lstlen(ace[i]); } } moveto(8, 55); (void) printf(lstlen(deck) ? "## " : " "); if (lstlen(over)) pcard(peek(over)); else (void) 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) (void) printf(" "); else pcard(run[i][j]); } while (j < long_run[i]) { moveto(j++ + 4, 7 + i * 5); (void) printf(" "); } long_run[i] = lstlen(run[i]); base_run[i] = run[i][0]; } } (void) fflush(stdout); (void) 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)) != 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); } 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); (void) printf("\007Invalid Command: '%s'\007", cmd); (void) fflush(stdout); (void) sleep(4); } disphelp() { clear(); (void) 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"); (void) 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 -"); (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 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"); (void) 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"); (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\ 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 -"); (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(17, 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); }