/* MASTERMIND game. Invoke as mast pick from maxx */ #include "windows.h" #include #include #include #define WIDTH 4 #define HEIGHT 4 #define SMALL 1 #define PICK 4 #define FROM 6 FILE *file; struct DATA {char name[10]; int type; char datelastran[9]; int prevgames; double prevavg; int totalgames; double overallavg; }; struct DATA mm; int winnum(MOUSEPTR mouse,int numwindows,int winwidth); int which(MOUSEPTR mouse); void message(WINDOWPTR wp,int count,double *rounds,double *average,int navg); void readmm(FILE *file, struct DATA *mm, char *nameseek, int typeseek); void updatemm(MOUSEPTR mouse,FILE *file,struct DATA *mm, int presentgames,double presentavg); int pick; main(int argc, char *argv[]) { MOUSEPTR m1; WINDOWPTR w1[FROM],w2,w3[13*(PICK+1)]; int watrib[FROM],watrib2,watrib3[PICK]; int maxx,from,batrib=0,batrib2,ntar[PICK]; int color[]={RED,CYAN,MAGENTA,GREEN,BLUE,YELLOW}; int i,j,row,col,nx,no,n[FROM],flag,nrow,count,xpos[]={30,32,34,36,38}; int typeseek,navg; static char player[10],letter,list[]="ABCDEF"; char target[PICK+1],savestr[PICK+1],guess[PICK+1]; double average,rounds=0; time_t ltime; if(argc>=2) /* set number of pegs to pick */ {pick=atoi(argv[1]); if(pick>PICK)pick=PICK; } else pick=PICK; if(argc>=3) /* set number of a available colors */ {from=atoi(argv[2]); if(from>FROM)from=FROM; } else from=FROM; if(argc>=4) /* set maximum number of occurences of each color */ maxx=atoi(argv[3]); else maxx=2; nx=time(<ime)%100; /* randomize */ for (i=0;i maxx); target[i]=list[j]; ntar[i]=j; } target[pick]='\0'; if(strncmp(target,"AAAA",pick)==0)continue; if(strncmp(target,"BBBB",pick)==0)continue; do{ /* Restore generated string and read new trial */ nrow+=2; strcpy(savestr,target); for(i=0;i right button */ {for(i=0;i=13*(pick+1) && nx!=pick) {wn_printf(w2,"Sorry. You lost.\n"); winnum(m1,from,WIDTH); flag=1; break; } }while(nx!=pick && flag!=2); if(flag==0) {message(w2,count,&rounds,&average,navg); winnum(m1,from,WIDTH); } for(i=0;i1)wn_printf(wp,"average=%.2f ",*average); if(n>=3)wn_printf(wp,"LUCKEYYY!"); else if(n==2)wn_printf(wp,"WOW!"); else if(n==1)wn_printf(wp,"Fantastic!"); else if(n==0)wn_printf(wp,"Super!"); else if(n==-1)wn_printf(wp,"Good!"); wn_printf(wp,"\n"); } int which(MOUSEPTR mouse) { int num,stat,col,row,click,crick,slick=0; do{ mo_wait(mouse); mo_press(mouse,MO_LEFT,&stat,&click,&row,&col); mo_press(mouse,MO_RIGHT,&stat,&crick,&row,&col); slick+=(click+crick); }while(slick==0); num=(click>crick) ? 1 : 2; return num; } void updatemm(MOUSEPTR mouse, FILE *file, struct DATA *mm, int presentgames, double presentavg) { double temp,denom; long pos; if(presentgames>0) {_strdate(mm->datelastran); denom=mm->totalgames+presentgames; temp =mm->overallavg * mm->totalgames + presentavg * presentgames; mm->overallavg=temp/denom; mm->totalgames=denom; pos=ftell(file); fseek(file,pos,SEEK_SET); fprintf(file,"%s %d %s %d %.2f %d %.2f\n",mm->name,mm->type, mm->datelastran,presentgames,presentavg,mm->totalgames, mm->overallavg); system("mode co80"); printf("%s, you just played %d games at an average of %.2f.\n", mm->name,presentgames,presentavg); if(mm->totalgames>presentgames) printf("Overall,\nyou have played %d games at an average of %.2f.\n", mm->totalgames,mm->overallavg); getch(); } fclose(file); } void readmm(FILE *file, struct DATA *mm, char *nameseek, int typeseek) { struct DATA tt; int flag=0; do{ if(fscanf(file,"%s %d %s %d %lf %d %lf",tt.name,&tt.type,tt.datelastran, &tt.prevgames,&tt.prevavg,&tt.totalgames,&tt.overallavg)==EOF) break; if(strcmpi(tt.name,nameseek)==0 && tt.type==typeseek) {*mm=tt; flag=1; } }while(1); if(flag) printf("%s, you last played game type %d on %s,\nwhen you played %d " "games with an average score of %.2f.\nOverall,\nyou have " "played %d games with an average score of %.2f.\n" "Good luck on improving your score this time!\n", mm->name,mm->type,mm->datelastran,mm->prevgames,mm->prevavg, mm->totalgames,mm->overallavg); else {printf("%s, Enjoy Your Game of MASTERMIND!\n",nameseek); mm->prevgames=0; mm->prevavg=0.00; mm->totalgames=0; mm->overallavg=0.0; mm->type=typeseek; strcpy(mm->name,nameseek); } }