/* MP_OUT.C  Various display and print routines */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
#include "defines.h"

/* macro for calculating adjustment on hole h (x = hcp[a]-hcp[b]-order[h]) */
#define correction(x)  (x)<0 ? 0 : (x)<18 ? 1 : (x)<36 ? 2 : 3
extern PLAYER player[MAXFL][MAXSZ];

extern int numflights,scol,srow,width;
extern short indx[MAXFL][MAXSZ],outcome[MAXSZ][MAXSZ];
extern int NP[],ndx[],order[];
extern char namefield[],mark[];

/* function prototypes */
void
  asort(int),
  compare_two(void),
  fprnspaces(FILE *,int),
  gsort(int),
  init_compare(void),
  init_list_gross(void),
  init_list_matches(void),
  init_list_net(void),
  init_list_results(void),
  list_gross(int),
  list_net(int),
  list_matches(void),
  list_results(int),
  msort(int),
  nsort(int),
  psort(int),
  print_gross(int),
  print_net(int),
  print_points(int),
  print_matches(int),
  print_results(int),
  prnspaces(int),
  strokes_msg(int,int,int);
int
  make_resultstring(int,int,int,char *);
extern void
  calc_outcome(int);
extern int
  findn(char *, int *, int *),
  get_string(char *,char *,char *,int,int),
  pick_one(int, int, int *, int *);

static char *outcomestring[21] =
  {" Tied","1 up","2&1 ","3&2 ","4&3 ","5&4 ","6&5 ","7&6 ","8&7 ","9&8 ",
    "","","2 up","3&1 ","4&2 ","5&3 ","6&4 ","7&5 ","8&6 ","9&7 ","10&8"};

/* indexing arrays */
short indx[MAXFL][MAXSZ];             /* for indexing flights */
short I[MAXFL*MAXSZ],J[MAXFL*MAXSZ];  /* for indexing total field */

int gross[MAXFL][MAXSZ], net[MAXFL][MAXSZ]; /* gross and net scores */
int points[MAXFL][MAXSZ];                   /* point system points */
char *basic_msgs[] = {    /* msgs used in list_xxx() functions */
        "PgUp : Prev Flight",
        "PgDn : Next Flight",
        "",
        "",
        "Press Esc or Enter",
        "to return to menu."
     };

/* ---------------------------------------------------------------------- */
void
asort(int flight)
{
  int i,j,temp,sz;

  sz = NP[flight];
  for (i=0; i<sz; i++) indx[flight][i] = i;

  for (i=1; i<sz; i++) {
    temp = indx[flight][i];
    j = i-1;
    while(j>=0) {
      if (strcmp(player[flight][indx[flight][j]].name,player[flight][temp].name)>0) {
        indx[flight][j+1] = indx[flight][j];
        j--;
      }
      else
        break;
    }
    indx[flight][j+1] = temp;
  }
}

/* ---------------------------------------------------------------------- */
void
init_compare()
{
  int i;

  std_screen();
  gotoxy(7,3);
  cprintf("Compare Two");
  gotoxy(3,5);
  cprintf("Any two players, not");
  gotoxy(3,6);
  cprintf("necessarily in the");
  gotoxy(3,7);
  cprintf("same flight, may be");
  gotoxy(3,8);
  cprintf("compared at match play.");
  gotoxy(3,10);
  cprintf("In the list of net");
  gotoxy(3,11);
  cprintf("scores, the winning");
  gotoxy(3,12);
  cprintf("score on each hole is");
  gotoxy(3,13);
  cprintf("highlighted. Only the");
  gotoxy(3,14);
  cprintf("holes needed to decide");
  gotoxy(3,15);
  cprintf("the match are printed.");
  gotoxy(3,17);
  cprintf("To return to the menu,");
  gotoxy(3,18);
  cprintf("press Esc or Enter.");
}

