home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 605b.lha / Spades_v2.10 / Source / Files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-06  |  6.9 KB  |  276 lines

  1. /*
  2.    FILE: Files.c
  3.    PURPOSE: Handles all File I/O operations (saving and printing)
  4.    AUTHOR: Gregory M. Stelmack
  5. */
  6.  
  7. #ifndef GLOBALS_H
  8. #include "Globals.h"
  9. #endif
  10.  
  11. #define EOF (-1)     /* since I don't use stdio */
  12.  
  13. /**********************************************
  14. * Function: WriteHand                         *
  15. * Parameters: None                            *
  16. * Return Values: None                         *
  17. * Purpose: Save the hand just played to disk. *
  18. **********************************************/
  19. void WriteHand()
  20. {
  21.    BPTR fh;
  22.    char buffer[55];
  23.    int  i,j,k;
  24.    LONG error;
  25.    char *SuitString = "DCHS";
  26.    char *CardString = "23456789TJQKA";
  27.    int  EastPos, WestPos;
  28.    int  player;
  29.    
  30.    /* Open save file, position at end */
  31.    
  32.    fh = Open("Spades.save",MODE_READWRITE);
  33.    if (!fh)
  34.       PError("Could not open save file Spades.save");
  35.    (void)Seek(fh,0L,OFFSET_END);
  36.    
  37.    /* Mark beginning */
  38.    
  39.    error = FPuts(fh,"\n*****************************************\n\n");
  40.    if (error!=0)
  41.       PError("Error writing save file");
  42.    
  43.    /* Write out the rules */
  44.  
  45.    if (NilRule==TRUE)
  46.    {
  47.       error = FPuts(fh,"NIL Rule in effect\n");
  48.       if (error!=0)
  49.          PError("Error writing save file");
  50.    }
  51.    if (BagsRule==TRUE)
  52.    {
  53.       error = FPuts(fh,"Bags Rule in effect\n");
  54.       if (error!=0)
  55.          PError("Error writing save file");
  56.    } 
  57.    
  58.    if ((NilRule==TRUE)||(BagsRule==TRUE))
  59.    {
  60.       error = FPutC(fh,'\n');
  61.       if (error==EOF)
  62.          PError("Error writing save file");
  63.    }
  64.    
  65.    /* Write the dealer */
  66.    
  67.    error = FPuts(fh,"Dealer = ");
  68.    if (error!=0)
  69.       PError("Error writing save file");
  70.    switch(HandLead)
  71.    {
  72.       case 0:
  73.          error = FPuts(fh,"South\n");
  74.          break;
  75.       case 1:
  76.          error = FPuts(fh,"West\n");
  77.          break;
  78.       case 2:
  79.          error = FPuts(fh,"North\n");
  80.          break;
  81.       case 3:
  82.          error = FPuts(fh,"East\n");
  83.          break;
  84.    }
  85.    if (error!=0)
  86.       PError("Error writing save file");
  87.    
  88.    /* Write North's hand */
  89.    
  90.    for (i=3 ; i>=0 ; i--)   /* loop through suits */
  91.    {
  92.       error = FPuts(fh,"               ");
  93.       if (error!=0)
  94.          PError("Error writing save file");
  95.       error = FPutC(fh,*(SuitString+i));
  96.       if (error==EOF)
  97.          PError("Error writing save file");
  98.       error = FPutC(fh,':');
  99.       if (error==EOF)
  100.          PError("Error writing save file");
  101.       for (j=12 ; j>=0 ; j--)
  102.       {
  103.          if (Deck[i*13+j]==3)
  104.          {
  105.             error = FPutC(fh,*(CardString+j));
  106.             if (error==EOF)
  107.                PError("Error writing save file");
  108.          }
  109.       }
  110.       error = FPutC(fh,'\n');
  111.       if (error==EOF)
  112.          PError("Error writing save file");
  113.    }
  114.    
  115.    /* Write West and East's hand */
  116.    
  117.    buffer[53] = '\n';
  118.    buffer[54] = '\0';
  119.    
  120.    for (i=3 ; i>=0 ; i--)   /* loop through suits */
  121.    {
  122.       for (k=0 ; k<53 ; k++) buffer[k] = ' ';
  123.       buffer[0] = buffer[35] = *(SuitString+i);
  124.       buffer[1] = buffer[36] = ':';
  125.       WestPos = 2; EastPos = 37;
  126.       
  127.       for (j=12 ; j>=0 ; j--)
  128.       {
  129.          if (Deck[i*13+j]==2)
  130.          {
  131.             buffer[WestPos] = *(CardString+j);
  132.             WestPos++;
  133.          }
  134.          else if (Deck[i*13+j]==4)
  135.          {
  136.             buffer[EastPos] = *(CardString+j);
  137.             EastPos++;
  138.          }
  139.       }
  140.       
  141.       error = FPuts(fh,&buffer[0]);
  142.       if (error!=0)
  143.          PError("Error writing save file");
  144.    }
  145.    
  146.    /* Write South's hand */
  147.    
  148.    for (i=3 ; i>=0 ; i--)   /* loop through suits */
  149.    {
  150.       error = FPuts(fh,"               ");
  151.       if (error!=0)
  152.          PError("Error writing save file");
  153.       error = FPutC(fh,*(SuitString+i));
  154.       if (error==EOF)
  155.          PError("Error writing save file");
  156.       error = FPutC(fh,':');
  157.       if (error==EOF)
  158.          PError("Error writing save file");
  159.       for (j=12 ; j>=0 ; j--)
  160.       {
  161.          if (Deck[i*13+j]==1)
  162.          {
  163.             error = FPutC(fh,*(CardString+j));
  164.             if (error==EOF)
  165.                PError("Error writing save file");
  166.          }
  167.       }
  168.       error = FPutC(fh,'\n');
  169.       if (error==EOF)
  170.          PError("Error writing save file");
  171.    }
  172.    
  173.    /* Write Bids */
  174.    
  175.    error = FPuts(fh,"\nBIDS:   West: ");
  176.    if (error!=0)
  177.       PError("Error writing save file");
  178.    itoa(Bid[1],String);
  179.    error = FPuts(fh,String);
  180.    if (error!=0)
  181.       PError("Error writing save file");
  182.    error = FPuts(fh,"   North: ");
  183.    if (error!=0)
  184.       PError("Error writing save file");
  185.    itoa(Bid[2],String);
  186.    error = FPuts(fh,String);
  187.    if (error!=0)
  188.       PError("Error writing save file");
  189.    error = FPuts(fh,"   East: ");
  190.    if (error!=0)
  191.       PError("Error writing save file");
  192.    itoa(Bid[3],String);
  193.    error = FPuts(fh,String);
  194.    if (error!=0)
  195.       PError("Error writing save file");
  196.    error = FPuts(fh,"   South: ");
  197.    if (error!=0)
  198.       PError("Error writing save file");
  199.    itoa(Bid[0],String);
  200.    error = FPuts(fh,String);
  201.    if (error!=0)
  202.       PError("Error writing save file");
  203.    FPutC(fh,'\n');
  204.  
  205.    /* Print the play of the hand */
  206.    
  207.    for (i=0 ; i<4 ; i++)
  208.    {
  209.       error = FPutC(fh,'\n');
  210.       if (error==EOF)
  211.          PError("Error writing save file");
  212.       player = (i+1)%4;    /* Adjust to start with West */
  213.       switch(player)
  214.       {
  215.          case 0:
  216.             error = FPuts(fh,"S: ");
  217.             if (error!=0)
  218.                PError("Error writing save file");
  219.             break;
  220.          case 1:
  221.             error = FPuts(fh,"W: ");
  222.             if (error!=0)
  223.                PError("Error writing save file");
  224.             break;
  225.          case 2:
  226.             error = FPuts(fh,"N: ");
  227.             if (error!=0)
  228.                PError("Error writing save file");
  229.             break;
  230.          case 3:
  231.             error = FPuts(fh,"E: ");
  232.             if (error!=0)
  233.                PError("Error writing save file");
  234.             break;
  235.       }
  236.       
  237.       for (j=0 ; j<13 ; j++)  /* Loop through the tricks */
  238.       {
  239.          if (TrickLeader[j]==player)
  240.          {
  241.             error = FPutC(fh,'*');
  242.             if (error==EOF)
  243.                PError("Error writing save file");
  244.          }
  245.          else
  246.          {
  247.             error = FPutC(fh,' ');
  248.             if (error==EOF)
  249.                PError("Error writing save file");
  250.          }
  251.          error = FPutC(fh,*(SuitString+(CardPlayed[player][j]/13)));
  252.          if (error==EOF)
  253.             PError("Error writing save file");
  254.          error = FPutC(fh,*(CardString+(CardPlayed[player][j]%13)));
  255.          if (error==EOF)
  256.             PError("Error writing save file");
  257.       }  /* end loop through tricks */
  258.       
  259.       if (TrickLeader[13]==player)  /* See if player won last trick */
  260.       {
  261.          error = FPutC(fh,'*');
  262.          if (error==EOF)
  263.             PError("Error writing save file");
  264.       }
  265.       
  266.    }  /* end loop through players */
  267.    
  268.    error = FPutC(fh,'\n');
  269.    if (error==EOF)
  270.       PError("Error writing save file");
  271.    
  272.    /* Close save file */
  273.    
  274.    (void)Close(fh);
  275. }
  276.