Ported quiz game.

This commit is contained in:
Serge Vakulenko
2014-09-12 12:03:11 -07:00
parent 2ea9e02ba5
commit e7b77dadc3
6 changed files with 298 additions and 247 deletions

View File

@@ -12,8 +12,8 @@ CFLAGS += -Werror -Wall -Os
# Programs that live in subdirectories, and have makefiles of their own.
#
SUBDIR = adventure atc backgammon battlestar boggle btlgammon \
cribbage fortune hangman mille monop rogue sail trek
# TODO: quiz robots snake
cribbage fortune hangman mille monop quiz rogue sail trek
# TODO: robots snake
# C programs that live in the current directory and do not need
# explicit make lines.

1
src/games/quiz/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
quiz

View File

@@ -1,42 +1,30 @@
# @(#)Makefile 4.2 (Berkeley) 9/7/85
#
DESTDIR=
CFLAGS= -O
SEPFLAG= -i
TOPSRC = $(shell cd ../../..; pwd)
include $(TOPSRC)/target.mk
#include $(TOPSRC)/cross.mk
#CFLAGS = -O -DCROSS -Werror -Wall
quiz: quiz.c
cc ${SEPFLAG} ${CFLAGS} -o quiz quiz.c
CFLAGS += -Werror -Wall
install: quiz quiz.k
install -s quiz ${DESTDIR}/usr/games/quiz
cp -r quiz.k ${DESTDIR}/usr/games/lib
OBJS = quiz.o
MAN = quiz.0
all: quiz $(MAN)
quiz: $(OBJS)
$(CC) $(LDFLAGS) -o $@.elf $(OBJS) $(LIBS)
$(OBJDUMP) -S $@.elf > $@.dis
$(SIZE) $@.elf
$(ELF2AOUT) $@.elf $@ && rm $@.elf
$(MAN): quiz.6
nroff -man $< > $@
clean:
rm -f a.out core *.s *.o quiz
rm -f *.o core quiz *.0 *.dis
depend:
cat </dev/null >x.c
for i in quiz; do \
(echo $$i: $$i.c >>makedep; \
/bin/grep '^#[ ]*include' x.c $$i.c | sed \
-e 's,<\(.*\)>,"/usr/include/\1",' \
-e 's/:[^"]*"\([^"]*\)".*/: \1/' \
-e 's/\.c//' >>makedep); done
echo '/^# DO NOT DELETE THIS LINE/+2,$$d' >eddep
echo '$$r makedep' >>eddep
echo 'w' >>eddep
cp Makefile Makefile.bak
ed - Makefile < eddep
rm eddep makedep x.c
echo '# DEPENDENCIES MUST END AT END OF FILE' >> Makefile
echo '# IF YOU PUT STUFF HERE IT WILL GO AWAY' >> Makefile
echo '# see make depend above' >> Makefile
# DO NOT DELETE THIS LINE -- make depend uses it
quiz: quiz.c
quiz: /usr/include/stdio.h
quiz: /usr/include/signal.h
# DEPENDENCIES MUST END AT END OF FILE
# IF YOU PUT STUFF HERE IT WILL GO AWAY
# see make depend above
install: all
install quiz $(DESTDIR)/games/
install -m 644 $(MAN) $(DESTDIR)/share/man/cat6/
cp -r quiz.k ${DESTDIR}/games/lib

View File