/* ---------------------------------------------------------------------- */
void
compare_two(void)
{
  char name1[21], name2[21];
  int r,r1,i,m1,m2,k,a,b,h,diff;
  int col=2,row=3;
  int i0,w,flight1,flight2,flighta,flightb;
  int sa[18], sb[18], net[18],x, y;            /* gross and net scores */
  char surname_a[21],surname_b[21],*p,*q;
  extern int norm_attr, high_attr;
  int hcp;
  int flt[10],index[10];

  init_compare();
  window(28,1,80,25);
  textattr(norm_attr);
a:
  clrscr();
  r = get_string("Name1",name1,namefield,col+8,row);
  if (!r) goto ret;
  k = findn(name1,flt,index);
  if (k == -2) {
    gotoxy(col,row+1);
    cprintf("More than ten found. Press any key...");
    getch();
    goto a;
  }
  if (k == -1) {
    gotoxy(col,row+1);
    cprintf("Name not found. Press any key...");
    getch();
    goto a;
  }
  if (k > 0)
    k = pick_one(row+1, k, flt, index);
  flight1 = flt[k];
  m1 = index[k];
  if (player[flight1][m1].scores[0] == 0) {
    gotoxy(col,row+1);
    cprintf("%s has incomplete data.",player[flight1][m1].name);
    getch();
    goto a;
  }
  gotoxy(col+8,row);
  clreol();
  cputs(player[flight1][m1].name);
b:
  gotoxy(col,row+1); clreol();
  r = get_string("Name2",name2,namefield,col+8,row+1);
  if (!r) goto ret;
  k = findn(name2,flt,index);
  if (k == -2) {
    gotoxy(col,row+2);
    cprintf("More than ten found. Press any key...");
    getch();
    gotoxy(col,row+2); clreol();
    goto b;
  }
  if (k == -1) {
    gotoxy(col,row+2);
    cprintf("Name not found. Press any key...");
    getch();
    gotoxy(col,row+2); clreol();
    goto b;
  }
  if (k > 0)
    k = pick_one(row+2, k, flt, index);
  flight2 = flt[k];
  m2 = index[k];
  if (player[flight2][m2].scores[0] == 0) {
    gotoxy(col,row+2);
    cprintf("%s has incomplete data.",player[flight2][m2].name);
    getch();
    gotoxy(col,row+2); clreol();
    goto b;
  }

  k = player[flight1][m1].hcp - player[flight2][m2].hcp;
  if (k == 0) {
    i = strcmp(player[flight1][m1].name,player[flight2][m2].name);
    if (i == 0)
      goto b;                /* player duplicated */
    else if (i > 0) {        /* list alphabetically if same hcp */
      a = m1, b = m2;
      flighta = flight1, flightb = flight2;
    }
    else {
      a = m2, b = m1;
      flighta = flight2, flightb = flight1;
    }
  }
  else if (k > 0) {
    a=m1, b=m2;
    flighta = flight1, flightb = flight2;
  }
  else if (k < 0) {
    k = -k;
    a=m2, b=m1;
    flighta = flight2, flightb = flight1;
  }
  p = player[flighta][a].name;
  q = surname_a;
  do *q++ = *p++; while (*p && *p != ',');
  *q = '\0';
  p = player[flightb][b].name;
  q = surname_b;
  do *q++ = *p++; while (*p && *p != ',');
  *q = '\0';
  /* if surnames are identical, use full names */
  if (strcmp(surname_a,surname_b) == 0) {
    strcpy(surname_a,player[flighta][a].name);
    strcpy(surname_b,player[flightb][b].name);
  }
  clrscr();
  gotoxy(col,row);
  cputs(player[flightb][b].name);
  gotoxy(col+21,row); cprintf("hcp: %d",player[flightb][b].hcp);
  gotoxy(col,row+1);
  cputs(player[flighta][a].name);
  gotoxy(col+21,row+1); cprintf("hcp: %d",player[flighta][a].hcp);
  gotoxy(col,row+2);
  if (k == 0)
    cprintf("Neither player receives any strokes.");
  else {
    cprintf("Handicap correction:");
    strokes_msg(k,col,row+3);
  }
  gotoxy(col+2, row+8);
  cputs("Gross scores:");
  gotoxy(col,row+9);
  cputs(player[flightb][b].name);
  gotoxy(col+21,row+9);
  cputs(player[flightb][b].scores);
  gotoxy(col,row+10);
  cputs(player[flighta][a].name);
  gotoxy(col+21,row+10);
  cprintf("%s",player[flighta][a].scores);
      for (h=0; h<18; h++) sa[h] = player[flighta][a].scores[ndx[h]]-48;
      for (h=0; h<18; h++) sb[h] = player[flightb][b].scores[ndx[h]]-48;
      k = player[flighta][a].hcp - player[flightb][b].hcp;
                                   /* k is non-negative, by choice of a,b */
      i0 = 17;               /* i0 = number of last hole that counts here */
      w = 0;                       /* w counts net wins (+ for a, - for b) */
      for (h=0; h<18; h++) {
        diff = correction(k-order[h]);  /* diff = 0,1,2,3 = advantage to a */
        x = sa[h] - diff;               /* x = a's net score relative to b */
        net[h] = x>0 ? x : 0;           /* don't display negative net scores */
        y = sb[h];                      /* since b has lower hcp, no change */
        w += (y-x>0 ? 1 : y==x ? 0 : -1);
        if (h<17 && abs(w)>17-h) {
          i0 = h;
          break;
        }
      }
    if (w==19-i0) w += 10;  /* to distinguish 'w and w-1' from 'w and w-2'; */
    if (-w==19-i0) w -= 10; /* e.g., |w|=3 -> 3&1 while |w|=13 -> 3&2 */
  gotoxy(col+2,row+12);
  cputs("Net scores:");
  gotoxy(col,row+13);
  cputs(player[flightb][b].name);
  gotoxy(29,row+10);
  for (i=0; i<=i0; i++) {
    gotoxy(col+21+ndx[i],row+13);
    if (sb[i] < net[i])
      textattr(high_attr);
    else
      textattr(norm_attr);
    cprintf("%d",sb[i]);
  }
  textattr(norm_attr);            /* in case changed for last score */
  gotoxy(col,row+14);
  cputs(player[flighta][a].name);
  gotoxy(col+21,row+14);
  for (i=0; i<=i0; i++) {
    gotoxy(col+21+ndx[i],row+14);
    if (net[i] < sb[i])
      textattr(high_attr);
    else
      textattr(norm_attr);
    cprintf("%d",net[i]);
  }
  textattr(norm_attr);            /* in case changed for last score */
  r1 = abs(w);
  gotoxy(col,row+16);
  if (w==0) {
    cprintf("Tied");
    getch();
    goto a;
  }
  else if (w>0) cprintf("%s over %s, ",surname_a,surname_b);
  else if (w<0) cprintf("%s over %s, ",surname_b,surname_a);
  if (r1 == 1) cputs("1 up");
  else if (r1 == 12) cputs("2 up");
  else if (r1<10) cprintf("%d and %d",r1,r1-1);
  else if (r1>12) cprintf("%d and %d",r1-10,r1-12);
  if (getch() == ESC) goto ret;
  goto a;
  ret:
  return;
}

