[comp.sources.games] v03i078: challenge - a card game of skill, Part02/02

games-request@tekred.TEK.COM (01/26/88)

Submitted by: "Dik T. Winter" <mcvax!cwi.nl!dik@uunet.uu.net>
Comp.sources.games: Volume 3, Issue 78
Archive-name: challenge/Part02

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 2 (of 2)."
# Contents:  Makefile alarm.c challenge.6 demo.c instructions.c
#   logging.c
# Wrapped by billr@saab on Mon Jan 25 11:17:13 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f Makefile -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"Makefile\"
else
echo shar: Extracting \"Makefile\" \(528 characters\)
sed "s/^X//" >Makefile <<'END_OF_Makefile'
X#Edit the following lines for your situation.
XDESTDIR = /usr/games
XMANDIR = /usr/games/man
XDOCDIR = /usr/games/man
X#for BSD
XLIBS = -lcurses -ltermlib
XCFLAGS = -O -DSHELL -DTIMER=1
X#for SYSV
X#LIBS = -lcurses
X#CFLAGS = -O -DSHELL -DTIMER=1 -DSYSV
XOBJS = main.o strategy.o human.o instructions.o logging.o demo.o alarm.o
X
Xchallenge:	$(OBJS)
X	cc -o challenge $(OBJS) $(LIBS)
X
Xclean:
X	rm -f $(OBJS) core
X
Xinstall:	challenge
X	mv challenge $(DESTDIR)
X	cp challenge.6 $(MANDIR)
X	cp challenge.doc $(DOCDIR)
X
X$(OBJS):        challenge.h
X
END_OF_Makefile
if test 528 -ne `wc -c <Makefile`; then
    echo shar: \"Makefile\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f alarm.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"alarm.c\"
else
echo shar: Extracting \"alarm.c\" \(718 characters\)
sed "s/^X//" >alarm.c <<'END_OF_alarm.c'
X/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1988. */
X/* Permission granted to redistribute this program. */
X
X#ifdef TIMER
X#include "challenge.h"
X
Xint do_alarm = FALSE;
X
Xset_alarm()
X{
X    seconds = 0;
X    minutes = 0;
X    do_alarm = TRUE;
X    alarm(1);
X}
X
Xend_alarm()
X{
X    do_alarm = FALSE;
X    alarm(0);
X}
X
Xtimer()
X{
X    if(do_alarm) {
X	if(demo == HUMAN) {
X	    move(COMMAND_LINE, COMMAND_POS + 17);
X	} else {
X	    move(COMMAND_LINE, COMMAND_POS + 12);
X	}
X	if((seconds += TIMER) >= 60) {
X	    minutes += seconds / 60;
X	    seconds %= 60;
X	}
X	printw("(%d:%.2d)", minutes, seconds);
X	move(COMMENT_LINE, 1);
X	clrtoeol();
X	refresh();
X#ifdef SYSV
X	signal(SIGALRM, timer);
X#endif
X	alarm(1);
X    }
X}
X#endif
X
END_OF_alarm.c
if test 718 -ne `wc -c <alarm.c`; then
    echo shar: \"alarm.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f challenge.6 -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"challenge.6\"
else
echo shar: Extracting \"challenge.6\" \(540 characters\)
sed "s/^X//" >challenge.6 <<'END_OF_challenge.6'
X.TH CHALLENGE 6 "16 January 1988"
X.UC 4
X.SH NAME
Xchallenge \- An intriguing card game.
X.SH SYNOPSIS
X.B /usr/games/challenge
X[
X.B \-d\c
X.I depth
X]
X[
X.B \-s\c
X.I size
X]
X[
X.B \-a\c
X.I file
X]
X[
X.B \-o\c
X.I options
X]
X.SH DESCRIPTION
X.PP
X.I Challenge
Xis a game played with cards.
XPlayers have full knowledge about the position of the individual
Xcards.
XA complete description is found in the accompanying document.
X.SH AUTHORS
XA. S. Steenbeek and D. T. Winter, CWI, Amsterdam.
X.SH SEE ALSO
X/use/games/man/challenge.doc
X.SH BUGS
X.PP
XVery probable.
END_OF_challenge.6
if test 540 -ne `wc -c <challenge.6`; then
    echo shar: \"challenge.6\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f demo.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"demo.c\"
