home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / hce.lha / HCE / Examples / AMIGA / Pools / pools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  15.2 KB  |  569 lines

  1. /*
  2.  * EasyPools:
  3.  *
  4.  * Copyright (c) 1994. Author: Jason Petty.
  5.  *
  6.  * Permission is granted to anyone to use this software for any purpose
  7.  * on any computer system, and to redistribute it freely, with the
  8.  * following restrictions:
  9.  * 1) No charge may be made other than reasonable charges for reproduction.
  10.  * 2) Modified versions must be clearly marked as such.
  11.  * 3) The authors are not responsible for any harmful consequences
  12.  *    of using this software, even if they result from defects in it.
  13.  *
  14.  *
  15.  * USAGE:
  16.  *
  17.  *      Gives simple pools results based on tables found in the
  18.  *      'P_TABLES' file.
  19.  *      Can send the results to a printer.
  20.  *
  21.  *
  22.  *      This first version of EasyPools is a some what rushed version,
  23.  *      and does not do nearly enough calculations to get good pools results.
  24.  *      It is here merely as an example of using HCE with more than one C
  25.  *      source file. Feel free to improve EasyPools.
  26.  *
  27.  */
  28.  
  29.  
  30. #include <exec/types.h>
  31. #include <exec/errors.h>
  32. #include <exec/memory.h>
  33.  
  34. #include <clib/stdio.h>
  35. #include <clib/string.h>
  36.  
  37. #include "pools.h"
  38.  
  39. /* For calculation functions. */
  40. #define G_PLAYED 0
  41. #define G_WON    1
  42. #define G_DRAWN  2
  43. #define G_LOST   3
  44.  
  45. #define SAME_PLAYED  0x00000001
  46. #define NEAR_PLAYED  0x00000002
  47. #define SAME_WON     0x00000004
  48. #define NEAR_WON     0x00000008
  49. #define SAME_DRAWN   0x00000010
  50. #define NEAR_DRAWN   0x00000020
  51. #define SAME_LOST    0x00000040
  52. #define NEAR_LOST    0x00000080
  53.  
  54. extern P_TABLE *t_head;     /* Head of table list read in by read.c */
  55.  
  56.  
  57. int PRINT_ON=FALSE;         /* flag. Send results to printer?. */
  58. BYTE p_error;               /* flag. General printer error.    */
  59.  
  60. char PRT_B[5][100];         /* Printer Buffer.     */
  61. char TXT_B[100];            /* Window text buffer. */
  62.  
  63. char draw_msg[60];  /* Prediction message. */
  64. int home_c=0;       /* Vars - used to calculate chances of a Draw. */
  65. int away_c=0;
  66. int draw_c=0;
  67. int league_c=0;     /* Show/Print league number. */
  68. int coupon_c=0;     /* Show/Print coupon number. */
  69.  
  70. void main()
  71. {
  72.  long league=0;
  73.  int i=0;
  74.  int quit=0;
  75.  
  76.  if(!start())      /* Open screen/window/gad/printer set graphics etc. */
  77.       exit(10);    /* EXIT_ERROR. This must succeed to continue!!.   */
  78.  
  79.  if(!readfile()) { /* Try read the pools tables from disk .*/
  80.       close_shop("ERROR: Could not read tables from disk!!");
  81.       }
  82.  
  83.       PRINT_ON=FALSE;   /* Keep false for now. */
  84.       Show_LEAGUE_N();  /* Show league names to choose from.   */
  85.  
  86.  while(!quit)   /* MAIN LOOP */
  87.   {
  88.        league = Get_GMsgs2();   /* Get a valid league number. (gfx_win). */
  89.  
  90.     if(league != -1)
  91.         {
  92.            league_c = league+1;
  93.  
  94.        if(league == 1000) 
  95.            quit++;
  96.           else
  97.            Do_LEAGUE(league);
  98.  
  99.            Show_LEAGUE_N();     /* Reshow league names,( might have */
  100.            league = -1;         /* been trashed). */
  101.         }
  102.    }
  103.  
  104. close_shop("OK!!"); /* Ok!!. */
  105. }
  106.  
  107. void close_shop(msg) /* Close and free everything. */
  108. char *msg;
  109. {
  110.   /* printf("%s\n",msg); */
  111.   (void)finish();  /* Must call this!!. (frees all memory gads etc). */
  112.   exit(0);         /* OK!. */
  113. }
  114.  
  115. void Do_LEAGUE(id)     /* Do Fixtures until IDCMP_CLOSEWINDOW. */
  116. int id;
  117. {
  118.   P_TABLE *pt;
  119.   int i,r,home,away,near;
  120.   WORD endall=0;
  121.   WORD limmit = 0;
  122.   int *h_team,*a_team;
  123.   int *tb[30];
  124.   WORD step;
  125.   char *t_names[30];
  126.   char *p,pb[2];
  127.  
  128.   draw_msg[0] = '\0';
  129.   draw_msg[1] = '\0';
  130.   pt=t_head;                   /* Head of table list. */
  131.  
  132.   for(i=0;i < id;i++) {        /* Find correct table. */
  133.       if(pt->next != NULL)
  134.          pt = pt->next;
  135.       }
  136.      
  137.   if(!(Open_GWind(pt->league)))   /* Open wind with new heading. */
  138.       return;
  139.  
  140.       limmit = pt->count;
  141.       Show_LEAGUE(pt);            /* Show teams. */
  142.  
  143.   for(i=0;i <= limmit;i++)        /* Get pointers to team form numbers. */
  144.       tb[i] = pt->table[i];
  145.  
  146.   for(i=0;i <= limmit;i++)        /* Get pointers to team names. */
  147.       t_names[i] = pt->team[i];
  148.  
  149.       Draw_RBOX();                /* Draw pools result box. */
  150.       g_BPEN(10);                 /* Grey. */
  151.       Refresh_GWind();            /* Refresh gadgets in g_window. */
  152.  
  153.  while(!endall)
  154.   {
  155.    home_c=0;
  156.    away_c=0;
  157.    draw_c=0;
  158.    near=0;
  159.    step=0;
  160.  
  161.    if(!(Get_Team(&home,limmit))) {        /* Get valid home team. */
  162.         (void)Close_GWind();
  163.          return;                          /* User Quit? */
  164.          }
  165.         Clear_RBOX();                     /* Clear printing area. */
  166.         g_FPEN(1);                        /* White. */
  167.         i = strlen(t_names[home])-1;      /* Get home teams string length*/
  168.         pb[1] = '\0';
  169.         while(i >= 0) {                   /* Print home team in reverse, */
  170.               pb[0] = t_names[home][i];   /* keeps it even to away team. */
  171.               g_TXT(pb,(269-step), RS_Y); /* RS_Y: Result area Y start.  */
  172.               step += 8;
  173.               i--;
  174.               }
  175.  
  176.         g_TXT("- V`s -",293, RS_Y);
  177.  
  178.    if(!(Get_Team(&away,limmit))) {  /* Get valid away team. */
  179.         (void)Close_GWind();
  180.          return;                    /* User Quit? */
  181.          }
  182.         g_TXT(t_names[away],365, RS_Y);  /* No need for reverse here. */
  183.  
  184.         h_team = tb[home];
  185.         a_team = tb[away];
  186.         coupon_c++; /* Inc coupon number. */
  187.  
  188. /* DEBUG **************************************************************
  189.  printf("HOME TABLE: %d %d %d %d %d %d %d\n",
  190.      h_team[0],h_team[1],h_team[2],h_team[3],h_team[4],h_team[5],h_team[6]);
  191.  
  192.  printf("AWAY TABLE: %d %d %d %d %d %d %d\n",
  193.      a_team[0],a_team[1],a_team[2],a_team[3],a_team[4],a_team[5],a_team[6]);
  194. ***********************************************************************/
  195.  
  196.  
  197. /****************** CALCULATIONS *******************/
  198.  
  199.      /* Calc home_c and away_c. */
  200.   near |= cr_GamesPlayed(h_team,a_team);
  201.   near |= cr_GamesWon(h_team,a_team);
  202.   near |= cr_GamesDrawn(h_team,a_team);
  203.   near |= cr_GamesLost(h_team,a_team);
  204.  
  205. /****** These are not really required! *****
  206.         cr_GamesFor();
  207.         cr_GamesAgainst();
  208.         cr_TeamPoints();
  209. **********************************/
  210.  
  211.      /* Work out chances of a draw. */
  212.  
  213.        if(home_c > away_c) {
  214.           draw_c = 100 - (home_c - away_c);
  215.           }
  216.        if(home_c < away_c) {
  217.           draw_c = 100 - (away_c - home_c);
  218.           }
  219.        if(home_c == away_c) {
  220.           draw_c = 100;
  221.           }
  222.        if(draw_c < 0)
  223.           draw_c = 0;
  224.  
  225.      /* Work out comment to go with  results */
  226.         Do_Comment();
  227.  
  228.         strcat(draw_msg,"-(");
  229.  
  230.      if(near & SAME_PLAYED)     /* HOME & AWAY - PLAYED SAME.     */
  231.         strcat(draw_msg,"P");
  232.      if(near & NEAR_PLAYED)     /* HOME or AWAY - PLAYED, 1 MORE. */
  233.         strcat(draw_msg,"p");
  234.      if(near & SAME_WON)        /* HOME & AWAY - WON SAME.        */
  235.         strcat(draw_msg,"W");
  236.      if(near & NEAR_WON)        /* HOME or AWAY - WON, 1 MORE.    */
  237.         strcat(draw_msg,"w");
  238.      if(near & SAME_DRAWN)      /* HOME & AWAY - DRAWN SAME.      */
  239.         strcat(draw_msg,"D");
  240.      if(near & NEAR_DRAWN)      /* HOME or AWAY - DRAWN, 1 MORE.  */
  241.         strcat(draw_msg,"d");
  242.      if(near & SAME_LOST)       /* HOME & AWAY - LOST SAME.       */
  243.         strcat(draw_msg,"L");
  244.      if(near & NEAR_LOST)       /* HOME or AWAY - LOST, 1 MORE.   */
  245.         strcat(draw_msg,"l");
  246.  
  247.         strcat(draw_msg,")");
  248.  
  249. /*************** END OF CALCULATIONS *******************/
  250.  
  251. /* Print results in g_window. */
  252.  
  253.      g_FPEN(5);                                    /* Blue. */
  254.      g_TXT("COUPON-NO  LEAGUE  HOME  AWAY  DRAW   ?",156,RS_Y+20);
  255.  
  256.      g_FPEN(1);                                    /* White. */
  257.      sprintf(TXT_B,"%d",coupon_c);                 /* Coupon No. */
  258.      g_TXT(TXT_B,156,RS_Y+30);
  259.  
  260.      sprintf(TXT_B,"%d",league_c);                 /* League. */
  261.      g_TXT(TXT_B,156+88,RS_Y+30);
  262.  
  263.      sprintf(TXT_B,"%d%%",home_c);                 /* Print Home. */
  264.      g_TXT(TXT_B,156+152,RS_Y+30);
  265.  
  266.      sprintf(TXT_B,"%d%%",away_c);                 /* Print Away. */
  267.      g_TXT(TXT_B,156+200,RS_Y+30);
  268.  
  269.      sprintf(TXT_B,"%d%%",draw_c);                 /* Print Draw. */
  270.      g_TXT(TXT_B,156+248,RS_Y+30);
  271.  
  272.      g_FPEN(2);                                    /* Yellow. */
  273.      g_TXT(draw_msg,156+296,RS_Y+30);              /* ?. */
  274.  
  275.  
  276.   if(PRINT_ON) /* Send results to the Printer. */
  277.     {
  278.      sprintf(PRT_B[1],"%d  %d\r\t\b%s-%s",
  279.      coupon_c, league_c, t_names[home], t_names[away]);
  280.      sprintf(PRT_B[2],
  281. "\r\t\t\t\t%d%%\r\t\t\t\t\t\b%d%%\r\t\t\t\t\t\t\b%d%%\r\t\t\t\t\t\t\t\b%s\n",
  282.       home_c, away_c, draw_c, draw_msg);
  283.  
  284.      strcpy(PRT_B[0],PRT_B[1]);
  285.      strcat(PRT_B[0],PRT_B[2]);
  286.  
  287. /*     printf("%s",PRT_B[0]); DEBUG */
  288.  
  289.     if((p_error = (BYTE)DO_PrtText(PRT_B[0])))
  290.          PrtError(p_error);
  291.      }
  292.    }
  293. }
  294.  
  295. void Do_Comment() /* Work out comment to go with results. */
  296. {
  297.      if(draw_c <= 55)
  298.          strcpy(draw_msg,"*");
  299.      if(draw_c > 55 && draw_c < 66)
  300.          strcpy(draw_msg,"**");
  301.      if(draw_c > 65 && draw_c < 80)
  302.          strcpy(draw_msg,"***");
  303.      if(draw_c > 79 && draw_c < 90)
  304.          strcpy(draw_msg,"****");
  305.      if(draw_c > 89)
  306.          strcpy(draw_msg,"!* DRAW *!");
  307. }
  308.  
  309. void Print_Heading()  /* Print pools heading ready for results to follow. */
  310. {                     /* Uses workbench printer prefs.     */
  311.                       /* 'DO_PrtText()' is found in Gfx.c. */
  312.  strcpy(PRT_B[0],"\nCN LG         TEAMS");
  313.  strcat(PRT_B[0],
  314.  "\r\t\t\t\tHOME\r\t\t\t\t\t\bAWAY\r\t\t\t\t\t\t\bDRAW\r\t\t\t\t\t\t\t\b ?");
  315.  strcat(PRT_B[0],"\n\n");
  316.  
  317. /*     printf("%s",PRT_B[0]); DEBUG */
  318.  
  319.   if((p_error = (BYTE)DO_PrtText(PRT_B[0])))
  320.          PrtError(p_error);
  321. }
  322.  
  323. void Show_LEAGUE(pt)      /* Show leagues-teams.(g_window)*/
  324. P_TABLE *pt;
  325. {
  326.  WORD i = 0;
  327.  WORD len = 0;
  328.  WORD inn = INN_X+37;     /* near left edge.    */
  329.  WORD mid = MID_X+37;     /* middle area.       */
  330.  WORD out = OUT_X+37;     /* near right edge.   */
  331.  WORD top = TOP_Y+9;      /* From top edge.     */
  332.  WORD gap = GAP_Y+WID_Y;  /* Dist between gads. */
  333.  
  334.      g_FPEN(1);
  335.      g_BPEN(0);
  336.  
  337.      len = pt->count;
  338.  
  339.  while(i <= len)       /* Print league '*pt' points to. */
  340.   {
  341.         g_TXT(pt->team[i],inn,top);
  342.         i++;
  343.      if(!(i <= len))
  344.         return;
  345.         g_TXT(pt->team[i],mid,top);
  346.         i++;
  347.      if(!(i <= len))
  348.         return;
  349.         g_TXT(pt->team[i],out,top);
  350.         i++;
  351.     top += gap;
  352.    }
  353. }
  354.  
  355. void Show_LEAGUE_N()   /* Show all league names. (gfx_window). */
  356. {
  357.  P_TABLE *pt;
  358.  WORD out = c_OUT_X+45;       /* From left edge. */
  359.  WORD top = c_TOP_Y+10;       /* From top edge.  */
  360.  WORD gap = c_GAP_Y+c_WID_Y;  /* Dist between gads. */
  361.  
  362.      gfx_FPEN(4);
  363.      gfx_BPEN(0);
  364.      gfx_TXT("-------------------------------",(out-53),(top-22));
  365.      gfx_FPEN(2);
  366.      gfx_TXT(" USE MOUSE TO SELECT A LEAGUE.",(out-53),(top-30));
  367.  
  368.      gfx_FPEN(5);
  369.  
  370.      pt=t_head;
  371.  
  372.     while(pt != NULL) {
  373.        gfx_TXT(pt->league,out,top);
  374.        top += gap;
  375.        pt = pt->next;
  376.        }
  377. }
  378.  
  379. int Get_Team(tm,tsize)  /* Get a Valid Team number. (g_window). */
  380. int *tm, tsize;
  381. {
  382.  long msg = -1;
  383.  
  384.   do {
  385.       if((msg = Get_GMsgs()) == 1000)
  386.           return(0); /* Quit. */
  387.        if(msg != -1)
  388.          {
  389.           switch(msg)
  390.             {
  391.                 case 27:             /* Give user some help. */
  392.                           Clear_RBOX();
  393.                           Help();
  394.                           break;
  395.                 case 28: /* Set/Unset printer flag. */
  396.                           Clear_RBOX(); /* Clear result box. */
  397.                       if(!PRINT_ON) {
  398.                           RB_Msg("RESULTS TO SCREEN AND PRINTER.");
  399.                           PRINT_ON=TRUE;
  400.                           }
  401.                         else {
  402.                           RB_Msg("PRINTER-INACTIVE");
  403.                           PRINT_ON=FALSE;
  404.                           }
  405.                          break;
  406.                  case 29:               /* Print results heading. */
  407.                      if(PRINT_ON)
  408.                          Print_Heading();
  409.                          break;
  410.                  case 30:               /* Received new coupon number */
  411.                          coupon_c -= 1;
  412.                       if(coupon_c < 0)
  413.                          coupon_c = 0;  /* Minimum coupon number is 1. */
  414.                          break;
  415.                  default:
  416.                          break;
  417.              }
  418.           }
  419.       } 
  420.   while((msg < 0) || (msg > tsize));
  421.  
  422.   if(msg > tsize)
  423.      msg = tsize;
  424.    
  425.  *tm = (int)msg;
  426. return(1);
  427. }
  428.  
  429. /*********************** CALCULATION FUNCTIONS ********************/
  430.  
  431. /* GAMES PLAYED. */
  432. cr_GamesPlayed(h_team,a_team)
  433. int *h_team,*a_team;
  434. {
  435.    int i,near=0;
  436.    int n = 5;
  437.  
  438. /* [0] = Games played. */
  439. /* Take n% from the team which played more.*/
  440.  
  441.      if(h_team[G_PLAYED] == a_team[G_PLAYED])  /* Played the same. */
  442.         return(SAME_PLAYED);
  443.  
  444.      if(h_team[G_PLAYED] > a_team[G_PLAYED])   /* Home played more */
  445.        {
  446.          if((i = (h_team[G_PLAYED] - a_team[G_PLAYED])) == 1)
  447.             near = NEAR_PLAYED;
  448.  
  449.             while(i--)
  450.                   home_c -= n;
  451.         } 
  452.       else                      /* Away played more */
  453.         {
  454.          if(a_team[G_PLAYED] > h_team[G_PLAYED]) 
  455.            {
  456.             if((i = (a_team[G_PLAYED] - h_team[G_PLAYED])) == 1)
  457.                 near = NEAR_PLAYED;
  458.  
  459.             while(i--)
  460.                   away_c -= n;
  461.             }
  462.          }
  463. return(near);
  464. }
  465.  
  466. /* GAMES WON. */
  467. cr_GamesWon(h_team,a_team)
  468. int *h_team,*a_team;
  469. {
  470.   int i,near = 0;
  471.   int n = 6;
  472.  
  473. /* Give n% to the team which has won the most. */
  474.  
  475.      if(h_team[G_WON] == a_team[G_WON])  /* Won the same. */
  476.         return(SAME_WON);
  477.  
  478.      if(h_team[G_WON] > a_team[G_WON]) {         /* [1] = Games won. */
  479.         
  480.         if((i = (h_team[G_WON] - a_team[G_WON])) == 1) /* Home won most */ 
  481.             near=NEAR_WON;
  482.  
  483.          while(i--)
  484.                home_c += n;
  485.  
  486.         }
  487.        else      /* Away won most. */
  488.          {
  489.            if(a_team[G_WON] > h_team[G_WON])
  490.               {
  491.                if((i = (a_team[G_WON] - h_team[G_WON])) == 1)
  492.                    near=NEAR_WON;
  493.  
  494.                 while(i--)
  495.                       away_c += n;
  496.                }
  497.           }
  498. return(near);
  499. }
  500.  
  501. /* GAMES DRAWN. */
  502. cr_GamesDrawn(h_team,a_team)
  503. int *h_team,*a_team;
  504. {
  505.  int i,near=0;
  506.  int n = 2;
  507.  
  508.  /* Give n% to the team which has drawn the least. */
  509.  
  510.  if(h_team[G_DRAWN] == a_team[G_DRAWN]) /* Drawn the same. */
  511.     return(SAME_DRAWN);
  512.  
  513.  if(h_team[G_DRAWN] > a_team[G_DRAWN])  /* [2] = NUMBER OF DRAWS. */
  514.    {
  515.     if((i = (h_team[G_DRAWN] - a_team[G_DRAWN])) == 1) /* AWAY HAS LESS. */
  516.        near=NEAR_DRAWN;
  517.  
  518.     while(i--)
  519.           away_c += n;
  520.  
  521.    } 
  522.  else          /* AWAY HAS MORE DRAWS. */
  523.    {
  524.     if(a_team[G_DRAWN] > h_team[G_DRAWN]) 
  525.       {
  526.         if((i = (a_team[G_DRAWN] - h_team[G_DRAWN])) == 1)
  527.            near=NEAR_DRAWN;
  528.  
  529.         while(i--)
  530.            home_c += n;
  531.        }
  532.      }
  533. return(near);
  534. }
  535.  
  536. /* GAMES LOST. */
  537. cr_GamesLost(h_team,a_team)
  538. int *h_team,*a_team;
  539. {
  540.  int i,near=0;
  541.  int n = 6;
  542.  
  543.  /* Give n% to the team which has lost the least. */
  544.  
  545.  if(h_team[G_LOST] == a_team[G_LOST]) /* Lost the same. */
  546.     return(SAME_LOST);
  547.  
  548.  if(h_team[G_LOST] > a_team[G_LOST])  /* [3] = GAMES LOST. */
  549.    {
  550.        if((i = (h_team[G_LOST] - a_team[G_LOST])) == 1) /* HOME LOST MORE. */
  551.           near=NEAR_LOST;
  552.  
  553.     while(i--)
  554.           away_c += n;
  555.    } 
  556.  else          /* AWAY LOST MORE. */
  557.    {
  558.     if(a_team[G_LOST] > h_team[G_LOST]) 
  559.       {
  560.         if((i = (a_team[G_LOST] - h_team[G_LOST])) == 1)
  561.            near=NEAR_LOST;
  562.  
  563.         while(i--)
  564.            home_c += n;
  565.         }
  566.      }
  567. return(near);
  568. }
  569.