/* ---------------------------------------------------------------------- */
void
gsort(int flight)
{
  int i,j,temp,sz;
  int g[MAXSZ];

  sz = NP[flight];
  for (i=0; i<sz; i++)  indx[flight][i] = i;

  for (i=1; i<sz; i++) {
    temp = indx[flight][i];
    j = i-1;
    while(j>=0) {
      if (gross[flight][indx[flight][j]] > gross[flight][temp]
        || (gross[flight][indx[flight][j]] == gross[flight][temp] &&
        strcmp(player[flight][indx[flight][j]].name,player[flight][temp].name)>0)) {
        indx[flight][j+1] = indx[flight][j];
        j--;
      }
      else
        break;
    }
    indx[flight][j+1] = temp;
  }
}

/* ---------------------------------------------------------------------- */
void
init_list_gross()
{
  int i;

  std_screen();
  gotoxy(10,5);
  gotoxy(7,3);
  cprintf("Gross Scores");
  for (i=0; i<6; i++) {
    gotoxy(3,5+i);
    cputs(basic_msgs[i]);
  }
}

/* ---------------------------------------------------------------------- */
void
list_gross(int flight)
{
  int c,f,n,n0,n1,i,a,b,row=3;
  int p,i1,page,new;
  extern int norm_attr;
  int first = 1;

  if (first) {
    init_list_gross();
    first = 0;
  }

    n = NP[flight];
    for (i=0; i<n; i++) {
      gross[flight][i] = player[flight][i].gross;
      if (gross[flight][i] == 0)
        gross[flight][i] = 200;   /* so players with no scores will be at end */
    }
    gsort(flight);
    window(28,1,80,25);
    textattr(norm_attr);
    clrscr();
    n1 = n/2; if (n % 2) n1++;
    gotoxy(22,1);
    cprintf("FLIGHT %d",flight);
    for (i=0; i<n1; i++) {
      a = indx[flight][i]; b = indx[flight][i+n1];
      gotoxy(2,row);
      cprintf("%s",player[flight][a].name);
      gotoxy(23,row); cprintf("%-3d",player[flight][a].gross);
      if (i+n1 < NP[flight]) {
        gotoxy(27,row); cprintf(player[flight][b].name);
        gotoxy(49,row); cprintf("%-3d",player[flight][b].gross);
      }
      row++;
    }
    while (kbhit())
      getch();             /* clear kb buffer */
    while (1) {
      switch(getkey()) {
        case ESC:
        case CR:
          return;
        case PgUp:
          if (flight == 0)
            flight = numflights-1;     /* largest nonempty flight */
          else
            flight--;
          list_gross(flight); /* shift to next smaller nonempty flight */
          return;
        case PgDn:
          if (flight == numflights-1)
            flight = 0;
          else
            flight++;
          list_gross(flight); /* shift to next larger nonempty flight */
          return;
      }
    }
}

