xref: /csrg-svn/games/ching/cno/ching.cno.c (revision 60769)
135940Sbostic /*
2*60769Sbostic  * Copyright (c) 1988, 1993
3*60769Sbostic  *	The Regents of the University of California.  All rights reserved.
435940Sbostic  *
535940Sbostic  * This code is derived from software contributed to Berkeley by
635940Sbostic  * Guy Harris.
735940Sbostic  *
842572Sbostic  * %sccs.include.redist.c%
935940Sbostic  */
1035940Sbostic 
1135940Sbostic #ifndef lint
12*60769Sbostic static char copyright[] =
13*60769Sbostic "@(#) Copyright (c) 1988, 1993\n\
14*60769Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1535940Sbostic #endif /* not lint */
1635940Sbostic 
1735940Sbostic #ifndef lint
18*60769Sbostic static char sccsid[] = "@(#)ching.cno.c	8.1 (Berkeley) 05/31/93";
1935940Sbostic #endif /* not lint */
2035940Sbostic 
2135940Sbostic /*
2235940Sbostic  * cno - Read a question, cast a change, and output the line values to the
2335940Sbostic  * standard output for processing by "phx".
2435940Sbostic  */
2535940Sbostic #include <stdio.h>
2635940Sbostic #include "ching.h"
2735940Sbostic 
2835940Sbostic long	now;		/* current time */
2935940Sbostic 
3035940Sbostic unsigned seed;		/* seed for random number generator */
3135940Sbostic unsigned getrand();
3235940Sbostic 
3335940Sbostic char	*change();
3435940Sbostic char	string[6+1];	/* where the actual change string is put */
3535940Sbostic 
3635940Sbostic int	table[2][2][2] = {
3735940Sbostic 	{ { OYIN,  YYANG,}, { YYANG, YYIN,} },
3835940Sbostic 	{ { YYANG, YYIN,},  { YYIN,  OYANG,} },
3935940Sbostic };
4035940Sbostic 
main()4135940Sbostic main()
4235940Sbostic {
4335940Sbostic 	FILE *logf;
4435940Sbostic 
4535940Sbostic 	time(&now);
4635940Sbostic 	seed = (int)now + getquest() + getgid() + getuid() + getpid();	/* randomize */
4735940Sbostic 	printf("%s\n", change());
4835940Sbostic }
4935940Sbostic 
5035940Sbostic /*
5135940Sbostic  * Hash the question by adding all the characters together.
5235940Sbostic  */
5335940Sbostic int
getquest()5435940Sbostic getquest()
5535940Sbostic {
5635940Sbostic 	int result;
5735940Sbostic 	register int c;
5835940Sbostic 
5935940Sbostic 	result = 0;
6035940Sbostic 	while ((c = getchar()) != EOF)
6135940Sbostic 		result += c;
6235940Sbostic 	return(result);
6335940Sbostic }
6435940Sbostic 
6535940Sbostic /*
6635940Sbostic  * Get a set of six lines making up a change.
6735940Sbostic  */
6835940Sbostic char *
change()6935940Sbostic change()
7035940Sbostic {
7135940Sbostic 	register int i;
7235940Sbostic 
7335940Sbostic 	for (i = 0; i < 6; i++)
7435940Sbostic 		string[i] = table[getrnum()&01][getrnum()&01][getrnum()&01] + '0';
7535940Sbostic 	string[i] = '\0';
7635940Sbostic 	return(string);
7735940Sbostic }
7835940Sbostic 
7935940Sbostic /*
8035940Sbostic  * Get a number more random than what getrand() gives.
8135940Sbostic  */
getrnum()8235940Sbostic getrnum()
8335940Sbostic {
8435940Sbostic 	return((getrand())>>(getrand()%17));
8535940Sbostic }
8635940Sbostic 
8735940Sbostic /*
8835940Sbostic  * Get a random number.
8935940Sbostic  */
9035940Sbostic unsigned
getrand()9135940Sbostic getrand()
9235940Sbostic {
9335940Sbostic 	return(seed = (seed*13077) + 6925);
9435940Sbostic }
95