else
echo shar: Extracting \"demo.c\" \(1228 characters\)
sed "s/^X//" >demo.c <<'END_OF_demo.c'
X/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1988. */
X/* Permission granted to redistribute this program. */
X
X#include "challenge.h"
X
Xdo_demo()
X{
X    int finished, player = COMPUTER;
X    int card, card1;
X
X    finished = FALSE;
X    while(!finished) {
X	move(COMMAND_LINE, COMMAND_POS);
X	clrtoeol();
X	if(player == COMPUTER) {
X	    printw("thinking....");
X	} else {
X	    printw("also thinking....");
X	}
X	move(COMMENT_LINE, 1);
X	clrtoeol();
X	refresh();
X	demo = player;
X	card = strategy(player);
X	if(card != PASS) {
X	    move(COMMAND_LINE - 1, 1);
X	    clrtoeol();
X	    if(card <= PICK_UP) {
X		finished = do_move(player, card);
X	    } else {
X		finished = TRUE;
X		break;
X	    }
X	    move(COMMAND_LINE - 1, 1);
X	    clrtoeol();
X	    if(player == COMPUTER) {
X		printw("computer");
X	    } else {
X		printw("human replacement");
X	    }
X	    if(card != PICK_UP) {
X		printw(" plays ");
X		display_full(top);
X	    } else {
X		printw(" picks up");
X	    }
X	}
X	if(!finished) {
X	    interchange(&player);
X	}
X    }
X    if(card <= PICK_UP) {
X	printw(", and wins");
X    } else {
X	printw("game is a draw");
X    }
X    if(player == COMPUTER) {
X	computer_score += 2;
X    } else {
X	human_score += 2;
X    }
X    /* NOTE: no case for WIN! */
X}
X
END_OF_demo.c
if test 1228 -ne `wc -c <demo.c`; then
    echo shar: \"demo.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f instructions.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"instructions.c\"
else
echo shar: Extracting \"instructions.c\" \(2119 characters\)
sed "s/^X//" >instructions.c <<'END_OF_instructions.c'
X/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1988. */
X/* Permission granted to redistribute this program. */
X
X#include "challenge.h"
X
Xchar *instructions[] = {
X"The display  shows at the top  the hand of the computer,  in the middle the\n",
X"cards on the table and at the bottom your hand.  On the left of the display\n",
X"of the cards on the table  the topcard is shown.   If there are no cards on\n",
X"the table, it displays '---'.\n\n",
X"If it is your turn to play you can choose any of the following:\n",
X"1. Play a card of the same suit  as the top card on the table;  but it must\n",
X"   be higher.   If there are no cards  on the table you can play  any card.\n",
X"2. If the top card is not one of your trumps, you may also play a trump.\n",
X"3. If there are cards on the table you may pick them up (all of them).\n",
X"Your trumps are hearts; the computers trumps are spades.\n",
X"At any time,  if you enter a question mark,  the program  will show you the\n",
X"cards you are allowed to play.   To play a card mark suit and value.   10's\n",
X"may be indicated by T or X.  Case is not important.\n\n",
X"Goal of the game is to play your last card.\n\n",
X0,
X"The game can  be played  with different sizes  for the deck of cards.   The\n",
X"default size is 8, i.e. 8 cards of eacht suit or Ace, King etc.  down to 7.\n",
X"If you are a novice it is better to start with a smaller size.  Do you want\n",
X"to start with size is 4 (i.e. only Aces, Kings, Queens and Jacks)? (y/n) ",
X0,
X"\n",
X"Also  the move  search  strategy  of the computer  can be simpler  than the\n",
X"default.  Do you want a very simple one? (y/n) ",
X0};
X
Xput_instructions(initial)
X    char initial;
X{
X    char **cp, c;
X
X    clear();
X    move(0, 0);
X    for(cp = instructions; *cp != 0; cp++) {
X    	printw(*cp);
X    }
X    if(!initial) {
X    	refresh();
X    	return;
X    }
X    for(cp++; *cp != 0; cp++) {
X    	printw(*cp);
X    }
X    refresh();
X    if(yesno()) {
X    	size = 4;
X    	size2 = 8;
X    	max_card = 16;
X    }
X    for(cp++; *cp != 0; cp++) {
X    	printw(*cp);
X    }
X    refresh();
X    if(yesno()) {
X    	depth = 0;
X    }
X}
X
END_OF_instructions.c
if test 2119 -ne `wc -c <instructions.c`; then
    echo shar: \"instructions.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f logging.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"logging.c\"
