From e7b77dadc3fa0eb9c8d450e145517d19acff3aa3 Mon Sep 17 00:00:00 2001 From: Serge Vakulenko Date: Fri, 12 Sep 2014 12:03:11 -0700 Subject: [PATCH] Ported quiz game. --- rootfs.manifest | 41 +++- src/games/Makefile | 4 +- src/games/quiz/.gitignore | 1 + src/games/quiz/Makefile | 58 +++--- src/games/quiz/quiz.c | 381 +++++++++++++++++++----------------- src/games/quiz/quiz.k/index | 60 +++--- 6 files changed, 298 insertions(+), 247 deletions(-) create mode 100644 src/games/quiz/.gitignore diff --git a/rootfs.manifest b/rootfs.manifest index 4faefef..98240dc 100644 --- a/rootfs.manifest +++ b/rootfs.manifest @@ -425,6 +425,7 @@ file /games/morse file /games/number file /games/ppt file /games/primes +file /games/quiz file /games/rain file /games/rogue file /games/sail @@ -437,17 +438,48 @@ file /games/wump # # Files: /games/lib # +default +filemode 0444 dir /games/lib file /games/lib/adventure.dat -mode 0444 file /games/lib/battle_strings -mode 0444 file /games/lib/cfscores mode 0666 file /games/lib/fortunes.dat -mode 0444 file /games/lib/cards.pck -mode 0444 +dir /games/lib/quiz.k +file /games/lib/quiz.k/africa +file /games/lib/quiz.k/america +file /games/lib/quiz.k/areas +file /games/lib/quiz.k/arith +file /games/lib/quiz.k/asia +file /games/lib/quiz.k/babies +file /games/lib/quiz.k/bard +file /games/lib/quiz.k/chinese +file /games/lib/quiz.k/collectives +file /games/lib/quiz.k/ed +file /games/lib/quiz.k/elements +file /games/lib/quiz.k/europe +file /games/lib/quiz.k/greek +file /games/lib/quiz.k/inca +file /games/lib/quiz.k/index +file /games/lib/quiz.k/latin +file /games/lib/quiz.k/locomotive +file /games/lib/quiz.k/midearth +file /games/lib/quiz.k/morse +file /games/lib/quiz.k/murders +file /games/lib/quiz.k/poetry +file /games/lib/quiz.k/posneg +file /games/lib/quiz.k/pres +file /games/lib/quiz.k/province +file /games/lib/quiz.k/seq-easy +file /games/lib/quiz.k/seq-hard +file /games/lib/quiz.k/sexes +file /games/lib/quiz.k/sov +file /games/lib/quiz.k/spell +file /games/lib/quiz.k/state +file /games/lib/quiz.k/trek +file /games/lib/quiz.k/ucc # # Files: /include @@ -738,6 +770,7 @@ file /share/man/cat6/hangman.0 file /share/man/cat6/mille.0 file /share/man/cat6/monop.0 file /share/man/cat6/number.0 +file /share/man/cat6/quiz.0 file /share/man/cat6/rain.0 file /share/man/cat6/rogue.0 file /share/man/cat6/sail.0 diff --git a/src/games/Makefile b/src/games/Makefile index e032637..da2207f 100644 --- a/src/games/Makefile +++ b/src/games/Makefile @@ -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. diff --git a/src/games/quiz/.gitignore b/src/games/quiz/.gitignore new file mode 100644 index 0000000..fced154 --- /dev/null +++ b/src/games/quiz/.gitignore @@ -0,0 +1 @@ +quiz diff --git a/src/games/quiz/Makefile b/src/games/quiz/Makefile index c314721..4b3c2c3 100644 --- a/src/games/quiz/Makefile +++ b/src/games/quiz/Makefile @@ -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 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 diff --git a/src/games/quiz/quiz.c b/src/games/quiz/quiz.c index 51da296..6e910d3 100644 --- a/src/games/quiz/quiz.c +++ b/src/games/quiz/quiz.c @@ -1,8 +1,10 @@ - -static char sccsid[] = " quiz.c 4.2 85/01/09 "; - #include #include +#include +#include +#include +#include + #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;i1&&*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;j1) badinfo(); - if(x==1) { - printf("Right!\n"); - if(count==0) rights++; - if(++score[i]>=1 && nar&&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;j1) badinfo(); + if(x==1) { + printf("Right!\n"); + if(count==0) rights++; + if(++score[i]>=1 && na