home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 486.lha / Hi-Low_v1.0 / src / hilow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  6.2 KB  |  297 lines

  1.  
  2.  
  3. /*
  4.  *
  5.  * Module    : hilow.c
  6.  *
  7.  * Description    : Card game to demonstrate the use of my IFF tools.
  8.  *
  9.  * Author    : Simon J Raybould.    (sie@fulcrum.bt.co.uk)
  10.  *
  11.  * Date        :  9th September 1990.
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <signal.h>
  17. #include <intuition/intuition.h>
  18. #include <graphics/gfxmacros.h>
  19.  
  20. #include "hilow.h"
  21.  
  22.  
  23. main()
  24. {
  25.     struct Screen *OpenScreen();
  26.     struct Window *OpenWindow();
  27.     void CleanExit(), UpdateBank(), PlaySound(), Catch();
  28.     int Cash = STARTCASH, Winnings;    /* In Pence */
  29.     long Seed;
  30.  
  31.     signal(SIGINT, Catch);
  32.     if(!(IntuitionBase = (struct IntuitionBase *)
  33.                 OpenLibrary("intuition.library", 33L))) {
  34.         fprintf(stderr, "Failed to open intuition!!\n");
  35.         CleanExit(1);
  36.     }
  37.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33L))) {
  38.         fprintf(stderr, "Failed to open graphics!!\n");
  39.         CleanExit(1);
  40.     }
  41.     if(!(Screen = OpenScreen(&NewScreen))) {
  42.         fprintf(stderr, "Failed to open screen.\n");
  43.         CleanExit(1);
  44.     }
  45.     NewWindow.Screen = Screen;
  46.     if(!(Window = OpenWindow(&NewWindow))) {
  47.         fprintf(stderr, "Failed to open window.\n");
  48.         CleanExit(1);
  49.     }
  50.     LoadRGB4(&Screen->ViewPort, ColourTable, NCOLOURS);
  51.  
  52.     RPort = Window->RPort;    /* Stick this in a register */
  53.  
  54.     /* Get rid of title bar */
  55.     SetAPen(RPort, 0);
  56.     SetDrMd(RPort, JAM1);
  57.     RectFill(RPort, 0, 0, 320, 10);
  58.  
  59.     /* Set seed for random numbers */
  60.     time(&Seed);
  61.     srand((unsigned)Seed);
  62.  
  63.     DRAWSIG;
  64.     DrawImage(RPort, &TitleImage, TITLEX, TITLEY);
  65.  
  66.     SetAPen(RPort, 11);
  67.     SetDrMd(RPort, JAM2);
  68.     Move(RPort, BANKX+5, BANKY);
  69.     Text(RPort, "Bank", 4);
  70.     UpdateBank(Cash);
  71.  
  72.     SetAPen(RPort, 5);
  73.     SetDrMd(RPort, JAM2);
  74.     Move(RPort, 50, 100);
  75.     Text(RPort, "Loading data, please wait", 25);
  76.  
  77.     if(InitCards()) {
  78.         fprintf(stderr, "Can't find a pack of cards to use !!\n");
  79.         CleanExit(1);
  80.     }
  81.     if(InitSounds()) {
  82.         fprintf(stderr, "Can't find sound effects\n");
  83.         CleanExit(1);
  84.     }
  85.     SetAPen(RPort, 5);
  86.     SetDrMd(RPort, JAM2);
  87.     Move(RPort, 10, 100);
  88.     Text(RPort, "Ready to start, press DEAL to start", 35);
  89.     while(Cash) {
  90.         /* Wait before starting */
  91.         switch(GetChoice()) {
  92.         case CHOSE_QUIT:
  93.             CleanExit(0);
  94.             break;
  95.         case CHOSE_DEAL:
  96.             SetAPen(RPort, 0);
  97.             SetDrMd(RPort, JAM1);
  98.             RectFill(RPort, CARDX, CARDY, 320, CARDY+CHEIGHT+10);/*for Arw*/
  99.             Cash -= PPERGAME;    /* Must pay before you play */
  100.             UpdateBank(Cash);
  101.             if((Winnings = Play()) == -1) {
  102.                 fprintf(stderr, "Problem encoutered during game\n");
  103.                 CleanExit(1);
  104.             }
  105.             Cash += Winnings;
  106.             if(Winnings)
  107.                 UpdateBank(Cash);
  108.             break;
  109.         default:
  110.             break;
  111.         }
  112.     }
  113.     Delay(300);
  114.     CleanExit(0);
  115. }
  116.  
  117. Play()
  118. {
  119.     UBYTE TurnNo, CardNo;
  120.     int Choice=0, LastCard = 0, ThisCard = 0;
  121.  
  122.     /* Display card backs */
  123.     for(TurnNo=0; TurnNo<NTURNS; TurnNo++) {
  124.         DisplayCard(TurnNo, 0);    /* Display card back */
  125.         PlaySound(S_DEAL);
  126.         Delay(5);
  127.     }
  128.     Delay(50);    /* Show all backs before turning first card */
  129.     for(TurnNo=0; TurnNo<NTURNS; TurnNo++) {
  130.         LastCard = ThisCard;
  131.         if(TurnNo)    /* Don't wait for choice for first card i.e. Turn 0 */
  132.             while(Choice!=CHOSE_QUIT && Choice!=CHOSE_HIGH && Choice!=CHOSE_LOW)
  133.                 if((Choice = GetChoice())<0)
  134.                     return -1;
  135.         if(Choice == CHOSE_QUIT)
  136.             CleanExit(0);
  137.         /* Pick an unused card */
  138.         CardNo = 0;
  139.         while(!CardNo || CardUsed(TurnNo, CardNo))
  140.             CardNo = rand()%51 + 1;
  141.         ThisCard = (CardNo-1)%13;
  142.         /* Display it */
  143.         DisplayCard(TurnNo, CardNo);    /* Display card */
  144.         PlaySound(S_TURN);
  145.         /* Check to see if LOSE */
  146.         if(TurnNo) {    /* Not first card */
  147.             switch(Choice) {
  148.             case CHOSE_HIGH:
  149.                 DrawImage(RPort, &UpAImage, TurnNo*(CARDX+CWIDTH+CGAP)-8,
  150.                         CARDY+CHEIGHT-2);
  151.                 if(ThisCard <= LastCard) {
  152.                     PlaySound(S_LOSE);
  153.                     return 0;
  154.                 }
  155.                 break;
  156.             case CHOSE_LOW:
  157.                 DrawImage(RPort, &DnAImage, TurnNo*(CARDX+CWIDTH+CGAP)-8,
  158.                         CARDY+CHEIGHT-2);
  159.                 if(ThisCard >= LastCard) {
  160.                     PlaySound(S_LOSE);
  161.                     return 0;
  162.                 }
  163.                 break;
  164.             default:
  165.                 return -1;
  166.             }
  167.         }
  168.         Choice = 0;
  169.     }
  170.     PlaySound(S_WIN);
  171.     return ((rand()%5+1)*10);
  172. }
  173.  
  174. GetChoice()
  175. {
  176.     struct IntuiMessage *Message, *GetMsg();
  177.  
  178.     /* Clear message queue */
  179.     while(Message = GetMsg(Window->UserPort))
  180.         ReplyMsg(Message);
  181.  
  182.     /* Wait for event */
  183.     Message = GetMsg(Window->UserPort);
  184.     while (Message == NULL) {
  185.         Wait(1<<Window->UserPort->mp_SigBit);
  186.         Message = GetMsg(Window->UserPort);
  187.     };
  188.     if (Message)
  189.         ReplyMsg(Message);
  190.  
  191.     switch(Message->Class) {
  192.     case GADGETUP:
  193.         if(Message->IAddress == (APTR)&HiGadg)
  194.             return CHOSE_HIGH;
  195.         if(Message->IAddress == (APTR)&LoGadg)
  196.             return CHOSE_LOW;
  197.         if(Message->IAddress == (APTR)&DealGadg)
  198.             return CHOSE_DEAL;
  199.         if(Message->IAddress == (APTR)&QuitGadg)
  200.             return CHOSE_QUIT;
  201.         break;
  202.     default:
  203.         fprintf(stderr, "Unkown message (0x%x) recieved\n", Message->Class);
  204.         return -1;
  205.     }
  206.     return -1;
  207. }
  208.  
  209. CardUsed(TNo, CNo)
  210. UBYTE TNo, CNo;
  211. {
  212.     UBYTE i;
  213.     static UBYTE CrdUsd[NTURNS];
  214.  
  215.     for(i=0; i<TNo; i++)
  216.         if(CNo == CrdUsd[i])
  217.             return TRUE;
  218.     
  219.     CrdUsd[TNo] = CNo;
  220.     return FALSE;
  221. }
  222.  
  223. void
  224. CleanExit(Status)
  225. int Status;
  226. {
  227.     FreeBitMaps();
  228.     FreeSounds();
  229.     if(Window)
  230.         CloseWindow(Window);
  231.     if(Screen)
  232.         CloseScreen(Screen);
  233.     if(GfxBase)
  234.         CloseLibrary(GfxBase);
  235.     if(IntuitionBase)
  236.         CloseLibrary(IntuitionBase);
  237.     exit(Status);
  238. }
  239.  
  240. InitCards()
  241. {
  242.     int CardNo;
  243.     char *CardBM[] = {
  244.         "Back", "SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9",
  245.         "S10", "SJ", "SQ", "SK", "CA", "C2", "C3", "C4", "C5", "C6",
  246.         "C7", "C8", "C9", "C10", "CJ", "CQ", "CK", "HA", "H2", "H3",
  247.         "H4", "H5", "H6", "H7", "H8", "H9", "H10", "HJ", "HQ", "HK",
  248.         "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10",
  249.         "DJ", "DQ", "DK",
  250.     };
  251.     char Buffer[BUFSIZ];
  252.  
  253.     for(CardNo=0; CardNo<53; CardNo++) {
  254.         sprintf(Buffer, "%s%s", BITMAPDIR, CardBM[CardNo]);
  255.         if(SetUpBitMap(&CardArray[CardNo], Buffer))
  256.             return -1;
  257.     }
  258.     return 0;
  259. }
  260.  
  261. void
  262. UpdateBank(Cash)    /* Cash is is pence */
  263. int Cash;
  264. {
  265.     char Buffer[BUFSIZ];
  266.  
  267.     SetAPen(RPort, 3);
  268.     SetDrMd(RPort, JAM2);
  269.     Move(RPort, BANKX, BANKY+10);
  270.     sprintf(Buffer, "%c%d.%02d   ", C_SYMBOL, Cash/100, Cash%100);
  271.     Text(RPort, Buffer, strlen(Buffer));
  272. }
  273.  
  274. DisplayCard(TNo, CNo)
  275. int TNo, CNo;
  276. {
  277.     int x;
  278.  
  279.     if(TNo > NTURNS-1)
  280.         return -1;
  281.  
  282.     x = (CWIDTH + CGAP) * TNo + CARDX;
  283.     
  284.     BltBitMapRastPort(&CardArray[CNo], 0, 0, RPort, x, CARDY, CWIDTH,
  285.             CHEIGHT, MINTERM);
  286.     return 0;
  287. }
  288.  
  289. void
  290. Catch(s)
  291. int s;
  292. {
  293.     signal(SIGINT, SIG_IGN);    /* Lock out further breaks */
  294.     fprintf(stderr, "Caught signal %d, exiting\n", s);
  295.     CleanExit(1);
  296. }
  297.