home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l408 / 2.img / EXAMPLES.EXE / EXAMPLES / BJ / SUBS.BAS < prev    next >
Encoding:
BASIC Source File  |  1992-10-30  |  12.0 KB  |  463 lines

  1. DefInt A-Z
  2.  
  3. Sub ShowHandIndicator ()
  4.     ' If the player has split hands, then show the arrow indicator for
  5.     ' the active hand.
  6.     If Split Then
  7.        BjForm.HandIndicator(ActivePlayer).Picture = BjForm.HandIndicator(0).Picture
  8.        BjForm.HandIndicator(3 - ActivePlayer).Picture = LoadPicture()
  9.     End If
  10.     
  11. End Sub
  12.  
  13. Sub Shuffle ()
  14.  
  15.     ' shuffle the cards
  16.     Static Check(53)
  17.     ' Set our check array to all 1's
  18.     For Count = 1 To 52
  19.          Check(Count) = 1
  20.     Next
  21.     ' Stack the deck(s)
  22.     For Count = 1 To CARDSINDECK * Decks
  23.         RandomNumber = Rnd(1) * CARDSINDECK
  24.         'if we haven't assigned this card yet for each deck
  25.         If Check(RandomNumber) <= Decks And RandomNumber <> 0 Then
  26.         Check(RandomNumber) = Check(RandomNumber) + 1' Mark it as assigned
  27.         Cards(Count) = RandomNumber
  28.         Else
  29.         Count = Count - 1    ' continue through loop
  30.         End If
  31.     Next
  32.     CardsLeft = CARDSINDECK * Decks
  33.     NextCard = 1
  34.  
  35. End Sub
  36.  
  37. Sub ShowBetAmount ()
  38.  
  39.     BjForm.BaseBetAmount.text = Str$(Bet(PLAYER))
  40.  
  41. End Sub
  42.  
  43. Sub ShowStakeAmount ()
  44.  
  45.     ' Show the stake in the text box
  46.     BjForm.Amount(0).Caption = Str$(Stake&)
  47.  
  48. End Sub
  49.  
  50. Function CardValue (Who%, WhichCard%)
  51.  
  52.     Temp% = Hands(Who%, WhichCard) Mod 13
  53.     If Temp% = 0 Then
  54.         Temp = 13       ' isolate to 1-13 (A-K)
  55.     End If
  56.     CardValue = Temp
  57.  
  58. End Function
  59.  
  60. Function HandValue (Who%)         ' calculate the value of a hand
  61.  
  62.     Aces = 0                ' Start with no aces
  63.     Tot = 0               ' and zero total
  64.     ' For each card
  65.     For WhichCard = 1 To HowManyCards(Who)
  66.         Cvalue = CardValue(Who, WhichCard)
  67.         ' If it's 10, J, Q or K
  68.         If Cvalue >= 10 Then
  69.             Cvalue = 10
  70.         ElseIf Cvalue = 1 Then ' If it's an ace
  71.         Aces = Aces + 1
  72.         End If
  73.         
  74.         ' Accumulate the total
  75.         Tot = Tot + Cvalue
  76.  
  77.         ' If hand totals eleven, and there's one ace, and only
  78.         ' two cards in the hand, then it must be blackjack!
  79.         If Tot = 11 And Aces = 1 And HowManyCards(Who) = 2 Then
  80.             PlayerHasBJ(Who) = 1
  81.             Tot = 21
  82.             Exit For
  83.         End If
  84.     Next
  85.     HandValue = Tot
  86.  
  87. End Function
  88.  
  89. Sub ShowFirst ()
  90.  
  91.     ' This sub adds text to the message if it's a split hand
  92.     If Split Then
  93.        If ActivePlayer = PLAYERB Then
  94.       Message$ = Message$ + " (2nd hand)"
  95.        Else
  96.       Message$ = Message$ + " (1st hand)"
  97.        End If
  98.     End If
  99.  
  100. End Sub
  101.  
  102. Sub YouLose ()
  103.  
  104.     Stake& = Stake& - Bet(ActivePlayer)
  105.     ShowStakeAmount
  106.     Message$ = "You lose your bet!"
  107.     ShowFirst
  108.     MsgBox Message$, 64, "Too Bad!"
  109.  
  110. End Sub
  111.  
  112. Sub YouPush ()
  113.  
  114.     Message$ = "This hand is a push. At least you didn't lose!"
  115.     ShowFirst
  116.     MsgBox Message$, 64, "Nobody Wins!"
  117.  
  118. End Sub
  119.  
  120. Sub YouWin ()
  121.  
  122.     Stake& = Stake& + Bet(ActivePlayer)
  123.     ShowStakeAmount
  124.     Message$ = "You win!"
  125.     ShowFirst
  126.     MsgBox Message$, 64, "Congratulations!"
  127.  
  128. End Sub
  129.  
  130. Sub YouWinBJ ()
  131.  
  132.     Stake& = Stake& + Bet(ActivePlayer) + Bet(ActivePlayer) / 2
  133.     ShowStakeAmount
  134.     Message$ = "You win 1.5 times your bet!"
  135.     ShowFirst
  136.     MsgBox Message$, 64, "LOOSE DEALER!"
  137.  
  138. End Sub
  139.  
  140. Sub YouLoseInsurance ()
  141.  
  142.     Stake& = Stake& - Bet(ActivePlayer) / 2
  143.     ShowStakeAmount
  144.     Message$ = "You lose your insurance bet!"
  145.     ShowFirst
  146.     MsgBox Message$, 64, "Oops!"
  147.  
  148. End Sub
  149.  
  150. Sub ShowValue (Who%)
  151.  
  152.     ' Calculates the hand value and displays it in a label
  153.     Total(Who) = HandValue(Who)
  154.     If Who = DEALER Then
  155.        DealerVisible = MhTrue
  156.     End If
  157.     
  158.     If PlayerHasBJ(Who) Then
  159.        BjForm.ScoreBox(Who%).ForeColor = RGB(128, 0, 0)    ' Red
  160.        BjForm.ScoreBox(Who%).Caption = "BLACKJACK!"
  161.        PlayerDone(Who) = 1
  162.        Exit Sub
  163.     End If
  164.     
  165.     BjForm.ScoreBox(Who%).ForeColor = RGB(0, 0, 0) ' Assume black
  166.     Temp = Total(Who)               ' Secondary hand value counter
  167.     Message$ = Str$(Total(Who))     ' Start with raw score
  168.     
  169.     ' If we're calculating dealer's score, adjust temp to account
  170.     ' for all possible values
  171.     Do While Who = DEALER And Temp <= 11 And Aces <> 0
  172.           Temp = Temp + 10
  173.           Aces = Aces - 1
  174.           ' If the dealer has 17 or more, he must stand
  175.           If Temp >= 17 Then
  176.          Total(Who) = Temp
  177.          Message$ = Str$(Temp)
  178.          PlayerDone(DEALER) = 1
  179.           End If
  180.     Loop
  181.  
  182.     ' Anytime a player has 21, we no longer offer a hit
  183.     If Total(Who) = 21 Then
  184.        PlayerDone(Who) = 1
  185.     End If
  186.  
  187.     ' If the player is done, then calculate final hand value
  188.     If PlayerDone(Who) Then
  189.         While Temp <= 11 And Aces <> 0
  190.            Temp = Temp + 10
  191.            Aces = Aces - 1
  192.         Wend
  193.         Total(Who) = Temp
  194.         Message$ = Str$(Temp)
  195.     Else
  196.        ' Calculate all possible values
  197.        While Temp <= 11 And Aces <> 0
  198.           Temp = Temp + 10
  199.           Message$ = Message$ + " or" + Str$(Temp)
  200.           Aces = Aces - 1
  201.        Wend
  202.     End If
  203.        If Temp = 21 Then
  204.           Total(Who) = 21
  205.           PlayerDone(Who) = 1
  206.           Message$ = "21"
  207.        ElseIf Total(Who) >= 22 Then
  208.           Busted(Who) = 1
  209.           PlayerDone(Who) = 1
  210.           BjForm.ScoreBox(Who%).ForeColor = RGB(128, 0, 0) ' Red
  211.           Message$ = Message$ + " and is busted"
  212.        End If
  213.     BjForm.ScoreBox(Who).Caption = Message$
  214.  
  215. End Sub
  216.  
  217. Sub DrawCard (Who%, WhichCard%, BackOrFace%)
  218.  
  219.     ' Draws a card
  220.  
  221.     WhichBox = Who * 10 + WhichCard - 1
  222.     BjForm.CardBox(WhichBox).Visible = MhTrue
  223.  
  224.     If BackOrFace = FACE Then
  225.        ' Copy previously loaded bitmap
  226.        Ind = Hands(Who, WhichCard)  ' Relative to one
  227.        Card = CardValue(Who, WhichCard) - 1   ' 0-12
  228.        Suit = Ind \ 13 + ((Ind Mod 13) = 0) ' 0-3
  229.        W% = 74: H% = 99 ' Bit map spacing in big BMP
  230.        X% = Card * W%
  231.        Y% = Suit * H%
  232.        W% = 72: H% = 97 ' Actual bitmap dimensions
  233.        R% = BitBlt(BjForm.CardBox(WhichBox).Hdc, 0, 0, W%, H%, FDeck.FullDeck.Hdc, X%, Y%, SRCCOPY)
  234.        BjForm.CardBox(WhichBox).Refresh
  235.     Else
  236.        ' Set up correct bitmap file to draw
  237.        Select Case WhichDeck
  238.          Case 0
  239.            BjForm.CardBox(WhichBox).Picture = SelectForm.MirageDeck.Picture
  240.          Case 1
  241.            BjForm.CardBox(WhichBox).Picture = SelectForm.BlueSq.Picture
  242.          Case 2
  243.            BjForm.CardBox(WhichBox).Picture = SelectForm.RedSq.Picture
  244.          Case 3
  245.            BjForm.CardBox(WhichBox).Picture = SelectForm.RedDi.Picture
  246.          Case 4
  247.            BjForm.CardBox(WhichBox).Picture = SelectForm.BlueDi.Picture
  248.        End Select
  249.     End If
  250.  
  251. End Sub
  252.  
  253. Sub Hit (Who%)
  254.  
  255.       If BjForm.DoubleButton.Enabled Then
  256.      BjForm.DoubleButton.Enabled = MhFalse
  257.       End If
  258.       If HowManyCards(Who) = 5 Then
  259.      ' Move all the cards over for this hand (overlap them)
  260.      ' and adjust the CardColumn array for this hand.
  261.      ' Get difference (x) between first two cards
  262.      Difference = BjForm.CardBox(1).Left - BjForm.CardBox(0).Left
  263.      For I = 2 To 9
  264.          WhichBox = Who * 10 + I - 1
  265.          CardColumn(Who, I) = CardColumn(Who, I - 1) + Difference / 2
  266.          BjForm.CardBox(WhichBox).Left = CardColumn(Who, I)
  267.      Next
  268.      R% = DoEvents()    ' Update the screen
  269.       End If
  270.       HowManyCards(Who) = HowManyCards(Who) + 1
  271.       Hands(Who, HowManyCards(Who)) = Cards(NextCard)
  272.       NextCard = NextCard + 1
  273.       CardsLeft = CardsLeft - 1
  274.       DrawCard Who, HowManyCards(Who), FACE
  275.  
  276. End Sub
  277.  
  278. Sub SetUpForDeal ()
  279.  
  280.     BjForm.HandIndicator(PLAYER).Picture = LoadPicture()
  281.     BjForm.HandIndicator(PLAYERB).Picture = LoadPicture()
  282.     BjForm.BaseBetAmount.Enabled = MhTrue
  283.     If BjForm.HitButton.Enabled Then
  284.        BjForm.HitButton.Enabled = MhFalse
  285.     End If
  286.     If BjForm.DoubleButton.Enabled Then
  287.        BjForm.DoubleButton.Enabled = MhFalse
  288.     End If
  289.     For I = 0 To 2
  290.     BjForm.DeckButton(I).Enabled = MhTrue
  291.     Next
  292.     HandInPlay = MhFalse
  293.     ActivePlayer = PLAYER
  294.  
  295. End Sub
  296.  
  297. Sub AnalyzeHand ()
  298.  
  299.    ' Show the dealer's hidden card
  300.    DrawCard DEALER, 2, FACE
  301.    ' and his hand value
  302.    ShowValue DEALER
  303.    If BjForm.HitButton.Enabled Then
  304.       BjForm.HitButton.Enabled = MhFalse
  305.    End If
  306.    ' if neither hand is busted
  307.    If (Busted(PLAYER) = 0 And Split = 0) Or (Split <> 0 And Busted(PLAYER) = 0) Or (Busted(PLAYERB) = 0 And Split <> 0) Then
  308.       ' if only 1 hand and he doesn't have bj or
  309.       ' 2 hands and one or other doesn't have bj
  310.       If (PlayerHasBJ(PLAYER) = 0 And Split = 0) Or (Split <> 0 And PlayerHasBJ(PLAYER) = 0 Or PlayerHasBJ(PLAYERB) = 0) Then
  311.      ' play for the dealer until >=17 or busts
  312.      While Total(DEALER) <= 16
  313.          Hit (DEALER)
  314.          ShowValue (DEALER)
  315.      Wend
  316.       End If
  317.     End If
  318.     PlayerDone(DEALER) = 1
  319.     For ActivePlayer = PLAYER To PLAYER + Split
  320.     If Busted(ActivePlayer) Then
  321.        Outcome(ActivePlayer) = LOSE
  322.     ElseIf PlayerHasBJ(ActivePlayer) Then
  323.        Outcome(ActivePlayer) = WINBJ
  324.     ElseIf Busted(DEALER) Then
  325.        Outcome(ActivePlayer) = WIN
  326.     ElseIf Total(DEALER) > Total(ActivePlayer) Then
  327.        Outcome(ActivePlayer) = LOSE
  328.     ElseIf Total(DEALER) < Total(ActivePlayer) Then
  329.        Outcome(ActivePlayer) = WIN
  330.     Else
  331.        Outcome(ActivePlayer) = PUSH
  332.     End If
  333.     ShowHandIndicator
  334.     Select Case Outcome(ActivePlayer)
  335.         Case WIN
  336.             YouWin
  337.         Case LOSE
  338.             YouLose
  339.         Case WINBJ
  340.             YouWinBJ
  341.         Case PUSH
  342.             YouPush
  343.     End Select
  344.     Next
  345.     
  346.     SetUpForDeal
  347.     If CardsLeft < CARDSINDECK / 4 Then
  348.     MsgBox "The cards have been shuffled!", 64, "The house doesn't like counters!"
  349.     Shuffle
  350.     End If
  351.     BjForm.StandButton.SetFocus
  352.  
  353. End Sub
  354.  
  355. Sub Deal ()
  356.  
  357.     ' Hide cards not in use
  358.     For Who = DEALER To PLAYERB
  359.         For WhichCard = 1 To HowManyCards(Who)
  360.         WhichBox = Who * 10 + WhichCard - 1
  361.         BjForm.CardBox(WhichBox).Picture = LoadPicture()
  362.         BjForm.CardBox(WhichBox).Visible = MhFalse
  363.         Next
  364.     Next
  365.  
  366.     ' Make unused boxes invisible
  367.     BjForm.ScoreBox(PLAYERB).Caption = ""
  368.     BjForm.ScoreBox(DEALER).Caption = ""
  369.     BjForm.PlayerBTextBox.Caption = ""
  370.  
  371.     ' Reset CardBox().Left in case a player had more than 5 cards
  372.     For I = DEALER To PLAYERB
  373.         For J = 2 To 5
  374.         If CardColumn(I, J) <> MasterCardColumn(I, J) Then
  375.            CardColumn(I, J) = MasterCardColumn(I, J)
  376.            BjForm.CardBox(I * 10 + J - 1).Left = CardColumn(I, J)
  377.         End If
  378.         Next
  379.     Next
  380.     
  381.     ' here's the actual deal
  382.     Hands(PLAYER, 1) = Cards(NextCard)
  383.     Hands(DEALER, 1) = Cards(NextCard + 1)
  384.     Hands(PLAYER, 2) = Cards(NextCard + 2)
  385.     Hands(DEALER, 2) = Cards(NextCard + 3)
  386.     NextCard = NextCard + 4
  387.     HowManyCards(DEALER) = 2
  388.     HowManyCards(PLAYER) = 2
  389.     DrawCard PLAYER, 1, FACE
  390.     DrawCard PLAYER, 2, FACE
  391.     DrawCard DEALER, 1, FACE
  392.     DrawCard DEALER, 2, BACK
  393.  
  394.     CardsLeft = CardsLeft - 4
  395.     HandInPlay = MhTrue
  396.     DealerVisible = MhFalse
  397.     Bet(PLAYER) = Bet(DEALER)
  398.     Bet(PLAYERB) = Bet(DEALER)
  399.  
  400. End Sub
  401.  
  402. Sub ShowAdvanceAmount ()
  403.     
  404.     ' Place the advance amount in the text box
  405.     BjForm.Amount(1).Caption = Str$(Advance&)
  406.  
  407. End Sub
  408.  
  409. Sub KeyDown (KeyCode As Integer, Shift As Integer)
  410.  
  411.     If KeyCode = HELPKEYCODE Then
  412.        If Len(HelpFileName$) Then
  413.       WinHelp BjForm.hWnd, HelpFileName$, HELP_INDEX, 0&
  414.        Else
  415.       MsgBox "Online help is not available inside the Visual Basic environment.", 64, "VB Bug!"
  416.        End If
  417.        Exit Sub
  418.     End If
  419.     ' This procedure acts on "quick keys" keypresses.
  420.     ' If the appropriate field is enabled, then
  421.     ' this proc simulates a mouse click on the field.
  422.     Select Case KeyCode
  423.       Case 66, 98'48  'B
  424.     If BjForm.BaseBetAmount.Enabled Then
  425.        BjForm.BaseBetAmount.SetFocus
  426.     End If
  427.       Case 49'2, 79  '1
  428.     If BjForm.DeckButton(0).Enabled Then
  429.        BjForm.DeckButton(0).Value = MhTrue
  430.     End If
  431.       Case 50'3, 80  ' 2
  432.     If BjForm.DeckButton(0).Enabled Then
  433.        BjForm.DeckButton(1).Value = MhTrue
  434.     End If
  435.       Case 54'7, 77 ' 6
  436.     If BjForm.DeckButton(0).Enabled Then
  437.        BjForm.DeckButton(2).Value = MhTrue
  438.     End If
  439.       Case 68, 100, 83, 115'31    'D/S
  440.     If BjForm.StandButton.Enabled Then
  441.        BjForm.StandButton.Value = MhTrue
  442.     End If
  443.       Case 72, 104'35    'H
  444.     If BjForm.HitButton.Enabled Then
  445.        BjForm.HitButton.Value = MhTrue
  446.     End If
  447.       Case 79, 111'11, 82'O
  448.     If BjForm.DoubleButton.Enabled Then
  449.        BjForm.DoubleButton.Value = MhTrue
  450.     End If
  451.       Case 80, 112'25    'P
  452.     If BjForm.SplitButton.Enabled Then
  453.        BjForm.SplitButton.Value = MhTrue
  454.     End If
  455.       Case 77, 109'50    'M
  456.     If BjForm.AdvanceButton.Enabled Then
  457.        BjForm.AdvanceButton.Value = MhTrue
  458.     End If
  459.     End Select
  460.     
  461. End Sub
  462.  
  463.