/* entropy.tc - tct - 11/19/15 */ /* Ref: http://primepuzzle.com/tc/bullcow.html */ /* will need to convert this to C since tiny-c doesn't do natural log or reals */ /* globals */ int th,hu,te,on,sc(40) int ar(119) /* permutation formula : 5P4 = 5*4*3*2 */ int scr int sd,ls random int low,high [ /* returns a random integer between low and high */ int range if (ls==0) ls=sd=99 range=high-low+1 ls=ls*sd if (ls<0) ls=-ls return low+(ls/8)%range ] nodup int i [ /* returns 0 if there are duplicate or illegal digits, otherwise */ /* returns i. globals th, hu, te, on are set. */ int w,is is=i th=i/1000;i=i%1000 hu=i/100;i=i%100 te=i/10;on=i%10 if ((th>5)+(hu>5)+(te>5)+(on>5)) return 0 w=!th+!hu+!te+!on+(th==hu)+(th==te)+(th==on)+(hu==te)+(hu==on)+(te==on) return !w*is ] score int x,y [ /* compute bulls and cows and return encoded score */ int b,c,ths,hus,tes,ons nodup x ths=th;hus=hu;tes=te;ons=on /* save the digits */ nodup y if (ths==th) [++b] else if (ths==hu) [++c] /* parens and brackets needed! */ else if (ths==te) [++c] else if (ths==on) [++c] if (hus==hu) [++b] else if (hus==th) [++c] else if (hus==te) [++c] else if (hus==on) [++c] if (tes==te) [++b] else if (tes==th) [++c] else if (tes==hu) [++c] else if (tes==on) [++c] if (ons==on) [++b] else if (ons==th) [++c] else if (ons==hu) [++c] else if (ons==te) [++c] return sc(10*b+c) ] pr [ /* prompt for response to printed guess and get encoded bull/cow counts */ pl "" pl "Compare your number to my guess. Count the Bulls (right digit, right position)" pl "and the Cows (right digit, wrong position). Enter a number from 0 to 8 using the" pl "table below, eg. if there are 2 Bulls 2 Cows, enter 4." pl "" pl "4B0C 0" pl "3B0C 1 3B1C 2" pl "2B1C 3 2B2C 4" pl "1B2C 5 1B3C 6" pl "0B3C 7 0B4C 8" pl "" pl "? " return gn ] guess int maxm [ /* sets the global variable scr. returns a new guess. */ int gs char yn gs=ar(random(0,maxm)) pl "My guess for your 4-digit number is" pn gs;ps "." pl "";pl "override y/n? " yn=getchar if (yn=='y') [ pl "enter override : " gs=gn ] scr=pr;pl "" return gs ] entropy [ /* main function */ pl "entropy.tc - tct - 11/19/15" pl "Ref: http://primepuzzle.com/tc/bullcow.html";pl "";pl "" ps "Think of a 4-digit number. Use digits 1 thru 5. No repeating digits." pl "";pl "" /* encode the scores : number of bulls is multiplied by 10 and added to */ /* number of cows */ sc(40)=0;sc(30)=1;sc(31)=2;sc(21)=3;sc(22)=4;sc(12)=5;sc(13)=6 sc(3)=7;sc(4)=8 /* locals */ int i,m,gs,tries,maxm,j int counts(8) m=-1 for (i=1234;i<=5432;++i) [ /* load array ar */ if nodup(i) [pn i;ar(++m)=i] ] ls=sd=1253 /* hard code seed (sb odd) */ /* set things up */ maxm=119;tries=1 gs=guess(maxm) while scr [ /* keep going till you have a perfect score (ie 0) */ m=-1 for (i=0;i<=maxm;++i) [ if (scr==score(gs,ar(i))) ar(++m)=ar(i) /* overlay bad w/ good */ ] /* end for */ for (i=0;i<=m;++i) [ for (j=0;j<=8;++j) counts(j)=0 for (j=0;j<=m;++j) [ ++counts(score(ar(j),ar(i))) ] pn i;ps " - ";pn ar(i);ps " - " for (j=0;j<=8;++j) pn counts(j);pl "" ] pl "" gs=guess(maxm=m) ++tries ] /* end while */ ps "It took me";pn tries if tries==1 ps " try!";else ps " tries!" pl "I am pretty much awesome.";pl "" ]