🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.sources.games
Article #62

bassomatic :-)

#62
From: Arthur David Olson
Date: Tue, 19 Mar 1985 23:07
150 lines
3508 bytes
#include "stdio.h"

#ifndef BUFSIZ
#define BUFSIZ		1024
#endif

#define RANKCOUNT	13

char	leader[] = 	"A234567891JQK";
int	ihave[RANKCOUNT];
int	uhave[RANKCOUNT];

char *	begins(string, beginning)
char *	string;
char *	beginning;
{
	while (*beginning != '\0')
		if (*string++ != *beginning++)
			return 0;
	return string;
}

main(argc, argv)
int	argc;
char *	argv[];
{
	char *	cp;
	int	i;
	int	result;
	int	didpro;
	int	itoufids[2];
	int	utoifids[2];
	char	buf[BUFSIZ];
	char	outbuf[4];

	if (pipe(itoufids) != 0 || pipe(utoifids) != 0)
		return 2;
	switch (fork()) {
		case 0:	/* child */
			if (close(0) == 0 && close(1) == 0 &&
				dup(itoufids[0]) == 0 && dup(utoifids[1]) == 1)
					execl("/usr/games/fish", "fish", 0);
		case -1:
			return 2;
	}
	close(utoifids[1]);
	didpro = 0;
	while ((i = getit(buf, utoifids[0], itoufids[1])) > 0) {
		if (argc == 1)
			printf("%s", buf);
		if (begins(buf, "instructions?")) {
			write(itoufids[1], "n\n", 2);
			if (argc == 1)
				printf("n\n");
		}
		if (cp = begins(buf, "your hand is: ")) {
			for (i = 0; i < RANKCOUNT; ++i)
				ihave[i] = 0;
			for ( ; *cp != '+' && *cp != '\0'; ++cp)
				for (i = 0; i < RANKCOUNT; ++i)
					if (*cp == leader[i])
						ihave[i] = 1;
		}
		if (begins(buf + 1, "ou ask me for: ")) {
			cp = outbuf;
			if (didpro == 0) {
				*cp++ = 'p';
				didpro = 1;
			} else if ((i = which()) >= 0) {
				uhave[i] = 0;
				if ((*cp++ = leader[i]) == '1')
					*cp++ = '0';
			}
			*cp++ = '\n';
			*cp = '\0';
			write(itoufids[1], outbuf, cp - outbuf);
			if (argc == 1)
				printf(outbuf);
		}
		if (cp = begins(buf, "I ask you for: "))
			for (i = 0; i < RANKCOUNT; ++i)
				if (*cp == leader[i])
					uhave[i] = 1;
		if (cp = begins(buf, "Your books: "))
			result = (strlen(cp) - 1) / 2;
	}
	if (i < 0)
		return 2;
	if (argc != 1)
		printf("%d\n", result);
	return result <= RANKCOUNT / 2;
}

int	lasti;

int	which()
{
	int	i;

	for (i = 0; i < RANKCOUNT; ++i)
		if (ihave[i] && uhave[i])
			return lasti = i;
	for (i = 0; i < RANKCOUNT; ++i)
		if (ihave[lasti = (lasti + 1) % RANKCOUNT])
			return lasti;
	return -1;
}

int	unread;
char	mybuf[BUFSIZ + 1];
char *	saveptr = mybuf;

#define WHATLEN	strlen("what?\n")

int	getit(buf, in, out)
char *	buf;
{
	char *	cp;
	int	i;

	cp = buf;
	for ( ; ; ) {
		if (*saveptr == '\0') {
			unread = unread - WHATLEN;
			i = 0;
			while (unread <= BUFSIZ) {
				mybuf[i++] = 'n';
				mybuf[i++] = '\n';
				unread = unread + WHATLEN;
			}
			write(out, mybuf, i);
			i = read(in, mybuf, BUFSIZ);
			if (i <= 0)
				return i;
			mybuf[i] = '\0';
			saveptr = mybuf;
		}
		if ((*cp++ = *saveptr++) == '\n') {
			*cp = '\0';
			if (strcmp(buf, "what?\n") != 0)
				return cp - buf;
			unread = unread - WHATLEN;
			cp = buf;
			continue;
		}
		*cp = '\0';
		if (begins(buf + 1, "ou ask me for: "))
			return cp - buf;
	}
}
Technical Details
Message-ID: <5077@elsie.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!decvax!genrad!panda!talcott!harvard!seismo!elsie!ado

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/17/84; site elsie.UUCP
Path: arc1-ca.pugleaf.net!utzoo!decvax!genrad!panda!talcott!harvard!seismo!elsie!ado
From: ado@elsie.UUCP (Arthur David Olson)
Newsgroups: net.sources.games
Subject: bassomatic :-)
Message-ID: <5077@elsie.UUCP>
Date: Tue, 19-Mar-85 18:07:52 EST
Article-I.D.: elsie.5077
Posted: Tue Mar 19 18:07:52 1985
Date-Received: Wed, 20-Mar-85 06:46:20 EST
Distribution: na
Organization: NIH-LEC, Bethesda, MD
Lines: 150