else
echo shar: Extracting \"logging.c\" \(6451 characters\)
sed "s/^X//" >logging.c <<'END_OF_logging.c'
X/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1988. */
X/* Permission granted to redistribute this program. */
X
X#include "challenge.h"
X
Xrecord(player, card)
X    int player, card;
X{
X    int i;
X
X    if(move_logged == 0) {
X	record_start();
X	if(start_player == HUMAN) {
X	    fprintf(logfile, "\nH:");
X	    for(i = 0; i < size2; i++) {
X		record_card(s_human[i]);
X	    }
X	    fprintf(logfile, "\nC:");
X	    for(i = 0; i < size2; i++) {
X		record_card(s_computer[i]);
X	    }
X	} else {
X	    fprintf(logfile, "\nC:");
X	    for(i = 0; i < size2; i++) {
X		record_card(s_computer[i]);
X	    }
X	    fprintf(logfile, "\nH:");
X	    for(i = 0; i < size2; i++) {
X		record_card(s_human[i]);
X	    }
X	}
X    }
X    for(i = move_logged; i < nr_moves; i++) {
X	if((i & 1) == 0) {
X	    fprintf(logfile, "\n%3d.", i / 2 + 1);
X	}
X	record_card(moves[i]);
X    }
X    move_logged = nr_moves;
X    if(((player == COMPUTER) && (nr_computer == 1)) ||
X       ((player == HUMAN ) && (nr_human == 1))) {
X	if(moves[nr_moves - 1] != PICK_UP) {
X	    fprintf(logfile, " and wins");
X	}
X    }
X}
X
Xrecord_card(card)
X    int card;
X{
X    int value, suit;
X
X    if(card == PICK_UP) {
X	fprintf(logfile, " pu");
X	return(0);
X    }
X    if(card == PASS) {
X	fprintf(logfile, " --");
X	return(0);
X    }
X    if(card == RESIGN) {
X	fprintf(logfile, " resigns");
X	return(0);
X    }
X    if(card == DRAWN) {
X	fprintf(logfile, " draw");
X	return(0);
X    }
X    if(card == WIN) {
X	fprintf(logfile, " wins");
X	return(0);
X    }
X    value = VALUE(card);
X    suit = SUIT(card);
X    fprintf(logfile, " %c%c", "AKQJX98765432"[value], "scdh"[suit]);
X}
X
Xrecord_start()
X{
X    fprintf(logfile, "*S%d,D%d*", size, depth);
X}
X
Xrecord_end()
X{
X    fprintf(logfile, "\n*E*\n");
X}
X
Xrecord_stop()
X{
X    if(move_logged != 0) {
X	fprintf(logfile, "\n*X*\n");
X    }
X    logfile = NULL;
X}
X
Xopen_log()
X{
X    if(old_logfile == NULL) {
X	logfile = fopen("challenge.log", "w");
X	old_logfile = logfile;
X    } else {
X	logfile = old_logfile;
X    }
X}
X
Xread_game()
X{
X    int c, c1, player;
X
Xagain:
X    nr_moves = 0;
X    c = fgetc(logfile);
X    if(c == EOF) {
X	move(COMMAND_LINE, 1);
X	printw("no more games on this file");
X	quit();
X    }
X    if(c != '*') {
X	invalid_file();
X    }
X    c = fgetc(logfile);
X    if(c != 'S') {
X	invalid_file();
X    }
X    c = fgetc(logfile);
X    if(c < '1' || c > '8') {
X	invalid_file();
X    }
X    size = c - '0';
X    c = fgetc(logfile);
X    if(c != ',') {
X	invalid_file();
X    }
X    c = fgetc(logfile);
X    if(c != 'D') {
X	invalid_file();
X    }
X    c = fgetc(logfile);
X    depth = 0;
X    while(c >= '0' && c <= '9') {
X	depth = depth * 10 + depth;
X	c = fgetc(logfile);
X    }
X    if(c != '*') {
X	invalid_file();
X    }
X    c = fgetc(logfile);
X    if(c != '\n') {
X	invalid_file();
X    }
X    c = fgetc(logfile);
X    if(c != 'C' && c != 'H') {
X	invalid_file();
X    }
X    c1 = fgetc(logfile);
X    if(c1 != ':') {
X	invalid_file();
X    }
X    nr_human = 0;
X    nr_computer = 0;
X    nr_table = 0;
X    top = PICK_UP;
X    if(c == 'C') {
X	read_hand(computer, &nr_computer);
X	c = fgetc(logfile);
X	if(c != 'H') {
X	    invalid_file();
X	}
X	c = fgetc(logfile);
X	if(c != ':') {
X	    invalid_file();
X	}
X	read_hand(human, &nr_human);
X	start_player = COMPUTER;
X    } else {
X	read_hand(human, &nr_human);
X	c = fgetc(logfile);
X	if(c != 'C') {
X	    invalid_file();
X	}
X	c = fgetc(logfile);
X	if(c != ':') {
X	    invalid_file();
X	}
X	read_hand(computer, &nr_computer);
X	start_player = COMPUTER;
X    }
X    player = start_player;
X    for(;;) {
X	if(fgetc(logfile) == '*') {
X	    break;
X	}
X	while((c = fgetc(logfile)) != '.') {
X	    if(c == '*') {
X		break;
X	    }
X	    if(c == '\n' || c == EOF) {
X		invalid_file();
X	    }
X	}
X	while((c = fgetc(logfile)) == ' ') {
X	    if(c == '\n' || c == EOF) {
X		invalid_file();
X	    }
X	}
X	if(c == '*') {
X	    break;
X	}
X	ungetc(c, logfile);
X	c = read_card();
X	if(c < 0) {
X	    break;
X	}
X	step(player, c);
X	interchange(&player);
X	while((c = fgetc(logfile)) == ' ') {
X	    if(c == '\n' || c == EOF) {
X		invalid_file();
X	    }
X	}
X	ungetc(c, logfile);
X	c = read_card();
X	if(c < 0) {
X	    break;
X	}
X	step(player, c);
X	interchange(&player);
X	if(fgetc(logfile) != '\n') {
X	    invalid_file();
X	}
X    }
X    c = fgetc(logfile);
X    while(fgetc(logfile) != '\n') ;
X    if(c == 'X') {
X	goto again;
X    }
X    if(c != 'E') {
X	invalid_file();
X    }
X    if(player != HUMAN) {
X	step(HUMAN, PASS);
X    }
X}
X
Xinvalid_file()
X{
X    move(COMMAND_LINE - 1, 1);
X    printw("gamefile is invalid");
X    quit();
X}
X
Xread_hand(hand, nr_hand)
X    char *hand;
X    int *nr_hand;
X{
X    char c;
X
X    *nr_hand = 0;
X    for(;;) {
X	while((c = fgetc(logfile)) == ' ') ;
X	if(c == '\n') {
X	    return;
X	}
X	ungetc(c, logfile);
X	hand[(*nr_hand)++] = read_card();
X    }
X}
X
Xread_card()
X{
X    char c;
X    int value, suit;
X
X    c = fgetc(logfile);
X    switch(c) {
X    case 'A':
X	value = ACE;
X	break;
X    case 'K':
X	value = KING;
X	break;
X    case 'Q':
X	value = QUEEN;
X	break;
X    case 'J':
X	value = JACK;
X	break;
X    case 'X':
X	value = C_10;
X	break;
X    case '9':
X	value = C_9;
X	break;
X    case '8':
X	value = C_8;
X	break;
X    case '7':
X	value = C_7;
X	break;
X    case '6':
X	value = C_6;
X	break;
X    case '5':
X	value = C_5;
X	break;
X    case '4':
X	value = C_4;
X	break;
X    case '3':
X	value = C_3;
X	break;
X    case '2':
X	value = C_2;
X	break;
X    case 'p':
X	value = PICK_UP;
X	break;
X    default:
X	while((c = fgetc(logfile)) != '\n') {
X	    if(c == EOF) {
X		invalid_file();
X	    }
X	}
X	c = fgetc(logfile);
X	if(c != '*') {
X	    invalid_file();
X	}
X	return(-1);
X    }
X    c = fgetc(logfile);
X    switch(c) {
X    case 's':
X	suit = SPADES;
X	break;
X    case 'c':
X	suit = CLUBS;
X	break;
X    case 'd':
X	suit = DIAMONDS;
X	break;
X    case 'h':
X	suit = HEARTS;
X	break;
X    case 'u':
X	if(value != PICK_UP) {
X	    invalid_file();
X	}
X	return(PICK_UP);
X    default:
X	invalid_file();
X    }
X    if(value == PICK_UP) {
X	invalid_file();
X    }
X    return(value * 4 + suit);
X}
X
Xstep(player, card)
X{
X    int i;
X
X    i = card;
X    if(i != PASS) {
X	if(i != PICK_UP) {
X	    if((i = have(player, card)) < 0) {
X		invalid_file();
X	    }
X	} else {
X	    save_pick_up();
X	}
X	if(!valid(player, i)) {
X	    invalid_file();
X	}
X	do_move(player, i);
X    }
X    moves[nr_moves++] = card;
X}
X
Xhave(player, card)
X    int player, card;
X{
X    char *deck;
X    int nr_deck, i;
X
X    if(player == COMPUTER) {
X	deck = &computer[0];
X	nr_deck = nr_computer;
X    } else {
X	deck = &human[0];
X	nr_deck = nr_human;
X    }
X    for(i = 0; i < nr_deck; i++) {
X	if(deck[i] == card) {
X	    return(i);
X	}
X    }
X    return(-1);
X}
X
END_OF_logging.c
if test 6451 -ne `wc -c <logging.c`; then
    echo shar: \"logging.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of archive 2 \(of 2\).
cp /dev/null ark2isdone
MISSING=""
for I in 1 2 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked both archives.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0