/* ---------------------------------------------------------------------- */
void
init_list_matches()
{
  int row = 3;

  /* std_window() is a little too narrow, so use this one */
  window(23,1,80,25);
  textattr(norm_attr);
  clrscr();
  window(1,1,80,25);
  textattr(7);
  rect(1,1,22,25,7,1);
  window(2,2,21,48);
  textattr(high_attr);
  clrscr();
  gotoxy(4,row);
  cprintf("LIST MATCHES");
  row += 3;
  gotoxy(2,row++);
  cprintf("For each selected");
  gotoxy(2,row++);
  cprintf("player, the results");
  gotoxy(2,row++);
  cprintf("of his matches with");
  gotoxy(2,row++);
  cprintf("the other players");
  gotoxy(2,row++);
  cprintf("in his flight are");
  gotoxy(2,row++);
  cprintf("given. 'W' means");
  gotoxy(2,row++);
  cprintf("that the selected");
  gotoxy(2,row++);
  cprintf("player won this");
  gotoxy(2,row++);
  cprintf("match, 'L' that he");
  gotoxy(2,row++);
  cprintf("lost this one.");
  row++;
  gotoxy(2,row++);
  cprintf("Press Esc, or Enter");
  gotoxy(2,row++);
  cprintf("a blank name to");
  gotoxy(2,row);
  cprintf("return to the menu.");
  window(23,1,80,25);
  textattr(norm_attr);
}

/* ---------------------------------------------------------------------- */
void
list_matches()
{
  int i,j,w,row0,row,col;
  int flight, n;      /* n = NP[flight] */
  int m, k, cnt, r, r1;
  char pname[21];
  extern int norm_attr;
  int flt[10],index[10];

  init_list_matches();
a:
  clrscr();
  row = 3; col = 2;
  r = get_string("Name",pname,namefield,col+8,row);
  if (!r) goto ret;
  k = findn(pname,flt,index);
  if (k == -2) {
    gotoxy(col,row+1);
    cprintf("More than ten found. Press any key...");
    getch();
    gotoxy(col,row+1);
    clreol();
    goto a;
  }
  if (k == -1) {
    gotoxy(col,row+1);
    cprintf("Not found. Press any key...");
    getch();
    gotoxy(col,row+1);
    clreol();
    goto a;
  }
  if (k > 0)
    k = pick_one(row+1, k, flt, index);
  flight = flt[k];
  m = index[k];
  if (player[flight][m].scores[0] == 0) {
    gotoxy(col,row+1);
    cprintf("%s has no scores entered. Press any key ...",player[flight][m].name);
    getch();
    gotoxy(col,row+1);
    clreol();
    goto a;
  }
  strcpy(pname,player[flight][m].name);
  asort(flight);         /* arrange players in alphabtical order */
  n = NP[flight];
  m = 0;
  /* find pname in sorted flight */
  while (strcmp(player[flight][indx[flight][m]].name,pname)) m++;
  k = (n-2)/2 + 1;   /* half the number of players being compared to pname */
  clrscr();
  gotoxy(col,row);
  cputs(pname);
  gotoxy(col+21,row); cprintf("FLIGHT %d",flight);
  gotoxy(col,row+1);
  cprintf("Won %d Tied %d Lost %d",player[flight][indx[flight][m]].won,
  player[flight][indx[flight][m]].tied, player[flight][indx[flight][m]].lost);

  calc_outcome(flight);
  row0 = 5;
  cnt = 0;       /* count of players compared to m */
  for (j=0; j<n; j++) {
    if (j==m || player[flight][j].scores[0] == 0)
      continue;
    cnt++;
    if (cnt <= k) {
      row = row0+cnt;
      col = 2;
    }
    else {
      row = row0+cnt-k;
      col = 31;
    }
    gotoxy(col,row);
    cputs(player[flight][indx[flight][j]].name);
    w = outcome[indx[flight][m]][indx[flight][j]];   /* comparison array */
    gotoxy(col+21,row);
    if (w<0) cputs("L ");
    else if (w>0) cputs("W ");
    else {
      cputs("Tied");
      continue;
    }
    r1 = abs(w);
    if (r1 == 1) cputs("1 up");
    else if (r1 == 12) cputs("2 up");
    else if (r1<10) cprintf("%d&%d",r1,r1-1);
    else if (r1>11) cprintf("%d&%d",r1-10,r1-12);
  }
  gotoxy(3,20); cputs("Press any key to continue");
  getch();
  goto a;
ret:
  clrscr();
}

/* ---------------------------------------------------------------------- */
void
init_list_net()
{
  int i;

  std_screen();
  gotoxy(8,3);
  cprintf("Net Scores");
  for (i=0; i<6; i++) {
    gotoxy(4,5+i);
    cputs(basic_msgs[i]);
  }
}

