[net.sources.games] AI riddle program

bright@dataioDataio.UUCP (Walter Bright) (04/01/86)

The following program is a demonstration of AI techniques disguised
as a game. Cut, compile and run.

Note that this program may be made even more intelligent if it is written
in an AI language such as PROLOG or LISP.

--------------- Cut (ouch) here ---------------------
/*_ riddle.c   Tue Apr  1 1986   Written by: Walter Bright */

#include <stdio.h>
#include <signal.h>

main()
{	extern long time();
	static char *msg[] = { "ha ","hah ","ho ","hee " };
	int i,cntrlC();

	printf("Welcome to riddle, an AI demonstration program.\n");
	printf("riddle will attempt to solve one line riddles.\n");
	printf("\nInitializing...\n");
	delay(2);
	srand(time((long *) NULL));
	printf("Tell me a riddle (one line): ");
	while (1)
	{
		fflush(stdout);
		getline();
		signal(SIGINT,cntrlC);
		delay(rand() % 3 + 1);
		printf("Hmmmmm.....\n");
		delay(rand() % 10 + 3);
		printf("Gee, that's a tough one.\n");
		delay(rand() % 10 + 4);
		signal(SIGINT,SIG_DFL);
		printf("I give up. What's the answer? ");
		fflush(stdout);
		getline();
		printf("hah ");
		i = rand() % 15 + 5;
		while (i--)
		{	printf(msg[rand() % (sizeof(msg)/sizeof(msg[0]))]);
			fflush(stdout);
			delay(1);
		}
		printf("\nThat was a good one (sniff).\n");
		printf("Tell me another one: ");
	}
}

getline()
{	char buffer[100];

		fgets(buffer,sizeof(buffer),stdin);
}

delay(secs)
{
#ifdef DLC
	long starttime;

	time(&starttime);
	while (time((long *) NULL) <= starttime + secs)
		;
#else
	sleep(secs);
#endif
}

cntrlC()
{	static char *msg[] =
	{ "Hang on a sec, I've almost got it.",
	  "I said just a sec.",
	  "Give me a chance, will ya? The machine's a little slow today.",
	  "Buzz off, I'm artificially thinking.",
	  "Yer mother wears army boots",
	  "Row well, and live",
	  ""
	};
	static int i = 0;

	printf("%s\n",msg[i]);
	if (i < 5) i++;
}