holloway@drivax.UUCP (Bruce Holloway) (04/07/86)
Dumb little game written to be an opponent to the "moo" game distributed with System V. Think of a four digit number, no duplicate digits allowed. The computer will guess a number. Type in 'B' for each digit in the correct place, and 'W' for each digit in the answer but not in the correct place. 'BBBB' means it's solved the puzzle. Example: My guess (00) --> 6839 How'd I do? www My guess (97) --> 5963 How'd I do? bw My guess (99) --> 0368 How'd I do? ww My guess (99) --> 7986 How'd I do? bbb My guess (99) --> 1986 How'd I do? bbbb Ah ha! Play again? n ------------------------cut here---------------------------- #include "stdio.h" extern char *malloc(); main(){ init(); do{ ginit(); do{ guess(); } while(getfilter()); } while(playagain()); } playagain(){ char iline[10]; printf("\n\nPlay again? "); getline(iline,6); printf("\n\n"); return(iline[0]=='y' || iline[0]=='Y'); } char *pool; int left; init(){ if(!(pool = malloc(10000))){ printf("Allocation error."); exit(1); } srand(time(0L)); } ginit(){ int i; char *tp; for(i=0, tp=pool; i<10000; ++i) *tp++ = 0; left = 10000; } int g; guess(){ int prob; g = (rand() & 0x7FFF) % 10000; while(pool[g] || notok(g)){ if(!pool[g]){ pool[g]=1; --left; } g = (g + 1) % 10000; } prob = (10000-left)/100; printf("My guess (%02d\045) --> %04d\n",prob,g); } notok(tg){ int dig[4], i, j; makedig(tg,dig); for(i=0; i<3; ++i) for(j=i+1; j<4; ++j) if(dig[i]==dig[j]) return(1); return(0); } getfilter(){ int b, w, i, tb, tw; char iline[10]; printf("How'd I do? "); getline(iline,10); for(i=b=w=0; iline[i];){ switch(iline[i++]){ case 'b': case 'B': ++b; break; case 'w': case 'W': ++w; break; } } if(b == 4){ printf("Ah ha!\n"); return(0); } for(i=0; i<10000; ++i){ if(pool[i]) continue; score(g,i,&tb,&tw); if(tb != b || tw != w){ pool[i] = 1; --left; } } return(1); } score(guess,real,b,w) int guess, real, *b, *w; { int tb, tw, i, j, ga[4], ra[4]; if(notok(real)){ *b = *w = -1; return; } makedig(guess,ga); makedig(real,ra); tb = tw = 0; for(i=0; i<4; ++i) for(j=0; j<4; ++j) if(ga[i] == ra[j]){ if(i == j) ++tb; else ++tw; break; } *b = tb; *w = tw; } makedig(n,array) int n, *array; { int i; for(i=0; i<4; ++i){ *array++ = n % 10; n /= 10; } } getline(buf,count) char buf[]; int count; { gets(buf); } ----------------------------cut here------------------------------ -- +----------------------------------------------------------------------------+ |Whatever I write are not the opinions or policies of Digital Research, Inc.,| |and probably won't be in the foreseeable future. | +----------------------------------------------------------------------------+ Bruce Holloway ....!ucbvax!hplabs!amdahl!drivax!holloway (I'm not THAT Bruce Holloway, I'm the other one.)