/* ---------------------------------------------------------------------- */
void
list_net(int flight)
{
  int c,f,n,n0,n1,i,a,b,row=3;
  int p,j,page,new;
  extern int norm_attr;
  int first = 1;

  if (first) {
    init_list_net();
    first = 0;
  }

    n = NP[flight];
    for (i=0; i<n; i++) {
      net[flight][i] = player[flight][i].net;
      if (net[flight][i] == 0)
        net[flight][i] = 200;   /* so players with no scores will be at end */
    }
    nsort(flight);
    window(28,1,80,25);
    textattr(norm_attr);
    clrscr();
    n1 = n/2; if (n % 2) n1++;
    gotoxy(22,1);
    cprintf("FLIGHT %d",flight);
    for (i=0; i<n1; i++) {
      a = indx[flight][i]; b = indx[flight][i+n1];
      gotoxy(2,row);
      cprintf("%s",player[flight][a].name);
      gotoxy(23,row); cprintf("%-3d",player[flight][a].net);
      if (i+n1 < NP[flight]) {
        gotoxy(27,row); cprintf(player[flight][b].name);
        gotoxy(49,row); cprintf("%-3d",player[flight][b].net);
      }
      row++;
    }
    while (kbhit())
      getch();             /* clear kb buffer */
    while (1) {
      switch(getkey()) {
        case ESC:
        case CR:
          return;
        case PgUp:
          if (flight == 0)
            flight = numflights-1;     /* largest nonempty flight */
          else
            flight--;
          list_net(flight); /* shift to next smaller nonempty flight */
          return;
        case PgDn:
          if (flight == numflights-1)
            flight = 0;
          else
            flight++;
          list_net(flight); /* shift to next larger nonempty flight */
          return;
        }
    }
}

/* ---------------------------------------------------------------------- */
void
init_list_points()
{
  int i;

  std_screen();
  gotoxy(8,3);
  cprintf("Point System");
  for (i=0; i<6; i++) {
    gotoxy(4,5+i);
    cputs(basic_msgs[i]);
  }
}

/* ---------------------------------------------------------------------- */
void
list_points(int flight)
{
  int c,f,n,n0,n1,i,a,b,row=3;
  int p,j,page,new;
  extern int norm_attr;
  int first = 1;

  if (first) {
    init_list_points();
    first = 0;
  }

    n = NP[flight];
    for (i=0; i<n; i++) {
      points[flight][i] = player[flight][i].points;
    }
    psort(flight);
    window(28,1,80,25);
    textattr(norm_attr);
    clrscr();
    n1 = n/2; if (n % 2) n1++;
    gotoxy(22,1);
    cprintf("FLIGHT %d",flight);
    for (i=0; i<n1; i++) {
      a = indx[flight][i]; b = indx[flight][i+n1];
      gotoxy(2,row);
      cprintf("%s",player[flight][a].name);
      gotoxy(23,row); cprintf("%-3d",player[flight][a].points);
      if (i+n1 < NP[flight]) {
        gotoxy(27,row); cprintf(player[flight][b].name);
        gotoxy(49,row); cprintf("%-3d",player[flight][b].points);
      }
      row++;
    }
    while (kbhit())
      getch();             /* clear kb buffer */
    while (1) {
      switch(getkey()) {
        case ESC:
        case CR:
          return;
        case PgUp:
          if (flight == 0)
            flight = numflights-1;     /* largest nonempty flight */
          else
            flight--;
          list_points(flight); /* shift to next smaller nonempty flight */
          return;
        case PgDn:
          if (flight == numflights-1)
            flight = 0;
          else
            flight++;
          list_points(flight); /* shift to next larger nonempty flight */
          return;
        }
    }
}

/* ---------------------------------------------------------------------- */
void
init_list_results()
{
  int i;

  std_screen();
  gotoxy(4,2);
  cprintf("MATCH PLAY RESULTS");
  gotoxy(1,3);
  cprintf("(win = pt, tie = 1/2 pt)");
  for (i=0; i<6; i++) {
    gotoxy(3,5+i);
    cputs(basic_msgs[i]);
  }
}

/* ---------------------------------------------------------------------- */
void
list_results(int flight)
{
  int c,i,f,a,row;
  extern int norm_attr;
  int first = 1;
  if (first) {
    init_list_results();        /* fixed messages */
    first = 0;
  }
  window(28,1,80,25);
  textattr(norm_attr);
  msort(flight);
  clrscr(); row = 1;
  gotoxy(7,row);
  cprintf("Flight %d",flight);
  gotoxy(23,row);
  cprintf("Won    Tied  Lost   Points");
  for (i=0; i<NP[flight]; i++) {
    row++;
    a = indx[flight][i];
    gotoxy(2,row);
    cprintf("%s",player[flight][a].name);
    gotoxy(24,row);
    cprintf("%-3d",player[flight][a].won);
    gotoxy(30,row);
    cprintf("%-3d",player[flight][a].tied);
    gotoxy(36,row);
    cprintf("%-3d",player[flight][a].lost);
    gotoxy(45,row);
    cprintf("%-3d",player[flight][a].won + player[flight][a].tied/2);
    if (player[flight][a].tied % 2) cputs("1/2");
  }
  while (kbhit())
    getch();              /* clear kb buffer */
  while (1) {
    switch(getkey()) {
      case ESC:
      case CR:
        return;
      case PgUp:
        if (flight == 0)
          flight = numflights-1;
        else
          flight--;
        list_results(flight); /* shift to next smaller nonempty flight */
        return;
      case PgDn:
        if (flight == numflights-1)
          flight = 0;
        else
          flight++;
        list_results(flight); /* shift to next larger nonempty flight */
        return;
    }
  }
}

