home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 183_01 / hangmath.c < prev    next >
Text File  |  1979-12-31  |  7KB  |  302 lines

  1. /*    This game involves guessing the digits in a 
  2.     three by two digit multiplication.  It is a
  3.     simple game, but absorbing.  You are likely
  4.     to find that it is relatively easy to guess
  5.     the right answer within three to five tries
  6.     once you are proficient at the game.
  7.  
  8.     The original was written in BASIC and first
  9.     appeared in KILOBAUD at about 1979.  I have
  10.     lost the original reference.
  11.  
  12.     This version was written as an exercise for
  13.     my own edification in learning the  Digital
  14.     Research version of C on the IBM-PC.
  15.  
  16.     Peter G. Wohlmut                         */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20.  
  21.  
  22. #define ESC    putchar(27)                 /* ANSI escape code */
  23. #define CLS    ESC,printf("[2J")           /* ANSI Clear Screen */
  24. #define LOC(r,c)  ESC,printf("[%d;%dH",r,c) /* ANSI position cursor */
  25. #define BEEP    printf("%c",'\007')    /*ASCII bell */
  26. #define SMODE(n) ESC,printf("[=%dh",n)    /* ANSI set screen type */
  27.  
  28.  
  29.  
  30. /*Haven't figured out how to get at the system time under PC-DOS yet
  31.   so have settled on calling HANG with a numeric argument to set up
  32.   the random number seed. */
  33.  
  34. main(argc,argv)
  35.  
  36.     int argc;
  37.     char *argv[];
  38.  
  39. {
  40.  
  41.     int width;        /* width =0 is 40 column 
  42.                      =2 is 80 column */
  43.     int c,d;        /* c is column #, d is guessed digit */
  44.     int ans;            /* keyboard responses */
  45.     int g;                /* game number */
  46.     int t;                /* total missed guesses */
  47.     int n1;                /* # misses in game */
  48.     int n5;                /* good guesses in game */
  49.     int f[10][6];            /* flags for which digits have
  50.                                  been guesses (f=1) */
  51.     long int a[6][6];        /* value of each digit */
  52.     int i,j,m,n9;            /* general indices */
  53.     int v;                /* average # misses/game */
  54.     int q;                /* performance rating index */
  55.     long int n[6];            /* power series for digit set up */
  56.     static char *r[] = {              /*Set up ratings */
  57.              "GREAT",
  58.                  "EXCELLENT",
  59.                  "VERY GOOD",
  60.                  "GOOD",
  61.                  "ABOVE AVERAGE",
  62.                  "AVERAGE",
  63.                  "BELOW AVERAGE",
  64.                  "FAIR",
  65.                 "POOR"
  66.             };
  67.  
  68.     static char *p[] = {        /* set up masked array size */
  69.                  "* * * * * ",
  70.                  "* * * * * ",
  71.                  "* * * * * ",
  72.                  "* * * * * ",
  73.                  "* * * *   ",
  74.                  "* * * * * "
  75.                 };
  76.  
  77.         width= 0;               /* set screen to 40 column */
  78.         SMODE(width);
  79.         CLS;            /* clear screen */
  80.  
  81.         ans=0; i=argc;
  82.     while (--i>0) ans+=atoi(argv[i]);
  83.         ans=srand(ans);         /*set up random seed */
  84.  
  85.  
  86. /* Print title page */
  87.  
  88.         CLS; LOC(3,2);
  89.         printf("%s\n","HANGMATH - a game of luck and logic");
  90.         LOC(5,2);
  91.         printf("%s\n","Play at least 3 games to get a score");
  92.  
  93.         LOC(9,10);
  94.         printf("%s","Instructions (y/n)");
  95.  
  96.         while((ans=getchar())!='\n')
  97.         if (ans=='Y'||ans=='y' ) instruct(ans);
  98.  
  99.         g=1;                /* first game */
  100.         t=0;
  101.  
  102.       while (g)     {            /* main loop */
  103.  
  104.     /* reset masked digits each game */
  105.  
  106.         CLS;
  107.         c=d=0;
  108.         for (i=1;i<6;i++) {
  109.             for (j=0;j<5;j++)
  110.                 *(p[i]+2*j)='*';
  111.                   }
  112.             *(p[4]+8)=' ';  /* fourth is left shifted */
  113.  
  114.  
  115. /* initialize masked number set */
  116.  
  117.         for (i=1;i<6;i++) {
  118.             for (j=1;j<6;j++) {
  119.             a[i][j]=-1;
  120.             n[i]=0;
  121.                      }
  122.                 }
  123.  
  124. /* initialize flags for entered numbers */
  125.  
  126.         for (i=0;i<10;i++) {
  127.             for (j=1;j<6;j++) {
  128.                 f[i][j]=0;
  129.                                             }
  130.                    }
  131.  
  132. /* generate random digits for multiplication */
  133.  
  134.     a[1][1]=rand()%10; a[2][1]=rand()%10; a[3][1]=rand()%10;
  135.     a[1][2]=rand()%10; a[2][2]=rand()%10; 
  136.  
  137.  
  138. /* do the multiplication */
  139.  
  140.         n[1]=100*a[3][1]+10*a[2][1]+a[1][1];
  141.         n[2]=10*a[2][2]+a[1][2];
  142.         n[3]=a[1][2]*nC1WW!HkEEEjua[2][2]*n[1]*10;
  143.         n[5]=n[1]*n[2];
  144.  
  145.         for (i=5;i>0;i--) {
  146.             m = power(10,i-1);
  147.  
  148.             for (j=3;j<6;j++) 
  149.                 a[i][j]=(n[j]/m+0.001);
  150.  
  151.             for (j=3;j<6;j++) {
  152.             n[j]=(n[j]-a[i][j]*m+0.9);
  153.                       }
  154.                        }
  155.         n1=0;
  156.         n5=0;
  157.  
  158.     /* eliminate superfluous digits */
  159.  
  160.         a[1][4]=a[4][1]=a[5][1]=a[3][2]=a[4][2]=a[5][2]=-1;
  161.         a[5][3]=-1;
  162.  
  163.     CLS;
  164.  
  165. /* start game loop */
  166.  
  167.         while (n5<30) {
  168.  
  169.  
  170.     LOC(1,21);printf("%s",(p[1]+4));
  171.     LOC(3,23);printf("%s",(p[2]+6));
  172. /*    LOC(4,16);printf("%s",());*/
  173.     LOC(5,19);printf("%s",(p[3]+2));
  174.     LOC(6,17);printf("%s",p[4]);
  175.     LOC(7,16);printf("%s","-----------");
  176.     LOC(8,17);printf("%s",p[5]);
  177.  
  178.     LOC(12,1);printf("%s %1d","# misses: ",n1);
  179.     LOC(14,1);printf("%s","PREVIOUS GUESSES BY COLUMN");
  180.  
  181.         for (i=1;i<6;i++) {
  182.             LOC(15+i,1);printf("%s %d %s","COL ",i,":");
  183.  
  184.         for (j=0;j<10;j++) 
  185.             if (f[j][i]) printf("%d %s",j," ");
  186.                   }
  187.  
  188.         if (n5>=18) break;
  189.  
  190.         LOC(10,1); printf("ENTER COLUMN #");
  191.             c=interrupt(7);           /*DOS function 7 - keyboard 
  192.                         poll with no echo */
  193.         LOC(10,15); printf("%c %s",c," ");
  194.  
  195.  
  196.         LOC(10,20); printf("ENTER DIGIT ");
  197.             d=interrupt(7);
  198.         LOC(10,33); printf("%c %s",d," ");
  199.  
  200.     c=c-'0';            /*set c,d to integers */
  201.     d=d-'0';
  202.  
  203.     if ((c<1||c>5)||(d<0||d>9)) continue;  /*check for bounds */
  204.  
  205.         if (f[d][c]) {
  206.             LOC(24,1);BEEP;BEEP;
  207.         printf("YOU ALREADY GUESSED THAT!");
  208.                 }
  209.     else {
  210.         LOC(24,1);printf("                          ");
  211.         m=1;
  212.         i=0;
  213.  
  214.     while (m<6)
  215.         {
  216.         if ((a[c][m]==d) && (!f[d][c])) {
  217.  
  218.         j=8-2*(c-1);
  219.         *(p[m]+j)=d+'0';     /*replace * with digit */
  220.             ++n5;
  221.             i=m;
  222.                     }
  223.  
  224.             ++m;
  225.         }
  226.         if (!i)
  227.             ++n1;        /* tally wrong guess */
  228.  
  229.         f[d][c]=1;
  230.  
  231.     }
  232.             }
  233.     LOC(21,1);
  234.     BEEP;BEEP;BEEP;
  235.      printf("%s\n","YOU GOT IT !!!!");
  236.         t+=n1; v=t/g;       /*tally total wrong, and average */
  237.     LOC(22,1);
  238.     printf("%s %d %s %d\n","Average # of misses after ",g," games ",v);
  239.     LOC(23,1);
  240.     printf("%s","Another Game???? (y/n) ");
  241.  
  242.         if((ans=interrupt(7))=='N'||ans=='n') break;
  243.         ++g;
  244.  
  245.   }
  246.     q=v/2;
  247.         if(q<0 ) q=0;
  248.         if(q>8 ) q=8;
  249.  
  250.         width = 2;            /* set to 80 column B&W */
  251.         SMODE(width);
  252.  
  253.  
  254.     printf("%s %s %s\n","Your performance rating was ",r[q],". BYE");
  255.  
  256. }
  257.  
  258. instruct(i)
  259. {
  260.  
  261.         CLS;
  262.         printf("%s\n","This game sets up a three digit by");
  263.         printf("%s\n","two digit multiplication and displays");
  264.         printf("%s\n","the details with all digits masked by");
  265.         printf("%s\n","a * for each digit. Type the column");
  266.         printf("%s\n","and then the number you chose as");
  267.         printf("%s\n","a number between 0 and 9.  All");
  268.         printf("%s\n","occurences of that digit in that ");
  269.         printf("%s\n","will then be displayed.  Play until");
  270.         printf("%s\n","you have replace,$a`l *s with digits.");
  271.         printf("%s\n","Columns are numbered from right to");
  272.         printf("%s\n","left as #1 to #5");
  273.  
  274.         LOC(20,5);
  275.         printf("%s","Touch return key to continue");
  276.         getchar();
  277.  
  278.         return;
  279. }
  280. /* calculate i to the jth power */
  281. power(i,j)
  282. {
  283.     int l;
  284.     int m;
  285.  
  286.         m=1;
  287.         for (l=1;l<j+1;l++) 
  288.             m=m*i;
  289.  
  290.         return(m);
  291. }
  292. /* IBM DOS interrupt calls */
  293. interrupt(i)
  294. {
  295.     int c;
  296.  
  297.     c=__IBMDOS(i);
  298.  
  299.     return(c);
  300.  
  301. }
  302.