From 09974c6fd8e5c0d3afa4cc9534679b4a9990171c Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Wed, 25 Oct 2017 00:36:16 +0100 Subject: [PATCH] games add hangman and quiz --- Applications/V7/games/Makefile.z80 | 2 +- Applications/V7/games/hangman.c | 165 + Applications/V7/games/quiz.c | 483 + Applications/V7/games/quiz.k/africa | 44 + Applications/V7/games/quiz.k/america | 27 + Applications/V7/games/quiz.k/areas | 124 + Applications/V7/games/quiz.k/arith | 45 + Applications/V7/games/quiz.k/asia | 41 + Applications/V7/games/quiz.k/babies | 21 + Applications/V7/games/quiz.k/bard | 228 + Applications/V7/games/quiz.k/chinese | 12 + Applications/V7/games/quiz.k/collectives | 88 + Applications/V7/games/quiz.k/ed | 84 + Applications/V7/games/quiz.k/elements | 103 + Applications/V7/games/quiz.k/europe | 33 + Applications/V7/games/quiz.k/greek | 7 + Applications/V7/games/quiz.k/inca | 12 + Applications/V7/games/quiz.k/index | 31 + Applications/V7/games/quiz.k/latin | 157 + Applications/V7/games/quiz.k/locomotive | 11 + Applications/V7/games/quiz.k/midearth | 10 + Applications/V7/games/quiz.k/morse | 26 + Applications/V7/games/quiz.k/murders | 25 + Applications/V7/games/quiz.k/poetry | 184 + Applications/V7/games/quiz.k/posneg | 50 + Applications/V7/games/quiz.k/pres | 38 + Applications/V7/games/quiz.k/province | 13 + Applications/V7/games/quiz.k/seq-easy | 14 + Applications/V7/games/quiz.k/seq-hard | 15 + Applications/V7/games/quiz.k/sexes | 26 + Applications/V7/games/quiz.k/sov | 42 + Applications/V7/games/quiz.k/spell | 2 + Applications/V7/games/quiz.k/state | 50 + Applications/V7/games/quiz.k/trek | 19 + Applications/V7/games/quiz.k/ucc | 127 + Applications/V7/games/words | 24001 +++++++++++++++++++++ 36 files changed, 26359 insertions(+), 1 deletion(-) create mode 100644 Applications/V7/games/hangman.c create mode 100644 Applications/V7/games/quiz.c create mode 100644 Applications/V7/games/quiz.k/africa create mode 100644 Applications/V7/games/quiz.k/america create mode 100644 Applications/V7/games/quiz.k/areas create mode 100644 Applications/V7/games/quiz.k/arith create mode 100644 Applications/V7/games/quiz.k/asia create mode 100644 Applications/V7/games/quiz.k/babies create mode 100644 Applications/V7/games/quiz.k/bard create mode 100644 Applications/V7/games/quiz.k/chinese create mode 100644 Applications/V7/games/quiz.k/collectives create mode 100644 Applications/V7/games/quiz.k/ed create mode 100644 Applications/V7/games/quiz.k/elements create mode 100644 Applications/V7/games/quiz.k/europe create mode 100644 Applications/V7/games/quiz.k/greek create mode 100644 Applications/V7/games/quiz.k/inca create mode 100644 Applications/V7/games/quiz.k/index create mode 100644 Applications/V7/games/quiz.k/latin create mode 100644 Applications/V7/games/quiz.k/locomotive create mode 100644 Applications/V7/games/quiz.k/midearth create mode 100644 Applications/V7/games/quiz.k/morse create mode 100644 Applications/V7/games/quiz.k/murders create mode 100644 Applications/V7/games/quiz.k/poetry create mode 100644 Applications/V7/games/quiz.k/posneg create mode 100644 Applications/V7/games/quiz.k/pres create mode 100644 Applications/V7/games/quiz.k/province create mode 100644 Applications/V7/games/quiz.k/seq-easy create mode 100644 Applications/V7/games/quiz.k/seq-hard create mode 100644 Applications/V7/games/quiz.k/sexes create mode 100644 Applications/V7/games/quiz.k/sov create mode 100644 Applications/V7/games/quiz.k/spell create mode 100644 Applications/V7/games/quiz.k/state create mode 100644 Applications/V7/games/quiz.k/trek create mode 100644 Applications/V7/games/quiz.k/ucc create mode 100644 Applications/V7/games/words diff --git a/Applications/V7/games/Makefile.z80 b/Applications/V7/games/Makefile.z80 index 43520aa9..fa19f1c8 100644 --- a/Applications/V7/games/Makefile.z80 +++ b/Applications/V7/games/Makefile.z80 @@ -11,7 +11,7 @@ PROGLOAD=`(cat ../../../Kernel/platform/config.h; echo PROGLOAD) | cpp -E | tail .SUFFIXES: .c .rel -SRCS = arithmetic.c backgammon.c fish.c wump.c +SRCS = arithmetic.c backgammon.c fish.c hangman.c quiz.c wump.c SRCSBAD = diff --git a/Applications/V7/games/hangman.c b/Applications/V7/games/hangman.c new file mode 100644 index 00000000..243c9d7e --- /dev/null +++ b/Applications/V7/games/hangman.c @@ -0,0 +1,165 @@ +/* UNIX V7 source code: see /COPYRIGHT or www.tuhs.org for details. */ + +#include +#include +#include +#include +#include +#include +#include +#define DICT "/usr/dict/words" +#define MAXERR 7 +#define MINSCORE 0 +#define MINLEN 7 + +const char *dictfile; +int alive,lost; +FILE *dict; +long int dictlen; +float errors=0, words=0; +char word[26],alph[26],realword[26]; +const unsigned int freq[] = +{ 42066, 9228, 24412, 14500, 55162, + 6098, 11992, 12648, 48241, 639, + 2944, 33351, 15545, 35618, 36211, + 16033, 937, 36686, 34957, 37544, + 17621, 5453, 3028, 1556, 12875, + 1743 +}; + +void fatal(const char *s) +{ + fprintf(stderr,"%s\n",s); + exit(1); +} + +void setup(void) +{ int tvec[2]; + struct stat statb; + time(tvec); + srand(tvec[1]+tvec[2]); + if((dict=fopen(dictfile,"r"))==NULL) fatal("no dictionary"); + if(stat(dictfile,&statb)<0) fatal("can't stat"); + dictlen=statb.st_size; +} + +double frand(void) +{ + return(rand()/32768.); +} + +void pscore(void) +{ + if(words!=0) + printf("(%4.2f/%.0f) ",errors/words,words); +} + +void getword(void) +{ char wbuf[128],c; + int i,j; +loop: + if(fscanf(dict,"%s\n",wbuf)==EOF) + { fseek(dict,0L,0); + goto loop; + } + if((c=wbuf[0])>'z' || c<'a') goto loop; + for(i=j=0;wbuf[j]!=0;i++,j++) + { if(wbuf[j]=='-') j++; + wbuf[i]=wbuf[j]; + } + wbuf[i]=0; + if(i'z') goto loop; + pscore(); + strcpy(realword,wbuf); + for(j=0;j'z') + { printf("lower case\n"); + goto loop; + } + if(alph[c-'a']!=0) + { printf("you guessed that\n"); + goto loop; + } + else alph[c-'a']=c; + for(i=0;realword[i]!=0;i++) + if(realword[i]==c) + { word[i]=c; + ok=1; + } + if(ok==0) + { alive--; + errors=errors+1; + if(alive<=0) lost=1; + return; + } + for(i=0;word[i]!=0;i++) + if(word[i]=='.') return; + alive=0; + lost=0; + return; +} + +void wordout(void) +{ + errors=errors+2; + printf("the answer was %s, you blew it\n",realword); +} + +void youwon(void) +{ + printf("you win, the word is %s\n",realword); +} + +int main(int argc, char *argv[]) +{ + if(argc==1) dictfile=DICT; + else dictfile=argv[1]; + setup(); + for(;;) + { startnew(); + while(alive>0) + { stateout(); + getguess(); + } + words=words+1; + if(lost) wordout(); + else youwon(); + } +} + + diff --git a/Applications/V7/games/quiz.c b/Applications/V7/games/quiz.c new file mode 100644 index 00000000..e1355d9b --- /dev/null +++ b/Applications/V7/games/quiz.c @@ -0,0 +1,483 @@ +/* UNIX V7 source code: see /COPYRIGHT or www.tuhs.org for details. */ + +#include +#include +#include + +#define NF 10 +#define NL 300 +#define NC 200 +#define SL 100 +#define NA 10 + +int tflag; +int xx[NL]; +char score[NL]; +int rights; +int wrongs; +int guesses; +FILE *input; +int nl = 0; +int na = NA; +int inc; +int ptr = 0; +int nc = 0; +char line[150]; +char response[100]; +char *tmp[NF]; +int select[NF]; +char *eu; +char *ev; + +extern int readline(void); +extern int cmp(char *, char *); +extern int disj(int); +extern int string(int s); +extern int eat(int, char); +extern int fold(char); +extern int publ(int); +extern int publish(char *); +extern int segment(char *, char *[]); +extern int perm(char *u[],int m,char *v[],int n,int p[]); +extern int find(char *u[],int m); +extern void readindex(void); +extern void talloc(void); +extern int query(char *r); +extern int next(void); +extern void done(int sig); +extern void instruct(const char *info); +extern void badinfo(void); +extern void dunno(void); + +int readline(void) +{ + char *t; + int c; +loop: + for (t=line; c=getc(input), *t=c, c!=EOF; t++) { + nc++; + if(*t==' '&&(t==line||t[-1]==' ')) + t--; + if(*t=='\n') { + if(t[-1]=='\\') /*inexact test*/ + continue; + while(t>line&&t[-1]==' ') + *--t = '\n'; + *++t = 0; + return(1); + } + if(t-line>=NC) { + printf("Too hard for me\n"); + do { + *line = getc(input); + if(*line==0377) + return(0); + } while(*line!='\n'); + goto loop; + } + } + return(0); +} + +int cmp(char *u,char *v) +{ + int x; + eu = u; + ev = v; + x = disj(1); + if(x!=1) + return(x); + return(eat(1,0)); +} + +int disj(int s) +{ + int t, x; + char *u; + u = eu; + t = 0; + for(;;) { + x = string(s); + if(x>1) + return(x); + switch(*ev) { + case 0: + case ']': + case '}': + return(t|x&s); + case '|': + ev++; + t |= s; + s = 0; + continue; + } + if(s) eu = u; + if(string(0)>1) + return(2); + switch(*ev) { + case 0: + case ']': + return(0); + case '}': + return(1); + case '|': + ev++; + continue; + default: + return(2); + } + } +} + +int string(int s) +{ + int x; + for(;;) { + switch(*ev) { + case 0: + case '|': + case ']': + case '}': + return(1); + case '\\': + ev++; + if(*ev==0) + return(2); + if(*ev=='\n') { + ev++; + continue; + } + default: + if(eat(s,*ev)==1) + continue; + return(0); + case '[': + ev++; + x = disj(s); + if(*ev!=']' || x>1) + return(2); + ev++; + if(s==0) + continue; + if(x==0) + return(0); + continue; + case '{': + ev++; + x = disj(s); + if(*ev!='}'||x>1) + return(2); + ev++; + continue; + } + } +} + +int eat(int s,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 fold(char c) +{ + if(c<'A'||c>'Z') + return(c); + return(c|040); +} + +int pub1(int s) +{ + for(;;ev++){ + switch(*ev) { + case '|': + s = 0; + continue; + case ']': + case '}': + case 0: + return; + case '[': + case '{': + ev++; + pub1(s); + continue; + case '\\': + if(*++ev=='\n') + continue; + default: + if(s) + putchar(*ev); + } + } +} + +int publish(char *t) +{ + ev = t; + pub1(1); +} + +int segment(char *u, char *w[]) +{ + char *s; + int i; + char *t; + s = u; + for(i=0;i1) badinfo(); + if(x==0) + continue; + p[i] = j; + goto uloop; + } + return(0); +uloop: ; + } + return(1); +} + +int find(char *u[],int m) +{ + int n; + while(readline()){ + n = segment(line,tmp); + if(perm(u,m,tmp+1,n-1,select)) + return(1); + } + return(0); +} + +void readindex(void) +{ + xx[0] = nc = 0; + while(readline()) { + xx[++nl] = nc; + if(nl>=NL) { + printf("I've forgotten some of it;\n"); + printf("I remember %d items.\n", nl); + break; + } + } +} + +void talloc(void) +{ + int i; + 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; + 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); +} + +int next(void) +{ + 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); +} + +void done(int sig) +{ + printf("\nRights %d, wrongs %d, ", rights, wrongs); + if(guesses) + printf("extra guesses %d, ", guesses); + printf("score %d%%\n",100*rights/(rights+wrongs)); + exit(0); +} + +void instruct(const char *info) +{ + int i, n; + printf("Subjects:\n\n"); + while(readline()) { + printf("-"); + n = segment(line,tmp); + for(i=1;i