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

  1. /* MP_OUT.C  Various display and print routines */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <dos.h>
  9. #include "defines.h"
  10.  
  11. /* macro for calculating adjustment on hole h (x = hcp[a]-hcp[b]-order[h]) */
  12. #define correction(x)  (x)<0 ? 0 : (x)<18 ? 1 : (x)<36 ? 2 : 3
  13. extern PLAYER player[MAXFL][MAXSZ];
  14.  
  15. extern int numflights,scol,srow,width;
  16. extern short indx[MAXFL][MAXSZ],outcome[MAXSZ][MAXSZ];
  17. extern int NP[],ndx[],order[];
  18. extern char namefield[],mark[];
  19.  
  20. /* function prototypes */
  21. void
  22.   asort(int),
  23.   compare_two(void),
  24.   fprnspaces(FILE *,int),
  25.   gsort(int),
  26.   init_compare(void),
  27.   init_list_gross(void),
  28.   init_list_matches(void),
  29.   init_list_net(void),
  30.   init_list_results(void),
  31.   list_gross(int),
  32.   list_net(int),
  33.   list_matches(void),
  34.   list_results(int),
  35.   msort(int),
  36.   nsort(int),
  37.   psort(int),
  38.   print_gross(int),
  39.   print_net(int),
  40.   print_points(int),
  41.   print_matches(int),
  42.   print_results(int),
  43.   prnspaces(int),
  44.   strokes_msg(int,int,int);
  45. int
  46.   make_resultstring(int,int,int,char *);
  47. extern void
  48.   calc_outcome(int);
  49. extern int
  50.   findn(char *, int *, int *),
  51.   get_string(char *,char *,char *,int,int),
  52.   pick_one(int, int, int *, int *);
  53.  
  54. static char *outcomestring[21] =
  55.   {" Tied","1 up","2&1 ","3&2 ","4&3 ","5&4 ","6&5 ","7&6 ","8&7 ","9&8 ",
  56.     "","","2 up","3&1 ","4&2 ","5&3 ","6&4 ","7&5 ","8&6 ","9&7 ","10&8"};
  57.  
  58. /* indexing arrays */
  59. short indx[MAXFL][MAXSZ];             /* for indexing flights */
  60. short I[MAXFL*MAXSZ],J[MAXFL*MAXSZ];  /* for indexing total field */
  61.  
  62. int gross[MAXFL][MAXSZ], net[MAXFL][MAXSZ]; /* gross and net scores */
  63. int points[MAXFL][MAXSZ];                   /* point system points */
  64. char *basic_msgs[] = {    /* msgs used in list_xxx() functions */
  65.         "PgUp : Prev Flight",
  66.         "PgDn : Next Flight",
  67.         "",
  68.         "",
  69.         "Press Esc or Enter",
  70.         "to return to menu."
  71.      };
  72.  
  73. /* ---------------------------------------------------------------------- */
  74. void
  75. asort(int flight)
  76. {
  77.   int i,j,temp,sz;
  78.  
  79.   sz = NP[flight];
  80.   for (i=0; i<sz; i++) indx[flight][i] = i;
  81.  
  82.   for (i=1; i<sz; i++) {
  83.     temp = indx[flight][i];
  84.     j = i-1;
  85.     while(j>=0) {
  86.       if (strcmp(player[flight][indx[flight][j]].name,player[flight][temp].name)>0) {
  87.         indx[flight][j+1] = indx[flight][j];
  88.         j--;
  89.       }
  90.       else
  91.         break;
  92.     }
  93.     indx[flight][j+1] = temp;
  94.   }
  95. }
  96.  
  97. /* ---------------------------------------------------------------------- */
  98. void
  99. init_compare()
  100. {
  101.   int i;
  102.  
  103.   std_screen();
  104.   gotoxy(7,3);
  105.   cprintf("Compare Two");
  106.   gotoxy(3,5);
  107.   cprintf("Any two players, not");
  108.   gotoxy(3,6);
  109.   cprintf("necessarily in the");
  110.   gotoxy(3,7);
  111.   cprintf("same flight, may be");
  112.   gotoxy(3,8);
  113.   cprintf("compared at match play.");
  114.   gotoxy(3,10);
  115.   cprintf("In the list of net");
  116.   gotoxy(3,11);
  117.   cprintf("scores, the winning");
  118.   gotoxy(3,12);
  119.   cprintf("score on each hole is");
  120.   gotoxy(3,13);
  121.   cprintf("highlighted. Only the");
  122.   gotoxy(3,14);
  123.   cprintf("holes needed to decide");
  124.   gotoxy(3,15);
  125.   cprintf("the match are printed.");
  126.   gotoxy(3,17);
  127.   cprintf("To return to the menu,");
  128.   gotoxy(3,18);
  129.   cprintf("press Esc or Enter.");
  130. }
  131.  
  132. /* ---------------------------------------------------------------------- */
  133. void
  134. compare_two(void)
  135. {
  136.   char name1[21], name2[21];
  137.   int r,r1,i,m1,m2,k,a,b,h,diff;
  138.   int col=2,row=3;
  139.   int i0,w,flight1,flight2,flighta,flightb;
  140.   int sa[18], sb[18], net[18],x, y;            /* gross and net scores */
  141.   char surname_a[21],surname_b[21],*p,*q;
  142.   extern int norm_attr, high_attr;
  143.   int hcp;
  144.   int flt[10],index[10];
  145.  
  146.   init_compare();
  147.   window(28,1,80,25);
  148.   textattr(norm_attr);
  149. a:
  150.   clrscr();
  151.   r = get_string("Name1",name1,namefield,col+8,row);
  152.   if (!r) goto ret;
  153.   k = findn(name1,flt,index);
  154.   if (k == -2) {
  155.     gotoxy(col,row+1);
  156.     cprintf("More than ten found. Press any key...");
  157.     getch();
  158.     goto a;
  159.   }
  160.   if (k == -1) {
  161.     gotoxy(col,row+1);
  162.     cprintf("Name not found. Press any key...");
  163.     getch();
  164.     goto a;
  165.   }
  166.   if (k > 0)
  167.     k = pick_one(row+1, k, flt, index);
  168.   flight1 = flt[k];
  169.   m1 = index[k];
  170.   if (player[flight1][m1].scores[0] == 0) {
  171.     gotoxy(col,row+1);
  172.     cprintf("%s has incomplete data.",player[flight1][m1].name);
  173.     getch();
  174.     goto a;
  175.   }
  176.   gotoxy(col+8,row);
  177.   clreol();
  178.   cputs(player[flight1][m1].name);
  179. b:
  180.   gotoxy(col,row+1); clreol();
  181.   r = get_string("Name2",name2,namefield,col+8,row+1);
  182.   if (!r) goto ret;
  183.   k = findn(name2,flt,index);
  184.   if (k == -2) {
  185.     gotoxy(col,row+2);
  186.     cprintf("More than ten found. Press any key...");
  187.     getch();
  188.     gotoxy(col,row+2); clreol();
  189.     goto b;
  190.   }
  191.   if (k == -1) {
  192.     gotoxy(col,row+2);
  193.     cprintf("Name not found. Press any key...");
  194.     getch();
  195.     gotoxy(col,row+2); clreol();
  196.     goto b;
  197.   }
  198.   if (k > 0)
  199.     k = pick_one(row+2, k, flt, index);
  200.   flight2 = flt[k];
  201.   m2 = index[k];
  202.   if (player[flight2][m2].scores[0] == 0) {
  203.     gotoxy(col,row+2);
  204.     cprintf("%s has incomplete data.",player[flight2][m2].name);
  205.     getch();
  206.     gotoxy(col,row+2); clreol();
  207.     goto b;
  208.   }
  209.  
  210.   k = player[flight1][m1].hcp - player[flight2][m2].hcp;
  211.   if (k == 0) {
  212.     i = strcmp(player[flight1][m1].name,player[flight2][m2].name);
  213.     if (i == 0)
  214.       goto b;                /* player duplicated */
  215.     else if (i > 0) {        /* list alphabetically if same hcp */
  216.       a = m1, b = m2;
  217.       flighta = flight1, flightb = flight2;
  218.     }
  219.     else {
  220.       a = m2, b = m1;
  221.       flighta = flight2, flightb = flight1;
  222.     }
  223.   }
  224.   else if (k > 0) {
  225.     a=m1, b=m2;
  226.     flighta = flight1, flightb = flight2;
  227.   }
  228.   else if (k < 0) {
  229.     k = -k;
  230.     a=m2, b=m1;
  231.     flighta = flight2, flightb = flight1;
  232.   }
  233.   p = player[flighta][a].name;
  234.   q = surname_a;
  235.   do *q++ = *p++; while (*p && *p != ',');
  236.   *q = '\0';
  237.   p = player[flightb][b].name;
  238.   q = surname_b;
  239.   do *q++ = *p++; while (*p && *p != ',');
  240.   *q = '\0';
  241.   /* if surnames are identical, use full names */
  242.   if (strcmp(surname_a,surname_b) == 0) {
  243.     strcpy(surname_a,player[flighta][a].name);
  244.     strcpy(surname_b,player[flightb][b].name);
  245.   }
  246.   clrscr();
  247.   gotoxy(col,row);
  248.   cputs(player[flightb][b].name);
  249.   gotoxy(col+21,row); cprintf("hcp: %d",player[flightb][b].hcp);
  250.   gotoxy(col,row+1);
  251.   cputs(player[flighta][a].name);
  252.   gotoxy(col+21,row+1); cprintf("hcp: %d",player[flighta][a].hcp);
  253.   gotoxy(col,row+2);
  254.   if (k == 0)
  255.     cprintf("Neither player receives any strokes.");
  256.   else {
  257.     cprintf("Handicap correction:");
  258.     strokes_msg(k,col,row+3);
  259.   }
  260.   gotoxy(col+2, row+8);
  261.   cputs("Gross scores:");
  262.   gotoxy(col,row+9);
  263.   cputs(player[flightb][b].name);
  264.   gotoxy(col+21,row+9);
  265.   cputs(player[flightb][b].scores);
  266.   gotoxy(col,row+10);
  267.   cputs(player[flighta][a].name);
  268.   gotoxy(col+21,row+10);
  269.   cprintf("%s",player[flighta][a].scores);
  270.       for (h=0; h<18; h++) sa[h] = player[flighta][a].scores[ndx[h]]-48;
  271.       for (h=0; h<18; h++) sb[h] = player[flightb][b].scores[ndx[h]]-48;
  272.       k = player[flighta][a].hcp - player[flightb][b].hcp;
  273.                                    /* k is non-negative, by choice of a,b */
  274.       i0 = 17;               /* i0 = number of last hole that counts here */
  275.       w = 0;                       /* w counts net wins (+ for a, - for b) */
  276.       for (h=0; h<18; h++) {
  277.         diff = correction(k-order[h]);  /* diff = 0,1,2,3 = advantage to a */
  278.         x = sa[h] - diff;               /* x = a's net score relative to b */
  279.         net[h] = x>0 ? x : 0;           /* don't display negative net scores */
  280.         y = sb[h];                      /* since b has lower hcp, no change */
  281.         w += (y-x>0 ? 1 : y==x ? 0 : -1);
  282.         if (h<17 && abs(w)>17-h) {
  283.           i0 = h;
  284.           break;
  285.         }
  286.       }
  287.     if (w==19-i0) w += 10;  /* to distinguish 'w and w-1' from 'w and w-2'; */
  288.     if (-w==19-i0) w -= 10; /* e.g., |w|=3 -> 3&1 while |w|=13 -> 3&2 */
  289.   gotoxy(col+2,row+12);
  290.   cputs("Net scores:");
  291.   gotoxy(col,row+13);
  292.   cputs(player[flightb][b].name);
  293.   gotoxy(29,row+10);
  294.   for (i=0; i<=i0; i++) {
  295.     gotoxy(col+21+ndx[i],row+13);
  296.     if (sb[i] < net[i])
  297.       textattr(high_attr);
  298.     else
  299.       textattr(norm_attr);
  300.     cprintf("%d",sb[i]);
  301.   }
  302.   textattr(norm_attr);            /* in case changed for last score */
  303.   gotoxy(col,row+14);
  304.   cputs(player[flighta][a].name);
  305.   gotoxy(col+21,row+14);
  306.   for (i=0; i<=i0; i++) {
  307.     gotoxy(col+21+ndx[i],row+14);
  308.     if (net[i] < sb[i])
  309.       textattr(high_attr);
  310.     else
  311.       textattr(norm_attr);
  312.     cprintf("%d",net[i]);
  313.   }
  314.   textattr(norm_attr);            /* in case changed for last score */
  315.   r1 = abs(w);
  316.   gotoxy(col,row+16);
  317.   if (w==0) {
  318.     cprintf("Tied");
  319.     getch();
  320.     goto a;
  321.   }
  322.   else if (w>0) cprintf("%s over %s, ",surname_a,surname_b);
  323.   else if (w<0) cprintf("%s over %s, ",surname_b,surname_a);
  324.   if (r1 == 1) cputs("1 up");
  325.   else if (r1 == 12) cputs("2 up");
  326.   else if (r1<10) cprintf("%d and %d",r1,r1-1);
  327.   else if (r1>12) cprintf("%d and %d",r1-10,r1-12);
  328.   if (getch() == ESC) goto ret;
  329.   goto a;
  330.   ret:
  331.   return;
  332. }
  333.  
  334. /* ---------------------------------------------------------------------- */
  335. void
  336. gsort(int flight)
  337. {
  338.   int i,j,temp,sz;
  339.   int g[MAXSZ];
  340.  
  341.   sz = NP[flight];
  342.   for (i=0; i<sz; i++)  indx[flight][i] = i;
  343.  
  344.   for (i=1; i<sz; i++) {
  345.     temp = indx[flight][i];
  346.     j = i-1;
  347.     while(j>=0) {
  348.       if (gross[flight][indx[flight][j]] > gross[flight][temp]
  349.         || (gross[flight][indx[flight][j]] == gross[flight][temp] &&
  350.         strcmp(player[flight][indx[flight][j]].name,player[flight][temp].name)>0)) {
  351.         indx[flight][j+1] = indx[flight][j];
  352.         j--;
  353.       }
  354.       else
  355.         break;
  356.     }
  357.     indx[flight][j+1] = temp;
  358.   }
  359. }
  360.  
  361. /* ---------------------------------------------------------------------- */
  362. void
  363. init_list_gross()
  364. {
  365.   int i;
  366.  
  367.   std_screen();
  368.   gotoxy(10,5);
  369.   gotoxy(7,3);
  370.   cprintf("Gross Scores");
  371.   for (i=0; i<6; i++) {
  372.     gotoxy(3,5+i);
  373.     cputs(basic_msgs[i]);
  374.   }
  375. }
  376.  
  377. /* ---------------------------------------------------------------------- */
  378. void
  379. list_gross(int flight)
  380. {
  381.   int c,f,n,n0,n1,i,a,b,row=3;
  382.   int p,i1,page,new;
  383.   extern int norm_attr;
  384.   int first = 1;
  385.  
  386.   if (first) {
  387.     init_list_gross();
  388.     first = 0;
  389.   }
  390.  
  391.     n = NP[flight];
  392.     for (i=0; i<n; i++) {
  393.       gross[flight][i] = player[flight][i].gross;
  394.       if (gross[flight][i] == 0)
  395.         gross[flight][i] = 200;   /* so players with no scores will be at end */
  396.     }
  397.     gsort(flight);
  398.     window(28,1,80,25);
  399.     textattr(norm_attr);
  400.     clrscr();
  401.     n1 = n/2; if (n % 2) n1++;
  402.     gotoxy(22,1);
  403.     cprintf("FLIGHT %d",flight);
  404.     for (i=0; i<n1; i++) {
  405.       a = indx[flight][i]; b = indx[flight][i+n1];
  406.       gotoxy(2,row);
  407.       cprintf("%s",player[flight][a].name);
  408.       gotoxy(23,row); cprintf("%-3d",player[flight][a].gross);
  409.       if (i+n1 < NP[flight]) {
  410.         gotoxy(27,row); cprintf(player[flight][b].name);
  411.         gotoxy(49,row); cprintf("%-3d",player[flight][b].gross);
  412.       }
  413.       row++;
  414.     }
  415.     while (kbhit())
  416.       getch();             /* clear kb buffer */
  417.     while (1) {
  418.       switch(getkey()) {
  419.         case ESC:
  420.         case CR:
  421.           return;
  422.         case PgUp:
  423.           if (flight == 0)
  424.             flight = numflights-1;     /* largest nonempty flight */
  425.           else
  426.             flight--;
  427.           list_gross(flight); /* shift to next smaller nonempty flight */
  428.           return;
  429.         case PgDn:
  430.           if (flight == numflights-1)
  431.             flight = 0;
  432.           else
  433.             flight++;
  434.           list_gross(flight); /* shift to next larger nonempty flight */
  435.           return;
  436.       }
  437.     }
  438. }
  439.  
  440. /* ---------------------------------------------------------------------- */
  441. void
  442. init_list_matches()
  443. {
  444.   int row = 3;
  445.  
  446.   /* std_window() is a little too narrow, so use this one */
  447.   window(23,1,80,25);
  448.   textattr(norm_attr);
  449.   clrscr();
  450.   window(1,1,80,25);
  451.   textattr(7);
  452.   rect(1,1,22,25,7,1);
  453.   window(2,2,21,48);
  454.   textattr(high_attr);
  455.   clrscr();
  456.   gotoxy(4,row);
  457.   cprintf("LIST MATCHES");
  458.   row += 3;
  459.   gotoxy(2,row++);
  460.   cprintf("For each selected");
  461.   gotoxy(2,row++);
  462.   cprintf("player, the results");
  463.   gotoxy(2,row++);
  464.   cprintf("of his matches with");
  465.   gotoxy(2,row++);
  466.   cprintf("the other players");
  467.   gotoxy(2,row++);
  468.   cprintf("in his flight are");
  469.   gotoxy(2,row++);
  470.   cprintf("given. 'W' means");
  471.   gotoxy(2,row++);
  472.   cprintf("that the selected");
  473.   gotoxy(2,row++);
  474.   cprintf("player won this");
  475.   gotoxy(2,row++);
  476.   cprintf("match, 'L' that he");
  477.   gotoxy(2,row++);
  478.   cprintf("lost this one.");
  479.   row++;
  480.   gotoxy(2,row++);
  481.   cprintf("Press Esc, or Enter");
  482.   gotoxy(2,row++);
  483.   cprintf("a blank name to");
  484.   gotoxy(2,row);
  485.   cprintf("return to the menu.");
  486.   window(23,1,80,25);
  487.   textattr(norm_attr);
  488. }
  489.  
  490. /* ---------------------------------------------------------------------- */
  491. void
  492. list_matches()
  493. {
  494.   int i,j,w,row0,row,col;
  495.   int flight, n;      /* n = NP[flight] */
  496.   int m, k, cnt, r, r1;
  497.   char pname[21];
  498.   extern int norm_attr;
  499.   int flt[10],index[10];
  500.  
  501.   init_list_matches();
  502. a:
  503.   clrscr();
  504.   row = 3; col = 2;
  505.   r = get_string("Name",pname,namefield,col+8,row);
  506.   if (!r) goto ret;
  507.   k = findn(pname,flt,index);
  508.   if (k == -2) {
  509.     gotoxy(col,row+1);
  510.     cprintf("More than ten found. Press any key...");
  511.     getch();
  512.     gotoxy(col,row+1);
  513.     clreol();
  514.     goto a;
  515.   }
  516.   if (k == -1) {
  517.     gotoxy(col,row+1);
  518.     cprintf("Not found. Press any key...");
  519.     getch();
  520.     gotoxy(col,row+1);
  521.     clreol();
  522.     goto a;
  523.   }
  524.   if (k > 0)
  525.     k = pick_one(row+1, k, flt, index);
  526.   flight = flt[k];
  527.   m = index[k];
  528.   if (player[flight][m].scores[0] == 0) {
  529.     gotoxy(col,row+1);
  530.     cprintf("%s has no scores entered. Press any key ...",player[flight][m].name);
  531.     getch();
  532.     gotoxy(col,row+1);
  533.     clreol();
  534.     goto a;
  535.   }
  536.   strcpy(pname,player[flight][m].name);
  537.   asort(flight);         /* arrange players in alphabtical order */
  538.   n = NP[flight];
  539.   m = 0;
  540.   /* find pname in sorted flight */
  541.   while (strcmp(player[flight][indx[flight][m]].name,pname)) m++;
  542.   k = (n-2)/2 + 1;   /* half the number of players being compared to pname */
  543.   clrscr();
  544.   gotoxy(col,row);
  545.   cputs(pname);
  546.   gotoxy(col+21,row); cprintf("FLIGHT %d",flight);
  547.   gotoxy(col,row+1);
  548.   cprintf("Won %d Tied %d Lost %d",player[flight][indx[flight][m]].won,
  549.   player[flight][indx[flight][m]].tied, player[flight][indx[flight][m]].lost);
  550.  
  551.   calc_outcome(flight);
  552.   row0 = 5;
  553.   cnt = 0;       /* count of players compared to m */
  554.   for (j=0; j<n; j++) {
  555.     if (j==m || player[flight][j].scores[0] == 0)
  556.       continue;
  557.     cnt++;
  558.     if (cnt <= k) {
  559.       row = row0+cnt;
  560.       col = 2;
  561.     }
  562.     else {
  563.       row = row0+cnt-k;
  564.       col = 31;
  565.     }
  566.     gotoxy(col,row);
  567.     cputs(player[flight][indx[flight][j]].name);
  568.     w = outcome[indx[flight][m]][indx[flight][j]];   /* comparison array */
  569.     gotoxy(col+21,row);
  570.     if (w<0) cputs("L ");
  571.     else if (w>0) cputs("W ");
  572.     else {
  573.       cputs("Tied");
  574.       continue;
  575.     }
  576.     r1 = abs(w);
  577.     if (r1 == 1) cputs("1 up");
  578.     else if (r1 == 12) cputs("2 up");
  579.     else if (r1<10) cprintf("%d&%d",r1,r1-1);
  580.     else if (r1>11) cprintf("%d&%d",r1-10,r1-12);
  581.   }
  582.   gotoxy(3,20); cputs("Press any key to continue");
  583.   getch();
  584.   goto a;
  585. ret:
  586.   clrscr();
  587. }
  588.  
  589. /* ---------------------------------------------------------------------- */
  590. void
  591. init_list_net()
  592. {
  593.   int i;
  594.  
  595.   std_screen();
  596.   gotoxy(8,3);
  597.   cprintf("Net Scores");
  598.   for (i=0; i<6; i++) {
  599.     gotoxy(4,5+i);
  600.     cputs(basic_msgs[i]);
  601.   }
  602. }
  603.  
  604. /* ---------------------------------------------------------------------- */
  605. void
  606. list_net(int flight)
  607. {
  608.   int c,f,n,n0,n1,i,a,b,row=3;
  609.   int p,j,page,new;
  610.   extern int norm_attr;
  611.   int first = 1;
  612.  
  613.   if (first) {
  614.     init_list_net();
  615.     first = 0;
  616.   }
  617.  
  618.     n = NP[flight];
  619.     for (i=0; i<n; i++) {
  620.       net[flight][i] = player[flight][i].net;
  621.       if (net[flight][i] == 0)
  622.         net[flight][i] = 200;   /* so players with no scores will be at end */
  623.     }
  624.     nsort(flight);
  625.     window(28,1,80,25);
  626.     textattr(norm_attr);
  627.     clrscr();
  628.     n1 = n/2; if (n % 2) n1++;
  629.     gotoxy(22,1);
  630.     cprintf("FLIGHT %d",flight);
  631.     for (i=0; i<n1; i++) {
  632.       a = indx[flight][i]; b = indx[flight][i+n1];
  633.       gotoxy(2,row);
  634.       cprintf("%s",player[flight][a].name);
  635.       gotoxy(23,row); cprintf("%-3d",player[flight][a].net);
  636.       if (i+n1 < NP[flight]) {
  637.         gotoxy(27,row); cprintf(player[flight][b].name);
  638.         gotoxy(49,row); cprintf("%-3d",player[flight][b].net);
  639.       }
  640.       row++;
  641.     }
  642.     while (kbhit())
  643.       getch();             /* clear kb buffer */
  644.     while (1) {
  645.       switch(getkey()) {
  646.         case ESC:
  647.         case CR:
  648.           return;
  649.         case PgUp:
  650.           if (flight == 0)
  651.             flight = numflights-1;     /* largest nonempty flight */
  652.           else
  653.             flight--;
  654.           list_net(flight); /* shift to next smaller nonempty flight */
  655.           return;
  656.         case PgDn:
  657.           if (flight == numflights-1)
  658.             flight = 0;
  659.           else
  660.             flight++;
  661.           list_net(flight); /* shift to next larger nonempty flight */
  662.           return;
  663.         }
  664.     }
  665. }
  666.  
  667. /* ---------------------------------------------------------------------- */
  668. void
  669. init_list_points()
  670. {
  671.   int i;
  672.  
  673.   std_screen();
  674.   gotoxy(8,3);
  675.   cprintf("Point System");
  676.   for (i=0; i<6; i++) {
  677.     gotoxy(4,5+i);
  678.     cputs(basic_msgs[i]);
  679.   }
  680. }
  681.  
  682. /* ---------------------------------------------------------------------- */
  683. void
  684. list_points(int flight)
  685. {
  686.   int c,f,n,n0,n1,i,a,b,row=3;
  687.   int p,j,page,new;
  688.   extern int norm_attr;
  689.   int first = 1;
  690.  
  691.   if (first) {
  692.     init_list_points();
  693.     first = 0;
  694.   }
  695.  
  696.     n = NP[flight];
  697.     for (i=0; i<n; i++) {
  698.       points[flight][i] = player[flight][i].points;
  699.     }
  700.     psort(flight);
  701.     window(28,1,80,25);
  702.     textattr(norm_attr);
  703.     clrscr();
  704.     n1 = n/2; if (n % 2) n1++;
  705.     gotoxy(22,1);
  706.     cprintf("FLIGHT %d",flight);
  707.     for (i=0; i<n1; i++) {
  708.       a = indx[flight][i]; b = indx[flight][i+n1];
  709.       gotoxy(2,row);
  710.       cprintf("%s",player[flight][a].name);
  711.       gotoxy(23,row); cprintf("%-3d",player[flight][a].points);
  712.       if (i+n1 < NP[flight]) {
  713.         gotoxy(27,row); cprintf(player[flight][b].name);
  714.         gotoxy(49,row); cprintf("%-3d",player[flight][b].points);
  715.       }
  716.       row++;
  717.     }
  718.     while (kbhit())
  719.       getch();             /* clear kb buffer */
  720.     while (1) {
  721.       switch(getkey()) {
  722.         case ESC:
  723.         case CR:
  724.           return;
  725.         case PgUp:
  726.           if (flight == 0)
  727.             flight = numflights-1;     /* largest nonempty flight */
  728.           else
  729.             flight--;
  730.           list_points(flight); /* shift to next smaller nonempty flight */
  731.           return;
  732.         case PgDn:
  733.           if (flight == numflights-1)
  734.             flight = 0;
  735.           else
  736.             flight++;
  737.           list_points(flight); /* shift to next larger nonempty flight */
  738.           return;
  739.         }
  740.     }
  741. }
  742.  
  743. /* ---------------------------------------------------------------------- */
  744. void
  745. init_list_results()
  746. {
  747.   int i;
  748.  
  749.   std_screen();
  750.   gotoxy(4,2);
  751.   cprintf("MATCH PLAY RESULTS");
  752.   gotoxy(1,3);
  753.   cprintf("(win = pt, tie = 1/2 pt)");
  754.   for (i=0; i<6; i++) {
  755.     gotoxy(3,5+i);
  756.     cputs(basic_msgs[i]);
  757.   }
  758. }
  759.  
  760. /* ---------------------------------------------------------------------- */
  761. void
  762. list_results(int flight)
  763. {
  764.   int c,i,f,a,row;
  765.   extern int norm_attr;
  766.   int first = 1;
  767.   if (first) {
  768.     init_list_results();        /* fixed messages */
  769.     first = 0;
  770.   }
  771.   window(28,1,80,25);
  772.   textattr(norm_attr);
  773.   msort(flight);
  774.   clrscr(); row = 1;
  775.   gotoxy(7,row);
  776.   cprintf("Flight %d",flight);
  777.   gotoxy(23,row);
  778.   cprintf("Won    Tied  Lost   Points");
  779.   for (i=0; i<NP[flight]; i++) {
  780.     row++;
  781.     a = indx[flight][i];
  782.     gotoxy(2,row);
  783.     cprintf("%s",player[flight][a].name);
  784.     gotoxy(24,row);
  785.     cprintf("%-3d",player[flight][a].won);
  786.     gotoxy(30,row);
  787.     cprintf("%-3d",player[flight][a].tied);
  788.     gotoxy(36,row);
  789.     cprintf("%-3d",player[flight][a].lost);
  790.     gotoxy(45,row);
  791.     cprintf("%-3d",player[flight][a].won + player[flight][a].tied/2);
  792.     if (player[flight][a].tied % 2) cputs("1/2");
  793.   }
  794.   while (kbhit())
  795.     getch();              /* clear kb buffer */
  796.   while (1) {
  797.     switch(getkey()) {
  798.       case ESC:
  799.       case CR:
  800.         return;
  801.       case PgUp:
  802.         if (flight == 0)
  803.           flight = numflights-1;
  804.         else
  805.           flight--;
  806.         list_results(flight); /* shift to next smaller nonempty flight */
  807.         return;
  808.       case PgDn:
  809.         if (flight == numflights-1)
  810.           flight = 0;
  811.         else
  812.           flight++;
  813.         list_results(flight); /* shift to next larger nonempty flight */
  814.         return;
  815.     }
  816.   }
  817. }
  818.  
  819. /* ---------------------------------------------------------------------- */
  820. void
  821. nsort(int flight)
  822. {
  823.   int i,j,temp,sz;
  824.  
  825.   sz = NP[flight];
  826.   for (i=0; i<sz; i++) indx[flight][i] = i;
  827.  
  828.   for (i=1; i<sz; i++) {
  829.     temp = indx[flight][i];
  830.     j = i-1;
  831.     while(j>=0) {
  832.       if (net[flight][indx[flight][j]] > net[flight][temp]
  833.        || (net[flight][indx[flight][j]] == net[flight][temp] &&
  834.        strcmp(player[flight][indx[flight][j]].name,player[flight][temp].name)>0)) {
  835.         indx[flight][j+1] = indx[flight][j];
  836.         j--;
  837.       }
  838.       else
  839.         break;
  840.     }
  841.     indx[flight][j+1] = temp;
  842.   }
  843. }
  844.  
  845. /* ---------------------------------------------------------------------- */
  846. void
  847. psort(int flight)
  848. {
  849.   int i,j,temp,sz;
  850.  
  851.   sz = NP[flight];
  852.   for (i=0; i<sz; i++) indx[flight][i] = i;
  853.  
  854.   for (i=1; i<sz; i++) {
  855.     temp = indx[flight][i];
  856.     j = i-1;
  857.     while(j>=0) {
  858.       if (points[flight][indx[flight][j]] < points[flight][temp]
  859.        || (points[flight][indx[flight][j]] == points[flight][temp] &&
  860.        strcmp(player[flight][indx[flight][j]].name,player[flight][temp].name)>0)) {
  861.         indx[flight][j+1] = indx[flight][j];
  862.         j--;
  863.       }
  864.       else
  865.         break;
  866.     }
  867.     indx[flight][j+1] = temp;
  868.   }
  869. }
  870.  
  871. /* ---------------------------------------------------------------------- */
  872. void
  873. print_gross(int flight)
  874. {
  875.   int c,n,n1,i,a,b,pos;
  876.  
  877.   n = NP[flight];
  878.   for (i=0; i<n; i++) {
  879.     gross[flight][i] = player[flight][i].gross;
  880.     if (gross[flight][i] ==0)
  881.       gross[flight][i] = 200;  /* so players with no scores will be at end */
  882.   }
  883.   gsort(flight);
  884.   for (i=0; i<n; i++)
  885.     if (gross[flight][i] == 200)
  886.       gross[flight][i] = 0;     /* reset gross to 0 for players without scores */
  887.   n1 = n/2; if (n % 2) n1++;
  888.     fprintf(stdprn,"\n\n\n");
  889.     prnspaces(30);
  890.     fprintf(stdprn,"Gross Scores, Flight %d\n\n",flight);
  891.   for (i=0; i<n1; i++) {
  892.     pos = 0;             /* 'pos' is position of printer on current line */
  893.     a = indx[flight][i]; b = indx[flight][i+n1];
  894.     prnspaces(8); pos = 8;
  895.     fprintf(stdprn,"%s",player[flight][a].name);
  896.     pos += strlen(player[flight][a].name);
  897.     prnspaces(31 - pos); pos = 31;
  898.     fprintf(stdprn,"%-3d",player[flight][a].gross);
  899.     pos += 3;
  900.     if (i+n1 < NP[flight]) {
  901.       prnspaces(12); pos += 12;
  902.       fprintf(stdprn,player[flight][b].name);
  903.       pos += strlen(player[flight][b].name);
  904.       prnspaces(69-pos);
  905.       fprintf(stdprn,"%-3d",player[flight][b].gross);
  906.     }
  907.     fprintf(stdprn,"\n");
  908.   }
  909. }
  910.  
  911. /* ---------------------------------------------------------------------- */
  912. void
  913. print_net(int flight)
  914. {
  915.   int c,n,n1,i,a,b,pos;
  916.  
  917.   n = NP[flight];
  918.   for (i=0; i<n; i++)
  919.     if (player[flight][i].gross == 0)
  920.       net[flight][i] = 200; /* so players without scores will be at end */
  921.     else
  922.       net[flight][i] = player[flight][i].gross - player[flight][i].hcp;
  923.   nsort(flight);
  924.   for (i=0; i<n; i++)
  925.     if (net[flight][i] == 200)
  926.       net[flight][i] = 0;    /* reset net[] for players with no scores or hcps */
  927.   n1 = n/2; if (n % 2) n1++;
  928.     fprintf(stdprn,"\n\n\n");
  929.     prnspaces(32);
  930.     fprintf(stdprn,"Net Scores, Flight %d\n\n",flight);
  931.   for (i=0; i<n1; i++) {
  932.     pos = 0;             /* 'pos' is position of printer on current line */
  933.     a = indx[flight][i]; b = indx[flight][i+n1];
  934.     prnspaces(8); pos = 8;
  935.     fprintf(stdprn,"%s",player[flight][a].name);
  936.     pos += strlen(player[flight][a].name);
  937.     prnspaces(31 - pos); pos = 31;
  938.     fprintf(stdprn,"%-3d",player[flight][a].net);
  939.     pos += 3;
  940.     if (i+n1 < NP[flight]) {
  941.       prnspaces(12); pos += 12;
  942.       fprintf(stdprn,player[flight][b].name);
  943.       pos += strlen(player[flight][b].name);
  944.       prnspaces(69-pos);
  945.       fprintf(stdprn,"%-3d",player[flight][b].net);
  946.     }
  947.     fprintf(stdprn,"\n");
  948.   }
  949. }
  950.  
  951. /* ---------------------------------------------------------------------- */
  952. void
  953. print_points(int flight)
  954. {
  955.   int c,n,n1,i,a,b,pos;
  956.  
  957.   n = NP[flight];
  958.   for (i=0; i<n; i++)
  959.       points[flight][i] = player[flight][i].points;
  960.   psort(flight);
  961.   n1 = n/2; if (n % 2) n1++;
  962.     fprintf(stdprn,"\n\n\n");
  963.     prnspaces(32);
  964.     fprintf(stdprn,"Point System, Flight %d\n\n",flight);
  965.   for (i=0; i<n1; i++) {
  966.     pos = 0;             /* 'pos' is position of printer on current line */
  967.     a = indx[flight][i]; b = indx[flight][i+n1];
  968.     prnspaces(8); pos = 8;
  969.     fprintf(stdprn,"%s",player[flight][a].name);
  970.     pos += strlen(player[flight][a].name);
  971.     prnspaces(31 - pos); pos = 31;
  972.     fprintf(stdprn,"%-3d",player[flight][a].points);
  973.     pos += 3;
  974.     if (i+n1 < NP[flight]) {
  975.       prnspaces(12); pos += 12;
  976.       fprintf(stdprn,player[flight][b].name);
  977.       pos += strlen(player[flight][b].name);
  978.       prnspaces(69-pos);
  979.       fprintf(stdprn,"%-3d",player[flight][b].points);
  980.     }
  981.     fprintf(stdprn,"\n");
  982.   }
  983. }
  984.  
  985. /* ---------------------------------------------------------------------- */
  986. void
  987. print_matches(int flight)
  988. {
  989.   int f = flight;
  990.   int i,j,j1,j2,cnt,n,m,k,z;
  991.   char s[27],result[24][27];
  992.   char pname[21], ext[3];
  993.   FILE *fp;
  994.   char filename[11] = "matches.";
  995.   char command[20] = "copy ";
  996.   char divider[] =
  997. "--------------------------------------------------------------------------";
  998.  
  999.   strcat(filename,itoa(flight,ext,10));
  1000.   strcat(command,filename);
  1001.   strcat(command," PRN");
  1002.   fp = fopen(filename,"w");
  1003.  
  1004.   asort(flight);
  1005.   calc_outcome(flight);
  1006.   n = NP[flight];
  1007.   m = n/2;           /*  m = rows  needed to list matches in 2 columns */
  1008.   k = 63/(m+2)-1;    /*  number of complete data sets per 66-line page */
  1009.  
  1010.   for (i=0; i<n; i++) {
  1011.     if (i % k == 0) {                    /* start new page */
  1012.       fprintf(fp,"%c",12);               /* form feed for printer */
  1013.       fprintf(fp,"\n");
  1014.       fprnspaces(fp,35);
  1015.       fprintf(fp,"FLIGHT %d\n",flight);  /* page header */
  1016.     }
  1017.     if (player[f][indx[f][i]].scores[0] == 0)
  1018.       continue;                         /* ignore players w/o scores */
  1019.     strcpy(pname,player[flight][indx[flight][i]].name);
  1020.     fprintf(fp,"\n%s",strupr(pname));
  1021.     fprnspaces(fp,22-strlen(pname));
  1022.     fprintf(fp,"Won %d  Tied %d  Lost %d", player[f][indx[f][i]].won,
  1023.        player[f][indx[f][i]].tied, player[f][indx[f][i]].lost);
  1024.     z = 0;
  1025.     for (j=0; j<n; j++) {
  1026.       if (j == i || player[f][indx[f][j]].scores[0] == 0)
  1027.         continue;
  1028.       make_resultstring(flight,i,j,s);
  1029.       strcpy(result[z],s);
  1030.       z++;
  1031.     }
  1032.     for (z=0; z<m; z++) {
  1033.       fprintf(fp,"\n");
  1034.       fprintf(fp,"   %s",result[z]);
  1035.       if (z+m < n-1) {fprnspaces(fp,15); fprintf(fp,"%s",result[z+m]);}
  1036.     }
  1037.     z = fprintf(fp, "\n%s\n",divider);
  1038.   }
  1039.   fclose(fp);
  1040.   system(command);
  1041. }
  1042.  
  1043. /* ---------------------------------- */
  1044. int
  1045. make_resultstring(int flight, int i, int j, char *s)
  1046. {
  1047.   int k,w;
  1048.   char *p, *q;
  1049. int len;
  1050.  
  1051.   for (k=0; k<26; k++)
  1052.     s[k] = ' ';
  1053.   s[27] = '\0';
  1054.   p = player[flight][indx[flight][j]].name;
  1055.   q = s;
  1056.   while (*p)
  1057.     *q++ = *p++;
  1058.   q = s+20;
  1059.   w = outcome[i][j];
  1060.   if (w > 0)
  1061.     *q++ = 'W';
  1062.   else if (w < 0) {
  1063.     *q++ = 'L';
  1064.     w = -w;
  1065.   }
  1066.   q++;
  1067.   *q = '\0';
  1068.   strcat(s,outcomestring[w]);
  1069. len = strlen(s);
  1070. return len;
  1071. }
  1072.  
  1073. /* ---------------------------------------------------------------------- */
  1074. void
  1075. print_results(int flight)
  1076. {
  1077.   int n,n0,n1,i,temp,a,b;
  1078.  
  1079.   msort(flight);
  1080.   n = NP[flight]; n0 = n1 = 0;
  1081.  
  1082.   while(n1 < n) {
  1083.     fprintf(stdprn,"\n\n\n");
  1084.     prnspaces(33);
  1085.     fprintf(stdprn,"FLIGHT %d\n",flight);
  1086.     if (n1 > 0) fprintf(stdprn," (continued)");
  1087.     fprintf(stdprn,"\n\n");
  1088.     n1 = n0 + 50; if (n1 > n) n1 = n;
  1089.     prnspaces(32);
  1090.     fprintf(stdprn,"Won    Tied      Lost      Points\n");
  1091.     for (i=n0; i<n1; i++) {
  1092.       a = indx[flight][i];
  1093.       prnspaces(13);
  1094.       fprintf(stdprn,player[flight][a].name);
  1095.       prnspaces(20 - strlen(player[flight][a].name));
  1096.       fprintf(stdprn,"%-3d",player[flight][a].won);
  1097.       prnspaces(7);
  1098.       fprintf(stdprn,"%-3d",player[flight][a].tied);
  1099.       prnspaces(7);
  1100.       fprintf(stdprn,"%-3d",player[flight][a].lost);
  1101.       prnspaces(7);
  1102.       fprintf(stdprn,"%-3d",player[flight][a].won + player[flight][a].tied/2);
  1103.       if (player[flight][a].tied % 2) fprintf(stdprn,"1/2");
  1104.       fprintf(stdprn,"\n");
  1105.     }
  1106.     biosprint(0,12,0);                            /* send formfeed to printer */
  1107.     n0 = n1;
  1108.   }
  1109. }
  1110.  
  1111. /* ---------------------------------------------------------------------- */
  1112. void
  1113. prnspaces(int n)
  1114. {
  1115.    int i;
  1116.  
  1117.    for (i=0; i<n; i++)
  1118.      fprintf(stdprn," ");
  1119. }
  1120.  
  1121. /* ---------------------------------------------------------------------- */
  1122. void
  1123. fprnspaces(FILE *fp, int n)
  1124. {
  1125.    int i;
  1126.  
  1127.    for (i=0; i<n; i++)
  1128.      fprintf(fp," ");
  1129. }
  1130.  
  1131. /* ---------------------------------------------------------------------- */
  1132. void
  1133. msort(int flight)
  1134. {
  1135.   int i,j,temp,sz,mp[MAXSZ];
  1136.  
  1137.   sz = NP[flight];
  1138.   for (i=0; i<sz; i++) indx[flight][i] = i;
  1139.  
  1140.   for (i=0; i<sz; i++) {
  1141.     if (player[flight][i].won != 0 || player[flight][i].tied != 0)
  1142.       mp[i] = 2*player[flight][i].won + player[flight][i].tied;
  1143.     else if (player[flight][i].won == 0 && player[flight][i].tied == 0 &&
  1144.              player[flight][i].lost != 0)
  1145.       mp[i] = 0;
  1146.     else
  1147.       mp[i] = -1;
  1148.   }
  1149.   for (i=0; i<sz-1; i++) {
  1150.     for (j=i+1; j<sz; j++) {
  1151.       if (mp[indx[flight][i]] < mp[indx[flight][j]]) {
  1152.         temp = indx[flight][i];
  1153.         indx[flight][i] = indx[flight][j];
  1154.         indx[flight][j] = temp;
  1155.       }
  1156.     }
  1157.   }
  1158. }
  1159.  
  1160. /* ----------------------------------------------------------------------- */
  1161. void
  1162. strokes_msg(int k,int col, int row)
  1163. {
  1164.    int h;
  1165.    int difficult[18];     /* holes in order of decreasing difficulty */
  1166.  
  1167.    for (h=0; h<18; h++) difficult[order[h]-1] = h+1;
  1168.  
  1169.    gotoxy(col,row);
  1170.    if (k == 1)
  1171.      cprintf("one stroke, on hole %d", difficult[0]);
  1172.    else if (k < 18) {
  1173.      cprintf("%d strokes, on holes ",k);
  1174.      for (h=0; h<k; h++) {
  1175.        if (h == 10) gotoxy(col+21,++row);
  1176.        cprintf("%d ",difficult[h]);
  1177.      }
  1178.    }
  1179.    else if (k == 18)
  1180.      cputs("one stroke on each hole.");
  1181.    else if (k < 36) {
  1182.      cputs("two strokes on ");
  1183.      for (h=0; h<k-18; h++) {
  1184.        if (h == 12) gotoxy(col+15,++row);
  1185.        cprintf("%d ",difficult[h]);
  1186.      }
  1187.      gotoxy(col,row+1);
  1188.      cputs("one stroke on all other holes.");
  1189.    }
  1190.    else if (k == 36)
  1191.      cputs("two strokes on each hole.");
  1192.    else {
  1193.      cputs("three strokes on ");
  1194.      for (h=0; h<k-36; h++)
  1195.        cprintf("%d ",difficult[h]);
  1196.      gotoxy(col,row+1);
  1197.      cputs("two strokes on all other holes.");
  1198.    }
  1199. }
  1200.  
  1201. /* ---------------------------------------------------------------------- */
  1202. void
  1203. show_data(int flight, int index, int row)
  1204. {
  1205.   int i,j,k;
  1206.  
  1207.     j = indx[flight][index];
  1208.     gotoxy(2,row);
  1209.     cputs(player[flight][j].name);
  1210.     gotoxy(24,row);
  1211.     cprintf("%d",player[flight][j].hcp);
  1212.     gotoxy(28,row);
  1213.     if (player[flight][j].scores[0])
  1214.       cputs(player[flight][j].scores);
  1215.     else
  1216.       cputs("");
  1217. }
  1218.  
  1219. /* ---------------------------------------------------------------------- */
  1220. void
  1221. write_data(FILE *fp)
  1222. {
  1223.    int i,j,k;
  1224.    extern int par[];
  1225.    extern char data_title[];
  1226.  
  1227.    fprintf(fp,"%s\n\n",data_title);
  1228.    for (i=0; i<18; i++)
  1229.      fprintf(fp,"%d ",par[i]);
  1230.    fprintf(fp,"\n");
  1231.    for (i=0; i<18; i++)
  1232.      fprintf(fp,"%d ",order[i]);
  1233.    fprintf(fp,"\n\n");
  1234.    for (k=0; k<MAXFL; k++) {
  1235.      if (!NP[k]) continue;
  1236.      asort(k);
  1237.      for (i=0; i<NP[k]; i++) {
  1238.        j = indx[k][i];
  1239.        fprintf(fp,"%s\n",player[k][j].name);
  1240.        fprintf(fp,"%d\n",player[k][j].hcp);
  1241.        if (player[k][j].scores[0])
  1242.          fprintf(fp,"%s\n",player[k][j].scores);
  1243.        else
  1244.          fprintf(fp,"No Scores\n");
  1245.      }
  1246.      fprintf(fp," \n");
  1247.    }
  1248. }
  1249.  
  1250.