home *** CD-ROM | disk | FTP | other *** search
/ TestDrive Super Store 2.3 / TESTDRIVE_2.ISO / realizer / demos / games / bj.rlz next >
Encoding:
Text File  |  1992-09-30  |  10.1 KB  |  366 lines

  1. '***********************************************************************
  2. '    BJ.rlz                      
  3. '
  4. '    BlackJack Program
  5. '
  6. '    Copyright ⌐ 1991-1992 Computer Associates International, Inc.
  7. '    All rights reserved.
  8. '
  9. '***********************************************************************
  10.  
  11. SetSys(_LoadDir, QSys(_LoadDir) + ";" + QSys(_ProgDir) + "cards")
  12.  
  13. 'Shuffle the card deck 
  14. PROC ShuffleDeck 
  15.     LOCAL bmp, i 
  16.     NextCard = 1 
  17.     Deck = StatSort(rnd(NumCards), Deck) 
  18.     FOR I = 1 TO 8    'Show cards shuffling 
  19.         bmp = IF I MOD 2 THEN "shuff1.bmp" ELSE "shuff2.bmp" 
  20.     FormSetObject(10000, _Bitmap, bmp, 2 pct, 12 pct) 
  21.     NEXT I 
  22.     FormModifyObject(10000, _Close) 
  23. END PROC 
  24.  
  25. 'Return name of bitmap file containing a card.   
  26. FUNC CardName(card) 
  27.     RETURN "card" + sprint("P(0)", card) + ".bmp" 
  28. END FUNC 
  29.  
  30. 'Returns value of a card.  Note special processing for faces 
  31. 'and aces 
  32. FUNC CardValue(card) 
  33.     LOCAL num 
  34.     num = ((card-1) Mod 13)+1 
  35.     SELECT CASE num 
  36.     CASE 11 TO 13 
  37.         num = 10 
  38.     CASE 1 
  39.         num = 11 
  40.     END SELECT 
  41.     RETURN num      
  42. END FUNC 
  43.  
  44. 'Determines the value of a hand.  Sums cards, and if greater than 
  45. '21, sees if it is a soft hand. 
  46. FUNC HandValue(cards) 
  47.     LOCAL I, value 
  48.     value = Sum(cards) 
  49.     IF value > 21 THEN 
  50.         WHILE Sum(cards = 11)    'while there exists an ace 
  51.             FOR I = 1 TO EndValid(cards)    'flip the first 
  52.                                    'ace 
  53.                 IF cards[I] = 11 THEN 
  54.                     cards[I] = 1 
  55.                     EXIT FOR 
  56.                 END IF 
  57.             NEXT I 
  58.             value = Sum(cards)    'still busted? 
  59.             IF value < 22 THEN 
  60.                 EXIT WHILE 
  61.             END IF 
  62.         END WHILE 
  63.     END IF 
  64.     RETURN value 
  65. END FUNC 
  66.  
  67. 'Determine outcome of game, and adjust score appropriately 
  68. PROC DoScore(dlhand,playhand,playn) 
  69.     IF Bust[playn] THEN 
  70.         EXIT PROC 
  71.     END IF 
  72.     SELECT CASE dlhand 
  73.     CASE IS > 21 
  74.         IF NHands = 1 THEN 
  75.             INPUT "Dealer Broke"; 
  76.         ELSE 
  77.             INPUT "Dealer Broke -- Hand" + Sprint("##", playn) + " wins"; 
  78.         END IF 
  79.         Score = Score + Bet 
  80.     CASE IS > playhand 
  81.         IF NHands = 1 THEN 
  82.             INPUT "You Lose"; 
  83.         ELSE 
  84.             INPUT "Hand" + Sprint("##", playn) + " loses"; 
  85.         END IF 
  86.         Score = Score - Bet 
  87.     CASE playhand 
  88.         IF NHands = 1 THEN 
  89.             INPUT "Push"; 
  90.         ELSE 
  91.             INPUT "Push -- Hand" + Sprint("##", playn); 
  92.         END IF 
  93.     CASE ELSE 
  94.         IF NHands = 1 THEN 
  95.             INPUT "You Win"; 
  96.         ELSE 
  97.             INPUT "Hand" + Sprint("##", playn) + " wins"; 
  98.         END IF 
  99.         Score = Score + Bet 
  100.     END SELECT 
  101. END PROC 
  102.  
  103. 'Deal one card.  tdeck contains the cards in that deck, which 
  104. 'is 1 for the dealer and 2 or 3 for the player 
  105. PROC DealCard(tdeck,which) 
  106.     LOCAL ncards, crd 
  107.     ncards = CardsHand[which] 
  108.     crd = Deck[NextCard] 
  109.     IF which = 1 THEN 
  110.         FormSetObject(which*100 + ncards, _Bitmap, CardName(crd), 18*ncards pct, 12 pct) 
  111.     ELSE 
  112.         FormSetObject(which*100 + ncards, _Bitmap, CardName(crd), (which - 2)*20 + 2 pct, 7*ncards + 52 pct) 
  113.     END IF 
  114.     CardsHand[which] = ncards + 1 
  115.     tdeck[ncards+1] = CardValue(crd) 
  116.     NextCard = NextCard + 1 
  117. END PROC 
  118.  
  119. PROC MakeForm 
  120.     LOCAL fontTitle, fontCaption
  121.  
  122.     fontTitle = FontQUnique
  123.     FontNew(fontTitle; "helv", 24, _Bold)
  124.     fontCaption = FontQUnique
  125.     FontNew(fontCaption; "tms rmn", 12, _Bold)
  126.     formBJ = FormQUnique
  127.     FormNew(formBJ; "BlackJack", _Title + _Close + _Minimize) 
  128.     FormControl(_Size; _Center, _Center, 96 pct, 96 pct) 
  129.     FormSetObject(6, _CaptionCenter, "Realizer Casino -- Blackjack", fontTitle, 0 pct, 1 pct, 100 pct, _Default)
  130.     FormSetObject(Hit, _DefButton, "Deal", 2 pct, 42 pct, 13 pct, _Default) 
  131.     FormSetObject(Stand, _Button, "Quit", 22 pct, 42 pct, 13 pct, _Default) 
  132.     FormSetObject(210, _GroupBox, "", 47 pct, 53 pct, 51 pct, 46 pct)
  133.     FormSetObject(208, _CaptionCenter, "Double down on first two cards only.", fontCaption, 50 pct, 56 pct, 47 pct, _Default; _Blue)
  134.     FormSetObject(207, _CaptionCenter, "Dealer hits on 16, stands on 17.", fontCaption, 50 pct, 61 pct, 47 pct, _Default; _Blue)
  135.     FormSetObject(211, _CaptionCenter, "$5 minimum bet. $300 maximum bet.", fontCaption, 50 pct, 66 pct, 47 pct, _Default; _Blue)
  136.     FormSetObject(209, _CaptionCenter, "Blackjack pays 2-1.", fontCaption, 50 pct, 71 pct, 47 pct, _Default; _Blue)
  137.     FormSetObject(4, _CaptionCenter, Sprint("You have $#####.##", Score), fontCaption, 50 pct, 78 pct, 47 pct, _Default; _Red)
  138.     FormSetObject(5, _CaptionLeft, "Bet:", fontCaption, 65 pct, 87 pct; _Red)
  139.     FormSetObject(3, _EditText, "5.00", 72 pct, 86.5 pct, 12 pct, _Default)
  140.     FormSetProc(formprocBJ) 
  141.     FormControl(_Show) 
  142.     FontSelect(fontCaption)
  143.     FontControl(_Close)
  144.     FontSelect(fontTitle)
  145.     FontControl(_Close)
  146. END PROC 
  147.  
  148. 'Change Hit/Stand buttons to Deal/Quit 
  149. PROC SetBut1 
  150.     FormModifyObject(Hit, _Normal, "Deal") 
  151.     FormModifyObject(Stand, _Normal, "Quit") 
  152. END PROC 
  153.  
  154. 'Display Hit, Stand, Double, Split, and Surrender buttons 
  155. PROC SetBut2 
  156.     FormModifyObject(Hit, _Normal, "Hit") 
  157.     FormModifyObject(Stand, _Normal, "Stand") 
  158.     FormSetObject(Double, _Button, "Double", 42 pct, 42 pct, 13 pct, _Default) 
  159.     FormSetObject(Split, _Button, "Split", 62 pct, 42 pct, 13 pct, _Default) 
  160.     FormSetObject(Surrender, _Button, "Surrender", 82 pct,  42 pct, 13 pct, _Default) 
  161. END PROC 
  162.  
  163. 'Remove Double, Split, Surrender buttons.  Remove all cards. 
  164. 'Update score. 
  165. PROC ClearForm 
  166.     LOCAL I,J 
  167.     FOR I = 12 TO 14 
  168.         FormModifyObject(I, _Close) 
  169.     NEXT I 
  170.     FOR J = 0 TO NHands 
  171.         FOR I = 1 TO CardsHand[J+1] 
  172.             FormModifyObject(J*100 + I + 99, _Close) 
  173.         NEXT I 
  174.     NEXT J 
  175.     FormModifyObject(4, _Normal, Sprint("You have $#####.##", Score)) 
  176. END PROC 
  177.  
  178. 'This procedure processes clicks on buttons and cards. 
  179. 'buttonp contains the number of the form item clicked upon. 
  180. 'Returns 1 if the hand is over 
  181. FUNC ProcessButton(buttonp) 
  182.     LOCAL junk, button 
  183.     button = buttonp 
  184.     'Fake the proper hit 
  185.     IF button = Hit THEN 
  186.         button = (1 + CurHand)*100 
  187.     END IF 
  188.     SELECT CASE button 
  189.     CASE Surrender 
  190.         Score = Score - Bet/2 
  191.         RETURN 1 
  192.     CASE 200 TO 299    'Clicked in hand 1 
  193.         IF CurHand = 2 THEN 
  194.             RETURN 0 
  195.         END IF 
  196.         DealCard(Players, 2) 
  197.         IF HandValue(Players) > 21 THEN 
  198.             Score = Score - Bet 
  199.             Bust[1] = 1 
  200.             INPUT "You Broke"; 
  201.             IF NHands = 1 THEN 
  202.                 RETURN 1 
  203.             ELSE 
  204.                 CurHand = 2 
  205.                 RETURN 0 
  206.             END IF 
  207.         END IF 
  208.     CASE IS >= 300    'Clicked in hand 2 
  209.         CurHand = 2 
  210.         DealCard(Players2, 3) 
  211.         IF HandValue(Players2) > 21 THEN 
  212.             Score = Score - Bet 
  213.             INPUT "You Broke"; 
  214.             Bust[2] = 1 
  215.             IF Bust[1] THEN 
  216.                 RETURN 1 
  217.             ELSE 
  218.                 RETURN ProcessButton(Stand) 
  219.             END IF 
  220.         END IF 
  221.     CASE Stand 
  222.         IF CurHand <> NHands THEN 
  223.             CurHand = CurHand + 1 
  224.             EXIT SELECT 
  225.         END IF 
  226.         FormSetObject(100, _Bitmap, CardName(DownCard), 2 pct, 12 pct)    'Dealers turn 
  227.         WHILE HandValue(Dealers) < 17 
  228.             DealCard(Dealers, 1) 
  229.         END WHILE 
  230.         junk = HandValue(Dealers)    'Update score 
  231.         DoScore(junk,HandValue(Players), 1) 
  232.         IF NHands = 2 THEN 
  233.             DoScore(junk, HandValue(Players2), 2) 
  234.         END IF 
  235.         RETURN 1 
  236.     CASE Double 
  237.         Bet = 2*Bet 
  238.         IF NOT ProcessButton(Hit) THEN      'Deal one card 
  239.             junk = ProcessButton(Stand)    'Dealers turn 
  240.         END IF 
  241.         RETURN 1 
  242.     CASE Split 
  243.         NHands = 2 
  244.         'Be tricky 
  245.         NextCard = NextCard - 1 
  246.         IF Players[1] = 1 THEN 
  247.             Players[1] = 11    'If splitting aces,  
  248.                         'change first ace back 
  249.                         'to an 11 
  250.         END IF 
  251.         DealCard(Players2, 3) 
  252.         CardsHand[2] = 1 
  253.         DealCard(Players, 2) 
  254.         DealCard(Players2, 3) 
  255.     END SELECT 
  256.     RETURN 0  
  257. END FUNC 
  258.  
  259. 'This procedure processes all events in the blackjack playing 
  260. 'form.  The global variable State can have three values:  0 means 
  261. 'that the Deal/Quit buttons are visible.  1 means that 
  262. 'the hands were just dealt, and thus the Hit and Stand buttons 
  263. 'are visible, as well as Double and Split if  
  264. 'applicable.  2 means that the player has already played one 
  265. 'card, and thus cannot Double or Split. 
  266. PROC formprocBJ(params) 
  267.     'Only process buttons 
  268.     FormSelect(params[_FormNum])
  269.     IF params[_Invoke] <> _Click THEN 
  270.         EXIT PROC 
  271.     END IF 
  272.     event = params[_ItemNum]    'See what button was hit 
  273.     IF NOT State THEN    'State indicates the state -- if it 
  274.                       'is zero, expect a Deal or a Quit 
  275.         IF event = Stand THEN    'Really a Quit button 
  276.             'Close the form 
  277.             FormControl(_Close)
  278.             EXIT PROC 
  279.         ELSEIF event = Hit THEN    'Really a Deal button 
  280.             Bet = StrToNum(FormQStr(3)) 
  281.             IF Bet >= 5 and Bet <= 300 THEN 
  282.                 'Switch states 
  283.                 State = 1 
  284.                 'Do initialization 
  285.                 Bust[1] = 0 
  286.                 Bust[2] = 0 
  287.                 CardsHand[1] = 1 
  288.                 CardsHand[2] = 0 
  289.                 CardsHand[3] = 0 
  290.                 NHands = 1 
  291.                 CurHand = 1 
  292.                 SetBut2 
  293.                 'Clear all cards from the deck arrays 
  294.                 CLEAR Players, Players2, Dealers 
  295.                 'Check for reshuffle 
  296.                 IF NumCards - NextCard < 20 THEN 
  297.                     ShuffleDeck 
  298.                 END IF 
  299.                 'Deal dealer 
  300.                 FormSetObject(100, _Bitmap, "back.bmp", 2 pct,  12 pct) 
  301.                 DownCard = Deck[NextCard] 
  302.                 Dealers[1] = CardValue(Deck[NextCard]) 
  303.                 NextCard = NextCard + 1 
  304.                 DealCard(Players, 2) 
  305.                 DealCard(Dealers, 1) 
  306.                 DealCard(Players, 2) 
  307.                 'Check for BlackJack 
  308.                 IF HandValue(Players) = 21 THEN 
  309.                     INPUT "BlackJack"; 
  310.                     Score = Score + 2*Bet 
  311.                     'Reset the form 
  312.                     ClearForm() 
  313.                     SetBut1() 
  314.                     'Now back to state 0 
  315.                     State = 0 
  316.                 ELSEIF Players[1] <> Players[2] AND  Players[1] <> 1 THEN 
  317.                     FormModifyObject(Split, _Gray) 
  318.                 END IF 
  319.             ELSE    'Bad bet 
  320.                 IF Bet < 5 THEN
  321.                     INPUT "Table minimum is $5";
  322.                 ELSE
  323.                     INPUT "There's a $300 Maximum at this table, Tex"; 
  324.                 END IF
  325.             END IF 
  326.         END IF 
  327.     ELSE        'Expecting Hit, Stand, etc. 
  328.         IF State = 1 THEN 
  329.             'Need to clear out buttons that can no longer use 
  330.             FormModifyObject(Split, _Gray) 
  331.             FormModifyObject(Surrender, _Gray) 
  332.             FormModifyObject(Double, _Gray) 
  333.             'And switch states 
  334.             State = 2 
  335.         END IF 
  336.         'Process the event.  If the return value is 0, then it 
  337.         'is time to reset and return to state 0 
  338.         IF ProcessButton(event) THEN 
  339.             ClearForm 
  340.             SetBut1 
  341.             State = 0 
  342.         END IF 
  343.     END IF 
  344. END PROC 
  345.  
  346.  
  347. 'Here is where the program begins 
  348.  
  349. NumCards = 52 
  350. Deck = ((Index(NumCards) - 1) Mod 52) + 1    'Initialize deck 
  351. NextCard = NumCards 
  352. ShowCount = 0 
  353. Finished = 0 
  354. Score = 0 
  355. Bet = 5 
  356. State = 0 
  357. hit = 1    
  358. stand = 11 
  359. double = 12 
  360. split = 13 
  361. surrender = 14 
  362.  
  363. RANDOMIZE StrToNum(Sprint("D(s1)", QDate)) / 7 
  364.  
  365. MakeForm()
  366.