home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk430.lzh / Lotto / program.c < prev    next >
C/C++ Source or Header  |  1991-01-11  |  9KB  |  371 lines

  1. /*********************************************************
  2. *  FLORIDA LOTTO PROGRAM v1.0
  3. *  © Copyright 1988 Timm Martin
  4. *  All Rights Reserved
  5. *
  6. *  This program may be distributed freely as long as
  7. *  this notice remains intact.  This program may not
  8. *  be sold individually or as part of a group without
  9. *  the express written consent of the author.
  10. *
  11. *  This program is for entertainment only and is sold
  12. *  without any express or implied warranties whatsoever.
  13. *  The user must assume the entire risk of using the
  14. *  program (hey, you don't need my help to lose your
  15. *  money).  Any liability of the author will be limited
  16. *  to a refund of the purchase price (which is nothing).
  17. *
  18. *  Send all compliments, complaints, and monetary
  19. *  expressions of gratitude to:
  20. *
  21. *    Timm Martin
  22. *    11325 94th Street North
  23. *    Largo, FL 34643
  24. *
  25. *  My only request is that if you win BIG using this
  26. *  program, please mention to everyone that you used
  27. *  this program to generate your winning numbers.
  28. **********************************************************/
  29.  
  30. #include <exec/types.h>
  31. #include <graphics/gfxbase.h>
  32. #include <graphics/text.h>
  33. #include <intuition/intuition.h>
  34. #include <intuition/intuitionbase.h>
  35. #include <intuition/screens.h>  /* Kickstart v1.2.1 or later */
  36. #include <libraries/dos.h>
  37. #include <functions.h>          /* Manx C */
  38.  
  39. /********************
  40. *  AMIGA STRUCTURES
  41. *********************/
  42.  
  43. struct IntuitionBase *IntuitionBase = NULL;
  44. struct GfxBase *GfxBase = NULL;
  45. struct Screen *screen = NULL;
  46. struct Window *window = NULL;
  47.  
  48. /************************
  49. *  NEW SCREEN STRUCTURE
  50. *************************/
  51.  
  52. #define COLORS 8L
  53. #define PURPLE 0L
  54. #define BLACK  1L
  55. #define BLUE   2L
  56. #define WHITE  3L
  57. #define RED    6L
  58. #define VIOLET 7L
  59.  
  60. struct TextAttr screen_font = {
  61.   (STRPTR)"topaz.font", 8, 0, 0
  62. };
  63.  
  64. struct NewScreen new_screen = {
  65.   0, 0, 320, 200, 3, PURPLE, PURPLE, NULL, CUSTOMSCREEN,
  66.   &screen_font, NULL, NULL, NULL
  67. };
  68.  
  69. USHORT color_table[COLORS] = {
  70.   0x748, 0x000, 0x00F, 0xFFF, 0x000, 0x0F0, 0xF00, 0x415
  71. };
  72.  
  73. /*********************
  74. *  GADGET STRUCTURES
  75. **********************/
  76.  
  77. /*** TEXT ***/
  78.  
  79. struct IntuiText lotto_text = {
  80.   WHITE, BLUE, JAM1, 6, 5, NULL, (STRPTR)"LOTTO", NULL
  81. };
  82. struct IntuiText quit_text = {
  83.   WHITE, BLUE, JAM1, 10, 5, NULL, (STRPTR)"QUIT", NULL
  84. };
  85.  
  86. /*** BORDERS ***/
  87.  
  88. SHORT gadget_pairs2[] = {
  89.   4,21, 55,21, 55,4, 56,4, 56,22, 4,22, 4,23, 57,23, 57,4, 58,4, 58,24, 4,24
  90. };
  91. SHORT gadget_pairs1[] = {
  92.   0,0, 0,20, 54,20, 54,0, 1,0, 1,19, 53,19, 53,1, 2,1
  93. };
  94.  
  95. struct Border gadget_border2 = {
  96.   -2, -2, VIOLET, PURPLE, JAM1, 12, gadget_pairs2, NULL
  97. };
  98. struct Border gadget_border1 = {
  99.   -2, -2, BLACK, PURPLE, JAM1, 9, gadget_pairs1, &gadget_border2
  100. };
  101.  
  102. #define LOTTO_GADGET 1
  103. #define QUIT_GADGET  2
  104.  
  105. struct Gadget lotto_gadget = {
  106.   NULL, 83, 106, 51, 17, GADGHCOMP, RELVERIFY, BOOLGADGET,
  107.   (APTR)&gadget_border1, NULL, &lotto_text, NULL, NULL, LOTTO_GADGET, NULL
  108. };
  109. struct Gadget quit_gadget = {
  110.   &lotto_gadget, 186, 106, 51, 17, GADGHCOMP, RELVERIFY, BOOLGADGET,
  111.   (APTR)&gadget_border1, NULL, &quit_text, NULL, NULL, QUIT_GADGET, NULL
  112. };
  113.  
  114. /************************
  115. *  LOTTO BOX STRUCTURES
  116. *************************/
  117.  
  118.  
  119. SHORT box_pairs2[] = {
  120.   4,23, 158,23, 158,4, 159,4, 159,24, 4,24,
  121.   4,25, 160,25, 160,4, 161,4, 161,26, 4,26
  122. };
  123. SHORT box_pairs1[] = {
  124.   0,0, 0,22, 157,22, 157,0, 1,0, 1,21, 156,21, 156,1, 2,1
  125. };
  126.  
  127. struct Border box_border2 = {
  128.   81, 74, VIOLET, PURPLE, JAM1, 12, box_pairs2, NULL
  129. };
  130. struct Border box_border1 = {
  131.   81, 74, BLACK, PURPLE, JAM1, 9, box_pairs1, &box_border2
  132. };
  133.  
  134. /************************
  135. *  NEW WINDOW STRUCTURE
  136. *************************/
  137.  
  138. #define LATER NULL
  139. struct NewWindow new_window = {
  140.   0, 0, 320, 200, PURPLE, PURPLE, GADGETUP,
  141.   ACTIVATE | BORDERLESS | NOCAREREFRESH | SIMPLE_REFRESH, LATER,
  142.   NULL, NULL, LATER, NULL, 320, 200, 320, 200, CUSTOMSCREEN
  143. };
  144.  
  145. /********************
  146. *  GLOBAL VARIABLES
  147. *********************/
  148.  
  149. #define NUMBERS 6
  150. #define UPPER  49L
  151.  
  152. int  numbers[NUMBERS];
  153. long random_variable;
  154.  
  155. /*************
  156. *  FUNCTIONS
  157. **************/
  158.  
  159. void display_numbers();  /* displays the lotto numbers in the box */
  160. void end_program();      /* closes the window, screen, and libraries */
  161. void get_inputs();       /* receives and processes user input */
  162. void get_numbers();      /* gets the lotto numbers */
  163. void initialize();       /* draws the lotto box and renders the gadgets */
  164. void open_all();         /* opens the libraries, screen, and window */
  165. long random();           /* random number function */
  166. long random_mult();      /* calculation used by random() function */
  167. void randomize();        /* plants pseudo-random variable seed using clock */
  168.  
  169. /**************************
  170. *  M A I N  P R O G R A M
  171. ***************************/
  172.  
  173. main()
  174. {
  175.   open_all();
  176.   initialize();
  177.   get_inputs();
  178. }
  179.  
  180. /*******************
  181. *  DISPLAY NUMBERS
  182. ********************/
  183.  
  184. void display_numbers()
  185. {
  186.   char digits[2];
  187.   int  i, j;
  188.   long left, r;
  189.  
  190.   SetAPen( window->RPort, RED );
  191.   RectFill( window->RPort, 83L, 76L, 236L, 94L );
  192.  
  193.   SetBPen( window->RPort, RED );
  194.   for (i = 0; i < NUMBERS; i++) {
  195.     left = 92 + i * 24;
  196.     SetAPen( window->RPort, BLACK );
  197.     for (j = 0; j < 150; j++) {
  198.       r = random( UPPER ) + 1;
  199.       digits[0] = r / 10 + '0';
  200.       digits[1] = r % 10 + '0';
  201.       Move( window->RPort, left, 88L );
  202.       Text( window->RPort, digits, 2L );
  203.     }
  204.     SetAPen( window->RPort, WHITE );
  205.     digits[0] = numbers[i] / 10 + '0';
  206.     digits[1] = numbers[i] % 10 + '0';
  207.     Move( window->RPort, left, 88L );
  208.     Text( window->RPort, digits, 2L );
  209.   }
  210. }
  211.  
  212. /***************
  213. *  END PROGRAM
  214. ****************/
  215.  
  216. void end_program( return_code )
  217.   int return_code;
  218. {
  219.   if (window)        CloseWindow( window );
  220.   if (screen)        CloseScreen( screen );
  221.   if (GfxBase)       CloseLibrary( GfxBase );
  222.   if (IntuitionBase) CloseLibrary( IntuitionBase );
  223.  
  224.   exit( return_code );
  225. }
  226.  
  227. /**************
  228. *  GET INPUTS
  229. ***************/
  230.  
  231. #define FOREVER for(;;)
  232. #define GADGET_ID ((struct Gadget *)(imessage->IAddress))->GadgetID
  233. #define WAIT_FOR_INPUT Wait(1L<<window->UserPort->mp_SigBit)
  234. #define WINDOW_INPUT imessage=(struct IntuiMessage *)GetMsg(window->UserPort)
  235.  
  236. void get_inputs()
  237. {
  238.   struct IntuiMessage *imessage;
  239.  
  240.   FOREVER {
  241.     WAIT_FOR_INPUT;
  242.  
  243.     while (WINDOW_INPUT) {
  244.       /* switch used here in case add more input methods later */
  245.       switch (imessage->Class) {
  246.  
  247.         case GADGETUP:
  248.           switch (GADGET_ID) {
  249.             case LOTTO_GADGET: get_numbers(); break;
  250.             case QUIT_GADGET:  end_program( 0 ); break;
  251.           } /* switch gadget */
  252.           break;
  253.  
  254.       } /* switch input class */
  255.     }   /* while window input */
  256.   }     /* forever */
  257. }
  258.  
  259. /***************
  260. *  GET NUMBERS
  261. ****************/
  262.  
  263. void get_numbers()
  264. {
  265.   int  i, count, upper;
  266.   int  list[UPPER];
  267.   long r;
  268.  
  269.   for (i = 0; i < UPPER; i++)
  270.     list[i] = i + 1;
  271.   for (count = 0, upper = UPPER; count < NUMBERS; count++, upper--) {
  272.     /* the random() function returns a long integer 0 <= r < upper */
  273.     r = random( (long)upper );
  274.     numbers[count] = list[r];
  275.     for (i = r; i < upper; i++)
  276.       list[i] = list[i+1];
  277.   }
  278.   display_numbers();
  279. }
  280.  
  281. /**************
  282. *  INITIALIZE
  283. ***************/
  284.  
  285. void initialize()
  286. {
  287.   SetAPen( window->RPort, RED );
  288.   RectFill( window->RPort, 83L, 76L, 236L, 94L );
  289.   DrawBorder( window->RPort, &box_border1, 0L, 0L );
  290.  
  291.   SetAPen( window->RPort, BLUE );
  292.   RectFill( window->RPort, 83L, 106L, 133L, 122L );
  293.   RectFill( window->RPort, 186L, 106L, 236L, 122L );
  294.   AddGList( window, &quit_gadget, -1L, -1L, NULL );
  295.   RefreshGList( &quit_gadget, window, NULL, -1L );
  296. }
  297.  
  298. /************
  299. *  OPEN ALL
  300. *************/
  301.  
  302. void open_all()
  303. {
  304.   if (!(IntuitionBase = (struct IntuitionBase *)
  305.         OpenLibrary( "intuition.library", LIBRARY_VERSION )))
  306.     end_program( 1 );
  307.   if (!(GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 0L )))
  308.     end_program( 2 );
  309.  
  310.   if (!(screen = OpenScreen( &new_screen )))
  311.     end_program( 3 );
  312.   LoadRGB4( &screen->ViewPort, color_table, COLORS );
  313.  
  314.   new_window.Screen = screen;
  315.   if (!(window = OpenWindow( &new_window )))
  316.     end_program( 4 );
  317.  
  318.   randomize();
  319. }
  320.  
  321. /**********
  322. *  RANDOM
  323. ***********/
  324.  
  325. #define LARGE  100000000
  326. #define SMALL  10000
  327. #define MEDIUM 31415821
  328.  
  329. long random( upper )
  330.   long upper;
  331. {
  332.   if (upper < 0)
  333.     upper = -upper;  /* force positive */
  334.  
  335.   random_variable = (random_mult( random_variable, MEDIUM ) + 1) % LARGE;
  336.   upper = ((random_variable / SMALL) * upper) / SMALL;
  337.  
  338.   return (upper);
  339. }
  340.  
  341. /***************
  342. *  RANDOM MULT
  343. ****************/
  344.  
  345. long random_mult( a, b )
  346.   long a, b;
  347. {
  348.   long a0, a1, b0, b1, m;
  349.  
  350.   a0 = a % SMALL;
  351.   a1 = a / SMALL;
  352.   b0 = b % SMALL;
  353.   b1 = b / SMALL;
  354.  
  355.   m = (((a0 * b1 + a1 * b0) % SMALL) * SMALL + a0 * b0) % LARGE;
  356.  
  357.   return (m);
  358. }
  359.  
  360. /*************
  361. *  RANDOMIZE
  362. **************/
  363.  
  364. void randomize()
  365. {
  366.   struct DateStamp ds;
  367.  
  368.   DateStamp( &ds );
  369.   random_variable = ds.ds_Tick;
  370. }
  371.