@@ -1,8 +1,10 @@
static char sccsid[] = " quiz.c 4.2 85/01/09 ";
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define NF 10
#define NL 300
#define NC 200
@@ -24,8 +26,11 @@ int nc = 0;
char line[150];
char response[100];
char *tmp[NF];
int select[NF];
int choice[NF];
int string(int s);
int
readline()
{
char *t;
@@ -57,18 +62,34 @@ loop:
char *eu;
char *ev;
cmp(u,v)
char *u,*v;
int
fold(c)
char c;
{
int x;
eu = u;
ev = v;
x = disj(1);
if(x!=1)
return(x);
return(eat(1,0));
if(c<'A'||c>'Z')
return(c);
return(c|040);
}
int
eat(s,c)
char c;
{
if(*ev!=c)
return(2);
if(s==0) {
ev++;
return(1);
}
if(fold(*eu)!=fold(c))
return(0);
eu++;
ev++;
return(1);
}
int
disj(s)
{
int t, x;
@@ -83,7 +104,7 @@ disj(s)
case 0:
case ']':
case '}':
return(t|x&s);
return t | (x & s);
case '|':
ev++;
t |= s;
@@ -108,6 +129,7 @@ disj(s)
}
}
int
string(s)
{
int x;
@@ -152,44 +174,26 @@ string(s)
}
}
eat(s,c)
char c;
int
cmp(u,v)
char *u,*v;
{
if(*ev!=c)
return(2);
if(s==0) {
ev++;
return(1);
}
if(fold(*eu)!=fold(c))
return(0);
eu++;
ev++;
return(1);
}
fold(c)
char c;
{
if(c<'A'||c>'Z')
return(c);
return(c|040);
}
publish(t)
char *t;
{
ev = t;
pub1(1);
int x;
eu = u;
ev = v;
x = disj(1);
if(x!=1)
return(x);
return(eat(1,0));
}
void
pub1(s)
{
for(;;ev++){
switch(*ev) {
case '|':
s = 0;
ev;
continue;
case ']':
case '}':
@@ -199,7 +203,6 @@ pub1(s)
case '{':
ev++;
pub1(s);
ev;
continue;
case '\\':
if(*++ev=='\n')
@@ -211,6 +214,15 @@ pub1(s)
}
}
void
publish(t)
char *t;
{
ev = t;
pub1(1);
}
int
segment(u,w)
char *u, *w[];
{
@@ -240,8 +252,15 @@ char *u, *w[];
}
}
printf("Too many facts about one thing\n");
return 0;
}
void
badinfo(){
printf("Bad info %s\n",line);
}
int
perm(u,m,v,n,p)
int p[];
char *u[], *v[];
@@ -263,18 +282,20 @@ uloop: ;
return(1);
}
int
find(u,m)
char *u[];
{
int n;
while(readline()){
n = segment(line,tmp);
if(perm(u,m,tmp+1,n-1,select))
if(perm(u,m,tmp+1,n-1,choice))
return(1);
}
return(0);
}
void
readindex()
{
xx[0] = nc = 0;
@@ -288,139 +309,17 @@ readindex()
}
}
void
talloc()
{
int i;
char *malloc();
for(i=0;i<NF;i++)
tmp[i] = malloc(SL);
}
main(argc,argv)
char *argv[];
{
register j;
int i;
int x;
int z;
char *info;
int tvec[2];
char *t;
extern done();
int count;
info = "/usr/games/lib/quiz.k/index";
time(tvec);
inc = tvec[1]&077774|01;
loop:
if(argc>1&&*argv[1]=='-') {
switch(argv[1][1]) {
case 'i':
if(argc>2)
info = argv[2];
argc -= 2;
argv += 2;
goto loop;
case 't':
tflag = 1;
argc--;
argv++;
goto loop;
}
}
input = fopen(info,"r");
if(input==NULL) {
printf("No info\n");
exit(0);
}
talloc();
if(argc<=2)
instruct(info);
signal(SIGINT,done);
argv[argc] = 0;
if(find(&argv[1],argc-1)==0)
dunno();
fclose(input);
input = fopen(tmp[0],"r");
if(input==NULL)
dunno();
readindex();
if(!tflag || na>nl)
na = nl;
stdout->_flag |= _IONBF;
for(;;) {
i = next();
fseek(input,xx[i]+0L,0);
z = xx[i+1]-xx[i];
for(j=0;j<z;j++)
line[j] = getc(input);
segment(line,tmp);
if(*tmp[select[0]] == '\0' || *tmp[select[1]] == '\0') {
score[i] = 1;
continue;
}
publish(tmp[select[0]]);
printf("\n");
for(count=0;;count++) {
if(query(response)==0) {
publish(tmp[select[1]]);
printf("\n");
if(count==0) wrongs++;
score[i] = tflag?-1:1;
break;
}
x = cmp(response,tmp[select[1]]);
if(x>1) badinfo();
if(x==1) {
printf("Right!\n");
if(count==0) rights++;
if(++score[i]>=1 && na<nl)
na++;
break;
}
printf("What?\n");
if(count==0) wrongs++;
score[i] = tflag?-1:1;
}
guesses += count;
}
}
query(r)
char *r;
{
char *t;
for(t=r;;t++) {
if(read(0,t,1)==0)
done();
if(*t==' '&&(t==r||t[-1]==' '))
t--;
if(*t=='\n') {
while(t>r&&t[-1]==' ')
*--t = '\n';
break;
}
}
*t = 0;
return(t-r);
}
next()
{
int flag;
inc = inc*3125&077777;
ptr = (inc>>2)%na;
flag = 0;
while(score[ptr]>0)
if(++ptr>=na) {
ptr = 0;
if(flag) done();
flag = 1;
}
return(ptr);
}
done()
void
done(int sig)
{
printf("\nRights %d, wrongs %d, ", rights, wrongs);
if(guesses)
@@ -428,9 +327,10 @@ done()
printf("score %d%%\n",100*rights/(rights+wrongs));
exit(0);
}
instruct(info)
void
instruct(char *info)
{
char *t;
int i, n;
printf("Subjects:\n\n");
while(readline()) {
@@ -466,12 +366,141 @@ instruct(info)
exit(0);
}
badinfo(){
printf("Bad info %s\n",line);
}
void
dunno()
{
printf("I don't know about that\n");
exit(0);
}
int
next()
{
int flag;
inc = inc*3125&077777;
ptr = (inc>>2)%na;
flag = 0;
while(score[ptr]>0)
if(++ptr>=na) {
ptr = 0;
if(flag) done(0);
flag = 1;
}
return(ptr);
}
int
query(r)
char *r;
{
char *t;
for(t=r;;t++) {
if(read(0,t,1)==0)
done(0);
if(*t==' '&&(t==r||t[-1]==' '))
t--;
if(*t=='\n') {
while(t>r&&t[-1]==' ')
*--t = '\n';
break;
}
}
*t = 0;
return(t-r);
}
int
main(argc,argv)
char *argv[];
{
int j, i, x, z, count;
char *indexfile, *p;
time_t tvec;
char inputfile[100];
#ifdef CROSS
indexfile = "/usr/local/games/quiz.k/index";
#else
indexfile = "/games/lib/quiz.k/index";
#endif
time(&tvec);
inc = (tvec & 077774) | 01;
loop:
if(argc>1&&*argv[1]=='-') {
switch(argv[1][1]) {
case 'i':
if(argc>2)
indexfile = argv[2];
argc -= 2;
argv += 2;
goto loop;
case 't':
tflag = 1;
argc--;
argv++;
goto loop;
}
}
input = fopen(indexfile,"r");
if(input==NULL) {
printf("No indexfile\n");
exit(0);
}
talloc();
if(argc<=2)
instruct(indexfile);
signal(SIGINT,done);
argv[argc] = 0;
if(find(&argv[1],argc-1)==0)
dunno();
fclose(input);
strcpy(inputfile, indexfile);
p = strrchr(inputfile, '/');
if (! p)
dunno();
strcpy(p+1, tmp[0]);
input = fopen(inputfile,"r");
if(input==NULL)
dunno();
readindex();
if(!tflag || na>nl)
na = nl;
setbuf(stdout, NULL);
for(;;) {
i = next();
fseek(input,xx[i]+0L,0);
z = xx[i+1]-xx[i];
for(j=0;j<z;j++)
line[j] = getc(input);
segment(line,tmp);
if(*tmp[choice[0]] == '\0' || *tmp[choice[1]] == '\0') {
score[i] = 1;
continue;
}
publish(tmp[choice[0]]);
printf("\n");
for(count=0;;count++) {
if(query(response)==0) {
publish(tmp[choice[1]]);
printf("\n");
if(count==0) wrongs++;
score[i] = tflag?-1:1;
break;
}
x = cmp(response,tmp[choice[1]]);
if(x>1) badinfo();
if(x==1) {
printf("Right!\n");
if(count==0) rights++;
if(++score[i]>=1 && na<nl)
na++;
break;
}
printf("What?\n");
if(count==0) wrongs++;
score[i] = tflag?-1:1;
}
guesses += count;
}
}

View File

@@ -1,30 +1,30 @@
/usr/games/lib/quiz.k/state:state:cap{ital}:abbr{ev{iation}}:flower
/usr/games/lib/quiz.k/america:America{n}:cap{ital}
/usr/games/lib/quiz.k/europe:Europe{an}:cap{ital}
/usr/games/lib/quiz.k/africa:Africa{n}:cap{ital}
/usr/games/lib/quiz.k/pres:pres{ident}:term:succ{essor}
/usr/games/lib/quiz.k/sov:sov{ereign}:cen{t{ury}}:succ{essor}
/usr/games/lib/quiz.k/seq-easy:easy{-}{seq{uence}}:next:name
/usr/games/lib/quiz.k/seq-hard:hard{-}{seq{uence}}:next:name
/usr/games/lib/quiz.k/posneg:pos{itive}:neg{ative}
/usr/games/lib/quiz.k/bard:Shakespeare{{-}line{s}}|line{s}:next:work:char{acter}
/usr/games/lib/quiz.k/murders:victim:killer
/usr/games/lib/quiz.k/collectives:ind{ividuals}:coll{ective}
/usr/games/lib/quiz.k/trek:star:trek
/usr/games/lib/quiz.k/babies:baby:adult
/usr/games/lib/quiz.k/poetry:poem{-}line:next:poem:author
/usr/games/lib/quiz.k/midearth:M[iddle{-}Earth|E]:cap{ital}
/usr/games/lib/quiz.k/inca:inca:succ{essor}
/usr/games/lib/quiz.k/morse:clear|alpha{bet{ic}}:morse
/usr/games/lib/quiz.k/sexes:male:female
/usr/games/lib/quiz.k/greek:greek:english
/usr/games/lib/quiz.k/latin:latin:english
/usr/games/lib/quiz.k/locomotive:locomotive:name
/usr/games/lib/quiz.k/ucc:section:ucc
/usr/games/lib/quiz.k/chinese:year:next
/usr/games/lib/quiz.k/elements:symbol:number:weight:element
/usr/games/lib/quiz.k/province:province:cap{ital}
/usr/games/lib/quiz.k/asia:Asia{n}:cap{ital}
/usr/games/lib/quiz.k/arith:arith{metic}:ans{wer}
/usr/games/lib/quiz.k/areas:area{-code}:state{-region}:city
/usr/games/lib/quiz.k/ed:function:ed-command
state:state:cap{ital}:abbr{ev{iation}}:flower
america:America{n}:cap{ital}
europe:Europe{an}:cap{ital}
africa:Africa{n}:cap{ital}
pres:pres{ident}:term:succ{essor}
sov:sov{ereign}:cen{t{ury}}:succ{essor}
seq-easy:easy{-}{seq{uence}}:next:name
seq-hard:hard{-}{seq{uence}}:next:name
posneg:pos{itive}:neg{ative}
bard:Shakespeare{{-}line{s}}|line{s}:next:work:char{acter}
murders:victim:killer
collectives:ind{ividuals}:coll{ective}
trek:star:trek
babies:baby:adult
poetry:poem{-}line:next:poem:author
midearth:M[iddle{-}Earth|E]:cap{ital}
inca:inca:succ{essor}
morse:clear|alpha{bet{ic}}:morse
sexes:male:female
greek:greek:english
latin:latin:english
locomotive:locomotive:name
ucc:section:ucc
chinese:year:next
elements:symbol:number:weight:element
province:province:cap{ital}
asia:Asia{n}:cap{ital}
arith:arith{metic}:ans{wer}
areas:area{-code}:state{-region}:city
ed:function:ed-command