/* ---------------------------------------------------------------------- */
void
nsort(int flight)
{
  int i,j,temp,sz;

  sz = NP[flight];
  for (i=0; i<sz; i++) indx[flight][i] = i;

  for (i=1; i<sz; i++) {
    temp = indx[flight][i];
    j = i-1;
    while(j>=0) {
      if (net[flight][indx[flight][j]] > net[flight][temp]
       || (net[flight][indx[flight][j]] == net[flight][temp] &&
       strcmp(player[flight][indx[flight][j]].name,player[flight][temp].name)>0)) {
        indx[flight][j+1] = indx[flight][j];
        j--;
      }
      else
        break;
    }
    indx[flight][j+1] = temp;
  }
}

/* ---------------------------------------------------------------------- */
void
psort(int flight)
{
  int i,j,temp,sz;

  sz = NP[flight];
  for (i=0; i<sz; i++) indx[flight][i] = i;

  for (i=1; i<sz; i++) {
    temp = indx[flight][i];
    j = i-1;
    while(j>=0) {
      if (points[flight][indx[flight][j]] < points[flight][temp]
       || (points[flight][indx[flight][j]] == points[flight][temp] &&
       strcmp(player[flight][indx[flight][j]].name,player[flight][temp].name)>0)) {
        indx[flight][j+1] = indx[flight][j];
        j--;
      }
      else
        break;
    }
    indx[flight][j+1] = temp;
  }
}

/* ---------------------------------------------------------------------- */
void
print_gross(int flight)
{
  int c,n,n1,i,a,b,pos;

  n = NP[flight];
  for (i=0; i<n; i++) {
    gross[flight][i] = player[flight][i].gross;
    if (gross[flight][i] ==0)
      gross[flight][i] = 200;  /* so players with no scores will be at end */
  }
  gsort(flight);
  for (i=0; i<n; i++)
    if (gross[flight][i] == 200)
      gross[flight][i] = 0;     /* reset gross to 0 for players without scores */
  n1 = n/2; if (n % 2) n1++;
    fprintf(stdprn,"\n\n\n");
    prnspaces(30);
    fprintf(stdprn,"Gross Scores, Flight %d\n\n",flight);
  for (i=0; i<n1; i++) {
    pos = 0;             /* 'pos' is position of printer on current line */
    a = indx[flight][i]; b = indx[flight][i+n1];
    prnspaces(8); pos = 8;
    fprintf(stdprn,"%s",player[flight][a].name);
    pos += strlen(player[flight][a].name);
    prnspaces(31 - pos); pos = 31;
    fprintf(stdprn,"%-3d",player[flight][a].gross);
    pos += 3;
    if (i+n1 < NP[flight]) {
      prnspaces(12); pos += 12;
      fprintf(stdprn,player[flight][b].name);
      pos += strlen(player[flight][b].name);
      prnspaces(69-pos);
      fprintf(stdprn,"%-3d",player[flight][b].gross);
    }
    fprintf(stdprn,"\n");
  }
}

/* ---------------------------------------------------------------------- */
void
print_net(int flight)
{
  int c,n,n1,i,a,b,pos;

  n = NP[flight];
  for (i=0; i<n; i++)
    if (player[flight][i].gross == 0)
      net[flight][i] = 200; /* so players without scores will be at end */
    else
      net[flight][i] = player[flight][i].gross - player[flight][i].hcp;
  nsort(flight);
  for (i=0; i<n; i++)
    if (net[flight][i] == 200)
      net[flight][i] = 0;    /* reset net[] for players with no scores or hcps */
  n1 = n/2; if (n % 2) n1++;
    fprintf(stdprn,"\n\n\n");
    prnspaces(32);
    fprintf(stdprn,"Net Scores, Flight %d\n\n",flight);
  for (i=0; i<n1; i++) {
    pos = 0;             /* 'pos' is position of printer on current line */
    a = indx[flight][i]; b = indx[flight][i+n1];
    prnspaces(8); pos = 8;
    fprintf(stdprn,"%s",player[flight][a].name);
    pos += strlen(player[flight][a].name);
    prnspaces(31 - pos); pos = 31;
    fprintf(stdprn,"%-3d",player[flight][a].net);
    pos += 3;
    if (i+n1 < NP[flight]) {
      prnspaces(12); pos += 12;
      fprintf(stdprn,player[flight][b].name);
      pos += strlen(player[flight][b].name);
      prnspaces(69-pos);
      fprintf(stdprn,"%-3d",player[flight][b].net);
    }
    fprintf(stdprn,"\n");
  }
}

