home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / catalogs / mtchplay.zip / MP_CALC.C < prev    next >
Text File  |  1993-02-12  |  7KB  |  210 lines

  1. /* MP_CALC.C   basic calulations for MTCHPLAY */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6. #include "defines.h"
  7.  
  8. /* macro for calculating adjustment on hole h (x = hcp[a]-hcp[b]-order[h]) */
  9. #define correction(x)  (x)<0 ? 0 : (x)<18 ? 1 : (x)<36 ? 2 : 3
  10.  
  11. extern int NP[], order[], ndx[], outcome[MAXSZ][MAXSZ];
  12. extern short indx[MAXFL][MAXSZ];
  13. extern PLAYER player[MAXFL][MAXSZ];
  14.  
  15. /* function prototypes */
  16. void
  17.   calc_all(void),
  18.   calc_outcome(int),
  19.   calc_results(int),
  20.   set_gnp(int);         /* set .gross, .net, .points for flight */
  21. int
  22.   callaway(char *);
  23.  
  24. /* ---------------------------------------------------------------------- */
  25. void
  26. calc_all(void)
  27. {
  28.    int i;
  29.  
  30.    for (i=0; i<MAXFL; i++) {
  31.      if (!NP[i]) continue;
  32.      calc_results(i);
  33.    }
  34. }
  35.  
  36. /* ---------------------------------------------------------------------- */
  37. void
  38. set_gnp(int flight)    /* set gross, net, points for all players in flight */
  39. {
  40.   int i,h,g,points,val;
  41.   extern int par[18];
  42.  
  43.   for (i=0; i<NP[flight]; i++) {
  44.     /* skip players with gross already entered */
  45.     if (player[flight][i].gross != 0)
  46.       continue;
  47.     /* set gross, net, points to 0 if no scores */
  48.     if (player[flight][i].scores[0] == 0) {
  49.       player[flight][i].gross = 0;
  50.       player[flight][i].net = 0;
  51.       player[flight][i].points = 0;
  52.       continue;
  53.     }
  54.     g = points = 0;
  55.     for (h=0; h<18; h++) {
  56.       g += player[flight][i].scores[ndx[h]]-48;
  57.       val = 2 + par[h] - (player[flight][i].scores[ndx[h]]-48);
  58.       if (val < -1) val = -1;
  59.       if (val > 4) val = 4;
  60.       points += val;
  61.     }
  62.     points += player[flight][i].hcp;
  63.     if (points > 40) points = 40;
  64.     player[flight][i].gross = g;
  65.     player[flight][i].net = g - player[flight][i].hcp;
  66.     player[flight][i].points = points;
  67.   }
  68. }
  69.  
  70. /* ---------------------------------------------------------------------- */
  71. void
  72. calc_outcome(int flight)     /* like calc_results() but fills outcome[][] */
  73. {
  74.   int i,j,a,b,k,h,w,x,y;
  75.   int diff;
  76.   int sa[18],sb[18];         /* gross scores for player[a], player[b] */
  77.   int i0;
  78.  
  79.   for (i=NP[flight]-1; i>0; i--) {
  80.     if (player[flight][i].scores[0] == 0)  /* ignore players w/o scores */
  81.        continue;
  82.     for (j=i-1; j>=0; j--) {
  83.       if (player[flight][j].scores[0] == 0)
  84.         continue;
  85.       if (player[flight][i].hcp > player[flight][j].hcp)
  86.         a=i, b=j;
  87.       else
  88.         a=j, b=i;
  89.       /* extract scores from formatted storage strings */
  90.       for (h=0; h<18; h++) sa[h] = player[flight][a].scores[ndx[h]]-48;
  91.       for (h=0; h<18; h++) sb[h] = player[flight][b].scores[ndx[h]]-48;
  92.       k = player[flight][a].hcp - player[flight][b].hcp;
  93.                                    /* k is non-negative, by choice of a,b */
  94.       i0 = 17;               /* i0 = number of last hole that counts here */
  95.       w = 0;                      /* w counts net wins (+ for a, - for b) */
  96.       for (h=0; h<18; h++) {
  97.         diff = correction(k-order[h]);  /* diff = 0,1,2,3 = advantage to a */
  98.         x = sa[h] - diff;               /* x = a's net score relative to b */
  99.         y = sb[h];                    /* since b has higher hcp, no change */
  100.         w += (y-x>0 ? 1 : y==x ? 0 : -1);
  101.         if (h<17 && abs(w)>17-h) {
  102.           i0 = h;
  103.           break;
  104.         }
  105.       }
  106.       if (w==19-i0) w += 10;  /* to distinguish 'w and w-1' from 'w and w-2'; */
  107.       if (-w==19-i0) w -= 10; /* e.g., |w|=3 -> 3&1 while |w|=13 -> 3&2 */
  108.       outcome[a][b] = w;
  109.       outcome[b][a] = -w;
  110.     }
  111.   }
  112. }
  113.  
  114.  
  115. /* ---------------------------------------------------------------------- */
  116. void
  117. calc_results(int flight)
  118. {
  119.   int i,j,a,b,k,h,w,x,y;
  120.   int diff,old_np;
  121.   int sa[18],sb[18];  /* gross scores, player[flight][a], player[flight][b] */
  122.   int w1;
  123.   for (i=0; i<NP[flight]; i++)
  124.     player[flight][i].won=player[flight][i].tied=player[flight][i].lost=0;
  125.   for (i=NP[flight]-1; i>0; i--) {
  126.     if (player[flight][i].scores[0] == 0)  /* ignore players w/o scores */
  127.        continue;
  128.     for (j=i-1; j>=0; j--) {
  129.       if (player[flight][j].scores[0] == 0)
  130.         continue;
  131.       if (player[flight][i].hcp > player[flight][j].hcp)
  132.         a=i, b=j;
  133.       else
  134.         a=j, b=i;
  135.       /* extract scores from formatted storage strings */
  136.       for (h=0; h<18; h++) sa[h] = player[flight][a].scores[ndx[h]]-48;
  137.       for (h=0; h<18; h++) sb[h] = player[flight][b].scores[ndx[h]]-48;
  138.  
  139.       k = player[flight][a].hcp - player[flight][b].hcp;
  140.                                    /* k is non-negative, by choice of a,b */
  141.       w = 0;                       /* w counts net wins (+ for a, - for b) */
  142.       for (h=0; h<18; h++) {
  143.         diff = correction(k-order[h]);  /* diff = 0,1,2,3 = advantage to a */
  144.         x = sa[h] - diff;               /* x = a's net score relative to b */
  145.         y = sb[h];                    /* since b has higher hcp, no change */
  146.         w += (y-x>0 ? 1 : y==x ? 0 : -1);
  147.         if (h<17 && abs(w)>17-h) break;
  148.       }
  149.       if (w == 0) {                               /* a and b tied */
  150.         player[flight][a].tied++; player[flight][b].tied++;
  151.       }
  152.       else if (w > 0) {                            /* a defeated b */
  153.         player[flight][a].won++;
  154.         player[flight][b].lost++;
  155.       }
  156.       else if (w < 0) {                            /* b defeated a */
  157.         player[flight][b].won++;
  158.         player[flight][a].lost++;
  159.       }
  160.     }
  161.   }
  162.   asort(flight);
  163. }
  164.  
  165. callaway(char *scores)
  166. {
  167.   int i,j,temp,num,h=0,g=0;
  168.   int w[11];
  169.   int score[18];
  170.  
  171.   /* extract scores from formatted string 'scores' into array score[] */
  172.   /* ndx[i] = location of score on hole i in string 'scores' */
  173.   for (i=0; i<18; i++) score[i] = scores[ndx[i]]-48;
  174.   /* set g = gross score, 18 holes */
  175.   for (i=0; i<18; i++) g += score[i];
  176.  
  177.   if (g < 73)
  178.     return 0;
  179.   if (g > 130)
  180.     return 40;
  181.  
  182.   /* sort first 16 scores in descending order */
  183.   for (i=0; i<15; i++)
  184.     for (j=i+1; j<16; j++)
  185.       if (score[i] < score[j]) {
  186.         temp = score[i];
  187.         score[i] = score[j];
  188.         score[j] = temp;
  189.       }
  190.  
  191.   /* establish array w[] =  */
  192.   /* worst,worst,next_worst,next_worst, ... sixth_worst,sixth_worst */
  193.   for (i=0; i<6; i++) w[2*i] = w[2*i+1] = score[i];
  194.  
  195.   /* determine number of worst holes to use */
  196.   num = (g-1)/5 - 14;
  197.  
  198.   /* calculate handicap */
  199.   for (i=0; i<= num; i++)
  200.     h += w[i];               /* h = 2*hcp, before adjustment */
  201.   h = (h+1)/2;               /* (half strokes count as strokes */
  202.   /* add adjustment */
  203.   if (g % 5 == 0)
  204.     h += 2;
  205.   else
  206.     h += (g % 5 - 3);
  207.   return h;
  208. }
  209.  
  210.