home *** CD-ROM | disk | FTP | other *** search
/ 100 Great Games for Palm OS 1 / 100PalmV1.iso / Cards / pocketjack / deckofcards_fo.cpk < prev    next >
Text File  |  1999-03-06  |  4KB  |  184 lines

  1. # deckofcards_fo.cpk
  2. # deck of cards module for any card game
  3.  
  4. # 6mar99, ver 0.1, frank o'brien, dianfrank@worldnet.att.net,
  5. # http://home.att.net/~dianfrank/casl_page.htm
  6. # freeware, no warranty, copyright
  7.  
  8. # user guide
  9. # see comments in sections marked "public"
  10.  
  11. # CARD VARIABLES - private
  12.  
  13. variables;
  14.     # deck constants
  15.     numeric dc_nShufs=10;
  16.     numeric dc_nShufCards=10;
  17.     numeric dc_nShufDelay=1000; # 1 s delay
  18.     numeric dc_nLastCard=52;
  19.     # deck variables
  20.     numeric dc_nDeck[dc_nLastCard];
  21.     numeric dc_nNextCard; # index for deck array
  22.     # temp string for holder main program name
  23.     string dc_sMainTitle;
  24. end;
  25.  
  26. # card variables - public
  27.  
  28. variables;
  29.     # user can append to about text, if desired
  30.     dc_sCredits="Card functions provided by "+
  31.      "deckofcards_fo.cpk, ver 0.1, 6Mar99, Frank O'Brien, "+
  32.      "http://home.att.net/~dianfrank/casl_page.htm"+char(10)+char(10)+
  33.      "Single deck of 52 shuffled cards.  After all cards dealt, "+
  34.      "deck is reshuffled by exchanging 2 random cards "+
  35.      string(dc_nShufs*dc_nShufCards,"")+" times.";
  36. end;
  37.  
  38. # CARD FUNCTIONS - private
  39.  
  40. # given cards to arrange, shufs times, randomly rearranges global deck array
  41. #  by exchanging card elements of array.
  42. # displays "shuffling..." on frMain for 2 s delay, as signal that shuffling,
  43. #  after delay, actually does shuffle
  44. #
  45. function dc_Shuffle;
  46.     variables;
  47.         numeric temp;
  48.         numeric i;
  49.         numeric j;
  50.         numeric n;
  51.         numeric m;
  52.         numeric t;
  53.         string s;
  54.     end;
  55.     # display that shuffling
  56.     get frMain, s;
  57.     put frMain, "Shuffling...";
  58.     # delay so can read display
  59.     t=timevalue();
  60.     while timevalue()-t<dc_nShufDelay;
  61.     end_while;
  62.     # do shuffle
  63.     i=0;
  64.     while i<dc_nShufs;
  65.         j=0;
  66.         while j<dc_nShufCards;
  67.             n=randomn(52);
  68.             m=randomn(52);
  69.             temp=dc_nDeck[n];
  70.             dc_nDeck[n]=dc_nDeck[m];
  71.             dc_nDeck[m]=temp;
  72.             j=j+1;
  73.         end_while;
  74.         i=i+1;
  75.     end_while;
  76.     # return to original display
  77.     put frMain, "PocketJack"; # s as parameter doesn't work, bug?
  78. end;
  79.  
  80. # CARD FUNCTIONS - public
  81.  
  82. # basic dealing functions
  83.  
  84. # initializes deck, must call once in startup function
  85. #
  86. function dc_InitDeck;
  87.     variables;
  88.         numeric i;
  89.     end;
  90.     # fill deck
  91.     i=0;
  92.     while i<52;
  93.         dc_nDeck[i]=i;
  94.         i=i+1;
  95.     end_while;
  96.     dc_nNextCard=52; # force shuffle
  97. end;
  98.  
  99. # returns next card in deck 0-51, if last card, performs reshuffle first
  100. #
  101. function dc_nfNextCard as numeric;
  102.     if dc_nNextCard=dc_nLastCard;
  103.         call dc_Shuffle;
  104.         dc_nNextCard=0;
  105.     end_if;
  106.     dc_nfNextCard=dc_nDeck[dc_nNextCard];
  107.     dc_nNextCard=dc_nNextCard+1;
  108. end;
  109.  
  110. # card information accessors
  111.  
  112. #given 0-51, returns 2-14
  113. #
  114. function dc_nfFace(numeric n) as numeric;
  115.     dc_nfFace=n%13+2;
  116. end;
  117.  
  118. #given 0-51, returns 0-3
  119. #
  120. function dc_nfSuit(numeric n) as numeric;
  121.     dc_nfSuit=int(n/13);
  122. end;
  123.  
  124. # display function - private
  125.  
  126. #given 0-51, returns display string for face 2-14
  127. #
  128. function dc_sfFace(numeric n) as string;
  129.     n=dc_nfFace(n);
  130.     if n > 1 and n < 10; # number 2-9
  131.         dc_sfFace=Char(48+n);
  132.     end_if;
  133.     if n = 10; # 10
  134.         dc_sfFace=Char(49)+Char(48);
  135.     end_if;
  136.     if n = 11; # jack
  137.         dc_sfFace=Char(74);
  138.     end_if;
  139.     if n = 12; # queen
  140.         dc_sfFace=Char(81);
  141.     end_if;
  142.     if n = 13; # king
  143.         dc_sfFace=Char(75);
  144.     end_if;
  145.     if n = 14; # high ace 
  146.         dc_sfFace=Char(65);
  147.     end_if;
  148. end;
  149.  
  150. #given 0-51, returns display string for 0-3 suit
  151. #
  152. function dc_sfSuit(numeric n) as string;
  153.     n=dc_nfSuit(n);
  154.     # do suit string for each platform, windows font doesn't have 
  155.     # suit char
  156.     if platform = "windows";
  157.         if n=0;
  158.             dc_sfSuit="d";
  159.         end_if;
  160.         if n=1;
  161.             dc_sfSuit="c";
  162.         end_if;
  163.         if n=2;
  164.             dc_sfSuit="h";
  165.         end_if;
  166.         if n=3;
  167.             dc_sfSuit="s";
  168.         end_if;
  169.     else;
  170.         # pilot platform
  171.         dc_sfSuit=Char(141+n);
  172.     end_if;
  173. end;
  174.  
  175. # Display functions - public
  176.  
  177. #given 0-51, returns display string for face and suit
  178. #
  179. function dc_sfCardName(numeric n) as string;
  180.     dc_sfCardName=dc_sfFace(n)+dc_sfSuit(n);
  181. end;
  182.  
  183.  
  184.