/* ---------------------------------------------------------------------- */
void
print_points(int flight)
{
  int c,n,n1,i,a,b,pos;

  n = NP[flight];
  for (i=0; i<n; i++)
      points[flight][i] = player[flight][i].points;
  psort(flight);
  n1 = n/2; if (n % 2) n1++;
    fprintf(stdprn,"\n\n\n");
    prnspaces(32);
    fprintf(stdprn,"Point System, Flight %d\n\n",flight);
  for (i=0; i<n1; i++) {
    pos = 0;             /* 'pos' is position of printer on current line */
    a = indx[flight][i]; b = indx[flight][i+n1];
    prnspaces(8); pos = 8;
    fprintf(stdprn,"%s",player[flight][a].name);
    pos += strlen(player[flight][a].name);
    prnspaces(31 - pos); pos = 31;
    fprintf(stdprn,"%-3d",player[flight][a].points);
    pos += 3;
    if (i+n1 < NP[flight]) {
      prnspaces(12); pos += 12;
      fprintf(stdprn,player[flight][b].name);
      pos += strlen(player[flight][b].name);
      prnspaces(69-pos);
      fprintf(stdprn,"%-3d",player[flight][b].points);
    }
    fprintf(stdprn,"\n");
  }
}

/* ---------------------------------------------------------------------- */
void
print_matches(int flight)
{
  int f = flight;
  int i,j,j1,j2,cnt,n,m,k,z;
  char s[27],result[24][27];
  char pname[21], ext[3];
  FILE *fp;
  char filename[11] = "matches.";
  char command[20] = "copy ";
  char divider[] =
"--------------------------------------------------------------------------";

  strcat(filename,itoa(flight,ext,10));
  strcat(command,filename);
  strcat(command," PRN");
  fp = fopen(filename,"w");

  asort(flight);
  calc_outcome(flight);
  n = NP[flight];
  m = n/2;           /*  m = rows  needed to list matches in 2 columns */
  k = 63/(m+2)-1;    /*  number of complete data sets per 66-line page */

  for (i=0; i<n; i++) {
    if (i % k == 0) {                    /* start new page */
      fprintf(fp,"%c",12);               /* form feed for printer */
      fprintf(fp,"\n");
      fprnspaces(fp,35);
      fprintf(fp,"FLIGHT %d\n",flight);  /* page header */
    }
    if (player[f][indx[f][i]].scores[0] == 0)
      continue;                         /* ignore players w/o scores */
    strcpy(pname,player[flight][indx[flight][i]].name);
    fprintf(fp,"\n%s",strupr(pname));
    fprnspaces(fp,22-strlen(pname));
    fprintf(fp,"Won %d  Tied %d  Lost %d", player[f][indx[f][i]].won,
       player[f][indx[f][i]].tied, player[f][indx[f][i]].lost);
    z = 0;
    for (j=0; j<n; j++) {
      if (j == i || player[f][indx[f][j]].scores[0] == 0)
        continue;
      make_resultstring(flight,i,j,s);
      strcpy(result[z],s);
      z++;
    }
    for (z=0; z<m; z++) {
      fprintf(fp,"\n");
      fprintf(fp,"   %s",result[z]);
      if (z+m < n-1) {fprnspaces(fp,15); fprintf(fp,"%s",result[z+m]);}
    }
    z = fprintf(fp, "\n%s\n",divider);
  }
  fclose(fp);
  system(command);
}

/* ---------------------------------- */
int
make_resultstring(int flight, int i, int j, char *s)
{
  int k,w;
  char *p, *q;
int len;

  for (k=0; k<26; k++)
    s[k] = ' ';
  s[27] = '\0';
  p = player[flight][indx[flight][j]].name;
  q = s;
  while (*p)
    *q++ = *p++;
  q = s+20;
  w = outcome[i][j];
  if (w > 0)
    *q++ = 'W';
  else if (w < 0) {
    *q++ = 'L';
    w = -w;
  }
  q++;
  *q = '\0';
  strcat(s,outcomestring[w]);
len = strlen(s);
return len;
}

