date and time created 88/10/19 19:55:09 by bostic
[unix-history] / usr / src / games / ching / cno / ching.cno.c
/*
* Copyright (c) 1988 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Guy Harris.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1988 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)ching.cno.c 5.1 (Berkeley) %G%";
#endif /* not lint */
/*
* cno - Read a question, cast a change, and output the line values to the
* standard output for processing by "phx".
*/
#include <stdio.h>
#include "ching.h"
long now; /* current time */
unsigned seed; /* seed for random number generator */
unsigned getrand();
char *change();
char string[6+1]; /* where the actual change string is put */
int table[2][2][2] = {
{ { OYIN, YYANG,}, { YYANG, YYIN,} },
{ { YYANG, YYIN,}, { YYIN, OYANG,} },
};
main()
{
FILE *logf;
time(&now);
seed = (int)now + getquest() + getgid() + getuid() + getpid(); /* randomize */
printf("%s\n", change());
if ((logf = fopen("log", "a")) != (FILE *)NULL) {
fprintf(logf, "%s\t%s", string, ctime(&now));
fclose(logf);
}
}
/*
* Hash the question by adding all the characters together.
*/
int
getquest()
{
int result;
register int c;
result = 0;
while ((c = getchar()) != EOF)
result += c;
return(result);
}
/*
* Get a set of six lines making up a change.
*/
char *
change()
{
register int i;
for (i = 0; i < 6; i++)
string[i] = table[getrnum()&01][getrnum()&01][getrnum()&01] + '0';
string[i] = '\0';
return(string);
}
/*
* Get a number more random than what getrand() gives.
*/
getrnum()
{
return((getrand())>>(getrand()%17));
}
/*
* Get a random number.
*/
unsigned
getrand()
{
return(seed = (seed*13077) + 6925);
}