home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / examples / amiga / pools / pools.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  16KB  |  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_ms