/* ---------------------------------------------------------------------- */
void
print_results(int flight)
{
  int n,n0,n1,i,temp,a,b;

  msort(flight);
  n = NP[flight]; n0 = n1 = 0;

  while(n1 < n) {
    fprintf(stdprn,"\n\n\n");
    prnspaces(33);
    fprintf(stdprn,"FLIGHT %d\n",flight);
    if (n1 > 0) fprintf(stdprn," (continued)");
    fprintf(stdprn,"\n\n");
    n1 = n0 + 50; if (n1 > n) n1 = n;
    prnspaces(32);
    fprintf(stdprn,"Won    Tied      Lost      Points\n");
    for (i=n0; i<n1; i++) {
      a = indx[flight][i];
      prnspaces(13);
      fprintf(stdprn,player[flight][a].name);
      prnspaces(20 - strlen(player[flight][a].name));
      fprintf(stdprn,"%-3d",player[flight][a].won);
      prnspaces(7);
      fprintf(stdprn,"%-3d",player[flight][a].tied);
      prnspaces(7);
      fprintf(stdprn,"%-3d",player[flight][a].lost);
      prnspaces(7);
      fprintf(stdprn,"%-3d",player[flight][a].won + player[flight][a].tied/2);
      if (player[flight][a].tied % 2) fprintf(stdprn,"1/2");
      fprintf(stdprn,"\n");
    }
    biosprint(0,12,0);                            /* send formfeed to printer */
    n0 = n1;
  }
}

/* ---------------------------------------------------------------------- */
void
prnspaces(int n)
{
   int i;

   for (i=0; i<n; i++)
     fprintf(stdprn," ");
}

/* ---------------------------------------------------------------------- */
void
fprnspaces(FILE *fp, int n)
{
   int i;

   for (i=0; i<n; i++)
     fprintf(fp," ");
}

/* ---------------------------------------------------------------------- */
void
msort(int flight)
{
  int i,j,temp,sz,mp[MAXSZ];

  sz = NP[flight];
  for (i=0; i<sz; i++) indx[flight][i] = i;

  for (i=0; i<sz; i++) {
    if (player[flight][i].won != 0 || player[flight][i].tied != 0)
      mp[i] = 2*player[flight][i].won + player[flight][i].tied;
    else if (player[flight][i].won == 0 && player[flight][i].tied == 0 &&
             player[flight][i].lost != 0)
      mp[i] = 0;
    else
      mp[i] = -1;
  }
  for (i=0; i<sz-1; i++) {
    for (j=i+1; j<sz; j++) {
      if (mp[indx[flight][i]] < mp[indx[flight][j]]) {
        temp = indx[flight][i];
        indx[flight][i] = indx[flight][j];
        indx[flight][j] = temp;
      }
    }
  }
}

/* ----------------------------------------------------------------------- */
void
strokes_msg(int k,int col, int row)
{
   int h;
   int difficult[18];     /* holes in order of decreasing difficulty */

   for (h=0; h<18; h++) difficult[order[h]-1] = h+1;

   gotoxy(col,row);
   if (k == 1)
     cprintf("one stroke, on hole %d", difficult[0]);
   else if (k < 18) {
     cprintf("%d strokes, on holes ",k);
     for (h=0; h<k; h++) {
       if (h == 10) gotoxy(col+21,++row);
       cprintf("%d ",difficult[h]);
     }
   }
   else if (k == 18)
     cputs("one stroke on each hole.");
   else if (k < 36) {
     cputs("two strokes on ");
     for (h=0; h<k-18; h++) {
       if (h == 12) gotoxy(col+15,++row);
       cprintf("%d ",difficult[h]);
     }
     gotoxy(col,row+1);
     cputs("one stroke on all other holes.");
   }
   else if (k == 36)
     cputs("two strokes on each hole.");
   else {
     cputs("three strokes on ");
     for (h=0; h<k-36; h++)
       cprintf("%d ",difficult[h]);
     gotoxy(col,row+1);
     cputs("two strokes on all other holes.");
   }
}

/* ---------------------------------------------------------------------- */
void
show_data(int flight, int index, int row)
{
  int i,j,k;

    j = indx[flight][index];
    gotoxy(2,row);
    cputs(player[flight][j].name);
    gotoxy(24,row);
    cprintf("%d",player[flight][j].hcp);
    gotoxy(28,row);
    if (player[flight][j].scores[0])
      cputs(player[flight][j].scores);
    else
      cputs("");
}

/* ---------------------------------------------------------------------- */
void
write_data(FILE *fp)
{
   int i,j,k;
   extern int par[];
   extern char data_title[];

   fprintf(fp,"%s\n\n",data_title);
   for (i=0; i<18; i++)
     fprintf(fp,"%d ",par[i]);
   fprintf(fp,"\n");
   for (i=0; i<18; i++)
     fprintf(fp,"%d ",order[i]);
   fprintf(fp,"\n\n");
   for (k=0; k<MAXFL; k++) {
     if (!NP[k]) continue;
     asort(k);
     for (i=0; i<NP[k]; i++) {
       j = indx[k][i];
       fprintf(fp,"%s\n",player[k][j].name);
       fprintf(fp,"%d\n",player[k][j].hcp);
       if (player[k][j].scores[0])
         fprintf(fp,"%s\n",player[k][j].scores);
       else
         fprintf(fp,"No Scores\n");
     }
     fprintf(fp," \n");
   }
}

