Rem: Black Jack Simulator  Version 6.30
Rem: Programmer : Nakamura Masaaki (masaaki@iname.com)

Rem: form design
Rem:   txtTern as Textbox
Rem:   txtFile as Textbox
Rem:   lstPselect(0 to 6) as Listbox
Rem:   lstCardID(0 to 6) as Listbox
Rem:   lstCardV(0 to 6) as Listbox
Rem:   lblCardSum(0 to 6) as Label
Rem:   lblChip(0 to 6) as Label
Rem:   lblBJ(0 to 6) as Label
Rem:   lblWin(0 to 6) as Label
Rem:   lblEven(0 to 6) as Label
Rem:   lblLose(0 to 6) as Label
Rem:   lblBst(0 to 6) as Label
Rem:   lblWL(0 to 6) as Label

Const NC = 52
Const nAtd = 7 Rem: number of attendance

Dim i, j As Integer
Dim nTrn As Integer Rem: number of terns(games)
Dim Cid(0 To NC - 1) As Integer
 Rem: card ID number
Dim Tdis As Integer
 Rem: created ramdom number (0 to NC-1)
Dim nStay As Integer Rem: number of Stayers
Dim pStay(0 To nAtd - 1) As Integer Rem: each attendance is Staying or not
Dim nA(0 To nAtd - 1) As Integer
 Rem: number of Aces each attendance has
Dim CardSum(0 To nAtd - 1) As Integer
 Rem: temporary sumation of each attendance's card
Dim BJ(0 To nAtd - 1) As Integer
 Rem: each attendance is Black Jack or not
Dim FinalResult(0 To nAtd - 1) As Integer
 Rem: each attendance's final result
Dim pCredit(0 To nAtd - 1) As Integer
 Rem: number of Chips (each attendance)
Dim pSession As Integer 'present session number
Dim Rc1(12 To 30) As Single
  Rem: expected credit by each final result
Dim Shoe(1 To 10) As Integer

Dim dSum(16 To 30) As Integer
 Rem: dealer's sum
Dim uCard As Integer Rem: dealer's up card
Dim dUF(1 To 10, 17 To 22) As Single
  Rem: expectation of dealer's final result from his upcard
Dim nBJ(0 To nAtd - 1) As Integer
Dim nWin(0 To nAtd - 1) As Integer
Dim nEven(0 To nAtd - 1) As Integer
Dim nLose(0 To nAtd - 1) As Integer
Dim sD6(1 To 10, 12 To 30) As Integer
  Rem: simple decision of StrategyD2 player
Dim nBust(0 To nAtd - 1) As Single
Dim WL(0 To nAtd - 1) As Single

Private Sub Form_Load()

Dim i, j As Integer

lstPselect(0).AddItem "16"
lstPselect(0).AddItem "17"
lstPselect(0).AddItem "18"
lstPselect(0).Selected(1) = True

For i = 1 To 6
  lstPselect(i).AddItem "A1"
  lstPselect(i).AddItem "A2"
  lstPselect(i).AddItem "A3"
  lstPselect(i).AddItem "B1"
  lstPselect(i).AddItem "B2"
  lstPselect(i).AddItem "B3"
  lstPselect(i).AddItem "C1"
  lstPselect(i).AddItem "C2"
  lstPselect(i).AddItem "C3"
  lstPselect(i).Selected(i - 1) = True
Next

Rc1(12) = -0.84
Rc1(13) = -0.84
Rc1(14) = -0.82
Rc1(15) = -0.75
Rc1(16) = -0.8
Rc1(17) = -0.39
Rc1(18) = 0.11
Rc1(19) = 0.71
Rc1(20) = 1.25
Rc1(21) = 1.87
For i = 22 To 30
  Rc1(i) = -2
Next

For i = 2 To 6
  For j = 17 To 21
    dUF(i, j) = 0.12
  Next
Next
For i = 7 To 9
  For j = 17 To 21
    If j - i = 10 Then
      dUF(i, j) = 0.33
    Else
      dUF(i, j) = 0.11
    End If
  Next
Next
For i = 17 To 19
  dUF(10, i) = 0.13
Next
dUF(10, 20) = 0.33
dUF(10, 21) = 0.05
For i = 17 To 20
  dUF(1, i) = 0.19
Next
dUF(1, 21) = 0.05
For i = 2 To 6
  dUF(i, 22) = 0.4
Next
For i = 7 To 10
  dUF(i, 22) = 0.23
Next
dUF(1, 22) = 0.19

For i = 1 To 10
  For j = 12 To 16
    sD6(i, j) = 1
  Next
  For j = 17 To 30
    sD6(i, j) = 0
  Next
Next
For i = 2 To 6
  For j = 14 To 16
    sD6(i, j) = 0
  Next
Next
sD6(8, 17) = 1

End Sub

Private Sub cmdOK_Click()

  Dim i As Integer
  
  If Len(txtTern.Text) = 0 Then
    txtTern.Text = 1
  End If
  
  nTrn = Val(txtTern.Text) Rem: how many terns(games) ?
    
  If Len(txtFile.Text) = 0 Then
    txtFile.Text = "BJ_data"
  End If
  
  pSession = 0 Rem: present session number
 
  For i = 0 To nAtd - 1
    pCredit(i) = 0
    nBJ(i) = 0
    nWin(i) = 0
    nEven(i) = 0
    nLose(i) = 0
    nBust(i) = 0
  Next
  
  Shuffle
  
  For i = 0 To nTrn - 1
    singleSession
    pSession = pSession + 1
    lblPSession.Caption = pSession
  Next
  
  Open "C:\Windows\Temp\" " txtFile.Text " ".dat" For Output As #1
    Print #1, txtTern.Text " " games"
    Print #1, "Player," " lstPselect(0).List(lstPselect(0).ListIndex) " "," " lstPselect(1).List(lstPselect(1).ListIndex) " "," " lstPselect(2).List(lstPselect(2).ListIndex) " "," " lstPselect(3).List(lstPselect(3).ListIndex) " "," " lstPselect(4).List(lstPselect(4).ListIndex) " "," " lstPselect(5).List(lstPselect(5).ListIndex) " "," " lstPselect(6).List(lstPselect(6).ListIndex)
    Print #1, "Chip," " pCredit(0) " "," " pCredit(1) " "," " pCredit(2) " "," " pCredit(3) " "," " pCredit(4) " "," " pCredit(5) " "," " pCredit(6)
    Print #1, "BJ," " nBJ(0) " "," " nBJ(1) " "," " nBJ(2) " "," " nBJ(3) " "," " nBJ(4) " "," " nBJ(5) " "," " nBJ(6)
    Print #1, "Win," " nWin(0) " "," " nWin(1) " "," " nWin(2) " "," " nWin(3) " "," " nWin(4) " "," " nWin(5) " "," " nWin(6)
    Print #1, "Even," " nEven(0) " "," " nEven(1) " "," " nEven(2) " "," " nEven(3) " "," " nEven(4) " "," " nEven(5) " "," " nEven(6)
    Print #1, "Lose," " nLose(0) " "," " nLose(1) " "," " nLose(2) " "," " nLose(3) " "," " nLose(4) " "," " nLose(5) " "," " nLose(6)
    Print #1, "Bust," " nBust(0) " "," " nBust(1) " "," " nBust(2) " "," " nBust(3) " "," " nBust(4) " "," " nBust(5) " "," " nBust(6)
  Close #1
  
End Sub

Private Sub singleSession()
  
  Dim Temp As Integer
  Dim j, k As Integer
  
  nStay = 0
  For j = 0 To nAtd - 1
    nA(j) = 0
    CardSum(j) = 0
    BJ(j) = 0
    FinalResult(j) = 0
    pStay(j) = 0
    lstCardID(j).Clear
    lstCardV(j).Clear
  Next
  
  Rem: if cards left are little, shuffle agein
  If lstDistribute.ListCount < nAtd * 4 Then
    Shuffle
  End If
  
  Rem: note dealer's up card
  uCard = CardValue(Val(lstDistribute.List(0)))

  Rem: hit 2 cards
  For k = 0 To nAtd - 1
    Hit (k)
  Next
  For k = 0 To nAtd - 1
    Hit (k)
  Next
  
  Rem: BLACK JACK ?
  For j = 0 To nAtd - 1
    If (lstCardID(j).List(0) Mod 13 = 0 And (lstCardID(j).List(1) Mod 13 > 9)) Or (lstCardID(j).List(1) Mod 13 = 0 And (lstCardID(j).List(0) Mod 13 > 9)) Then
      BJ(j) = 1
    End If
  Next  
  
  If BJ(0) = 1 Then    Rem: if dealer is Black Jack
    pStay(0) = 1
    nStay = nStay + 1
    nBJ(0) = nBJ(0) + 1
    lblCardSum(0).Caption = "BJ"
    For j = 1 To nAtd - 1
      pStay(j) = 1
      nStay = nStay + 1
      If BJ(j) = 1 Then Rem: when EVEN(player is also BJ)
        nBJ(j) = nBJ(j) + 1
        lblCardSum(j).Caption = "BJ"
      Else Rem: when player lost
        pCredit(0) = pCredit(0) + 2
        pCredit(j) = pCredit(j) - 2
      End If
    Next
  Else  Rem: if not dealer is Black Jack
    For j = 1 To nAtd - 1
      If BJ(j) = 1 Then  Rem: if player is BJ
        pCredit(0) = pCredit(0) - 3
        pCredit(j) = pCredit(j) + 3
        pStay(j) = 1
        nStay = nStay + 1
        nBJ(j) = nBJ(j) + 1
        lblCardSum(j).Caption = "BJ"
      End If
    Next
    Do While nStay < nAtd
      singleTern
    Loop
  End If
  singleTern
  
  Rem: credit(Chip) calculation
  For j = 1 To nAtd - 1
    If BJ(0) = 0 And BJ(j) = 0 Then
      If (FinalResult(j) < 22) And ((FinalResult(0) > 21) Or (FinalResult(j) > FinalResult(0))) Then
        pCredit(0) = pCredit(0) - 2
        pCredit(j) = pCredit(j) + 2
        nWin(j) = nWin(j) + 1
        nLose(0) = nLose(0) + 1
      Else
        If (FinalResult(j) > 21) Or ((FinalResult(j) < FinalResult(0)) And (FinalResult(0) < 22)) Then
          pCredit(0) = pCredit(0) + 2
          pCredit(j) = pCredit(j) - 2
          nLose(j) = nLose(j) + 1
          nWin(0) = nWin(0) + 1
        Else
          If FinalResult(0) = FinalResult(j) Then
            nEven(0) = nEven(0) + 1
            nEven(j) = nEven(j) + 1
          End If
        End If
      End If
    End If
  Next
  
  For j = 0 To nAtd - 1
    lblChip(j).Caption = pCredit(j)
    lblBJ(j).Caption = nBJ(j)
    lblWin(j).Caption = nWin(j)
    lblEven(j).Caption = nEven(j)
    lblLose(j).Caption = nLose(j)
  Next

End Sub

Private Sub singleTern()
  
  Dim i As Integer
  
  Select Case lstPselect(0).ListIndex
    Case 0
      Strategy16 (0)
    Case 1
      Strategy17 (0)
    Case 2
      Strategy18 (0)
  End Select
  
  For i = 1 To 6
    Select Case lstPselect(i).ListIndex
      Case 0
        StrategyA1 (i)
      Case 1
        StrategyA2 (i)
      Case 2
        StrategyA3 (i)
      Case 3
        StrategyB1 (i)
      Case 4
        StrategyB2 (i)
      Case 5
        StrategyB3 (i)
      Case 6
        StrategyC1 (i)
      Case 7
        StrategyC2 (i)
      Case 8
        StrategyC3 (i)
    End Select
  Next

End Sub

Private Sub Shuffle()

 Dim Temp As Integer
 Dim i As Integer

 lstDistribute.Clear
 For i = 0 To NC - 1
   Cid(i) = i
 Next

 Randomize
 
 For i = 0 To NC - 1
   Tdis = Int(Rnd * (NC - i))
     Rem: which card(still left) to take
   Temp = Cid(i)
   Cid(i) = Cid(Tdis + i)
   Cid(Tdis + i) = Temp
 Next Rem: Cid(i) is randomly re-ordered.

 For i = 0 To NC - 1
   lstDistribute.List(i) = Cid(i)
 Next
 
End Sub

Private Sub Hit(p As Integer)

  Dim t, Temp As Integer

  Rem: draw a card from shuffled stock
  If CardSum(p) < 21 Then
    lstCardID(p).AddItem lstDistribute.List(0), 0
    lstDistribute.RemoveItem 0
  End If

  Rem: sumation of one's card ...
  
  t = Val(lstCardID(p).List(0)) Mod 13 + 1
  Select Case t
    Case 11, 12, 13
      t = 10
    Case 1
      nA(p) = nA(p) + 1
      t = 11
  End Select
  lstCardV(p).AddItem t, 0

  CardSum(p) = 0
  For j = 0 To lstCardV(p).ListCount - 1
    CardSum(p) = CardSum(p) + lstCardV(p).List(j)
  Next
  Rem: ...sumation is over

  Rem: if the attendance has Ace(s) ..
  If CardSum(p) > 21 Then
    Temp = nA(p)
    Do While (Temp > 0) And (CardSum(p) > 10)
      CardSum(p) = CardSum(p) - 10
      Temp = Temp - 1
    Loop
  End If

  Rem: show result
  lblCardSum(p).Caption = CardSum(p)
    
End Sub

Function CardValue(ID As Integer)
 Rem: value is 1 to 10
 Rem: NOTE!  Ace is counted as 1
 
  Dim t As Integer

  t = ID Mod 13 + 1
  Select Case t
    Case 11, 12, 13
      t = 10
  End Select
  CardValue = t
  
End Function

Private Sub ShoeCount()
  
  For i = 1 To 10
    Shoe(i) = 0
  Next
  For i = 0 To lstDistribute.ListCount - 1
    Shoe(CardValue(lstDistribute.List(i))) = Shoe(CardValue(lstDistribute.List(i))) + 1
  Next

End Sub

Function Middle() As Single

  Dim tempN, q As Integer
  Dim m As Single

  ShoeCount
  
  tempN = 0
  q = 0
  m = 0
  If lstDistribute.ListCount Mod 2 = 1 Then
    Do While tempN < (lstDistribute.ListCount + 1) / 2
      q = q + 1
      m = m + 1
      tempN = tempN + Shoe(q)
    Loop
  Else
    Do While tempN < lstDistribute.ListCount / 2
      q = q + 1
      m = m + 1
      tempN = tempN + Shoe(q)
    Loop
    If tempN = lstDistribute.ListCount / 2 Then
      m = m + 0.5
    End If
  End If
  
  Middle = m
  
End Function

Private Sub Strategy17(p As Integer)
 Rem: hit under 16, stay over 17
  
  If pStay(p) = 0 Then
    If CardSum(p) < 17 Then
      Hit (p)
    Else
      FinalResult(p) = CardSum(p)
      nStay = nStay + 1
      pStay(p) = 1
      If CardSum(p) > 21 Then
        nBust(p) = nBust(p) + 1
      End If
      WL(p) = nWin(p) - nLose(p)
      lblBst(p).Caption = nBust(p)
      lblWL(p).Caption = WL(p)
    End If
  End If
  
End Sub

Private Sub Strategy16(p As Integer)
  
  If pStay(p) = 0 Then
    If CardSum(p) < 16 Then
      Hit (p)
    Else
      FinalResult(p) = CardSum(p)
      nStay = nStay + 1
      pStay(p) = 1
      If CardSum(p) > 21 Then
        nBust(p) = nBust(p) + 1
      End If
      WL(p) = nWin(p) - nLose(p)
      lblBst(p).Caption = nBust(p)
      lblWL(p).Caption = WL(p)
    End If
  End If
  
End Sub

Private Sub Strategy18(p As Integer)
  
  If pStay(p) = 0 Then
    If CardSum(p) < 18 Then
      Hit (p)
    Else
      FinalResult(p) = CardSum(p)
      nStay = nStay + 1
      pStay(p) = 1
      If CardSum(p) > 21 Then
        nBust(p) = nBust(p) + 1
      End If
      WL(p) = nWin(p) - nLose(p)
      lblBst(p).Caption = nBust(p)
      lblWL(p).Caption = WL(p)
    End If
  End If
  
End Sub

Private Sub StrategyS(p As Integer) Rem: simplest strategy
  
  If pStay(p) = 0 Then
    If CardSum(p) < 14 Then
      Hit (p)
    Else
      FinalResult(p) = CardSum(p)
      nStay = nStay + 1
      pStay(p) = 1
      If CardSum(p) > 21 Then
        nBust(p) = nBust(p) + 1
      End If
      WL(p) = nWin(p) - nLose(p)
      lblBst(p).Caption = nBust(p)
      lblWL(p).Caption = WL(p)
    End If
  End If
  
End Sub

Private Sub StrategyA1(p As Integer)  Rem: Middle value user
  
  If pStay(p) = 0 Then
    If CardSum(p) < 12 Then
      Hit (p)
    Else
      If CardSum(p) + Middle <= 21 Then
        Hit (p)
      Else
        FinalResult(p) = CardSum(p)
        nStay = nStay + 1
        pStay(p) = 1
        If CardSum(p) > 21 Then
          nBust(p) = nBust(p) + 1
        End If
        WL(p) = nWin(p) - nLose(p)
        lblBst(p).Caption = nBust(p)
        lblWL(p).Caption = WL(p)
      End If
    End If
  End If
    
End Sub

Private Sub StrategyA2(p As Integer)
  
  Dim rMid As Single 'roughly counted middle value
  
  If pStay(p) = 0 Then
    If CardSum(p) < 12 Then
      Hit (p)
    Else
      ShoeCount
      Select Case Shoe(10) - lstDistribute.ListCount / 2
        Case Is < 0
          rMid = 5
        Case Is = 0
          rMid = 7.5
        Case Is > 0
          rMid = 10
      End Select
      If CardSum(p) + rMid <= 20 Then
        Hit (p)
      Else
        FinalResult(p) = CardSum(p)
        nStay = nStay + 1
        pStay(p) = 1
        If CardSum(p) > 21 Then
          nBust(p) = nBust(p) + 1
        End If
        WL(p) = nWin(p) - nLose(p)
        lblBst(p).Caption = nBust(p)
        lblWL(p).Caption = WL(p)
      End If
    End If
  End If
    
End Sub

Private Sub StrategyA3(p As Integer)
  
  If pStay(p) = 0 Then
    If CardSum(p) < 15 Then
      Hit (p)
    Else
      FinalResult(p) = CardSum(p)
      nStay = nStay + 1
      pStay(p) = 1
      If CardSum(p) > 21 Then
        nBust(p) = nBust(p) + 1
      End If
      WL(p) = nWin(p) - nLose(p)
      lblBst(p).Caption = nBust(p)
      lblWL(p).Caption = WL(p)
    End If
  End If
  
End Sub

Private Sub StrategyB1(p As Integer)  Rem: guess expected Chips

  Dim i, j As Integer
  Dim eH1, eH2 As Single
  
  If pStay(p) = 0 Then
    If CardSum(p) < 12 Then
      Hit (p)
    Else
      If CardSum(p) < 21 Then
        ShoeCount
        eH1 = 0
        eH2 = -2
        For i = 1 To 10
          eH1 = eH1 + Rc1(CardSum(p) + i) * Shoe(i) / lstDistribute.ListCount
          If CardSum(p) + i < 21 And Shoe(i) > 0 Then
            eH2 = 0
            For j = 1 To 10
              If i = j Then
                eH2 = eH2 + Rc1(CardSum(p) + i + j) * (Shoe(j) - 1) / (lstDistribute.ListCount - 1)
              Else
                eH2 = eH2 + Rc1(CardSum(p) + i + j) * Shoe(j) / (lstDistribute.ListCount - 1)
              End If
            Next
          End If
          If eH1 < eH2 Then
            eH = eH2
          Else
            eH = eH1
          End If
        Next
      End If
      If CardSum(p) < 21 And Rc1(CardSum(p)) < eH Then
        Hit (p)
      Else
        FinalResult(p) = CardSum(p)
        nStay = nStay + 1
        pStay(p) = 1
        If CardSum(p) > 21 Then
          nBust(p) = nBust(p) + 1
        End If
        WL(p) = nWin(p) - nLose(p)
        lblBst(p).Caption = nBust(p)
        lblWL(p).Caption = WL(p)
      End If
    End If
  End If
  
End Sub

Private Sub StrategyB2(p As Integer) Rem: rough expected chips
  
  Dim pF, pN As Single Rem: possibility to hit face card, or another
  Dim eH As Single Rem: expected chips after a hit
  
  If pStay(p) = 0 Then
    If CardSum(p) < 18 And CardSum(p) > 11 Then
      ShoeCount
      pF = Shoe(10) / lstDistribute.ListCount
      pN = (lstDistribute.ListCount - Shoe(10)) / (9 * lstDistribute.ListCount)
      eH = 0
      For i = 1 To 9
        eH = eH + pN * Rc1(CardSum(p) + i)
      Next
      eH = eH + pF * Rc1(CardSum(p) + 10)
    End If
    If CardSum(p) < 12 Then
      Hit (p)
    Else
      If eH >= Rc1(CardSum(p)) And CardSum(p) < 18 Then
        Hit (p)
      Else
        FinalResult(p) = CardSum(p)
        nStay = nStay + 1
        pStay(p) = 1
        If CardSum(p) > 21 Then
          nBust(p) = nBust(p) + 1
        End If
        WL(p) = nWin(p) - nLose(p)
        lblBst(p).Caption = nBust(p)
        lblWL(p).Caption = WL(p)
      End If
    End If
  End If
  
End Sub

Private Sub StrategyB3(p As Integer)
  
  If pStay(p) = 0 Then
    If CardSum(p) < 15 Then
      Hit (p)
    Else
      FinalResult(p) = CardSum(p)
      nStay = nStay + 1
      pStay(p) = 1
      If CardSum(p) > 21 Then
        nBust(p) = nBust(p) + 1
      End If
      WL(p) = nWin(p) - nLose(p)
      lblBst(p).Caption = nBust(p)
      lblWL(p).Caption = WL(p)
    End If
  End If
  
End Sub

Private Sub StrategyC1(p As Integer) Rem: guess from dealer's up card

  Dim eSum As Single Rem: expected value after a hit
  Dim eSum2 As Single Rem: temporary variable, when hit two cards,...
  Dim eH As Single Rem: expect of Win,  when hit
  Dim eH1 As Single  Rem: temporary number when hit one card
  Dim eH2 As Single Rem: ..when hit two cards, ..
  Dim eH2t As Single  Rem: temp number
  Dim eS As Single Rem: expect of Win,  when stay
  Dim i, j As Integer
  
  eH = 0
  eS = 0
  
  If CardSum(p) > 11 And CardSum(p) < 21 And pStay(p) = 0 Then
    
    ShoeCount
    
    Rem: if player hits
    For i = 1 To 10
      eH1 = 0
      eH2 = -1
      eSum = CardSum(p) + i
      If eSum > 21 Then
        eH1 = eH1 - 1
      Else
        For j = 17 To 21
          If j > eSum Then
            eH1 = eH1 - dUF(uCard, j)
          Else
            If j < eSum Then
              eH1 = eH1 + dUF(uCard, j)
            End If
          End If
        Next
        eH1 = eH1 + dUF(uCard, 22)
      End If
      eH1 = eH1 * Shoe(i) / lstDistribute.ListCount
     
      If eSum < 21 And Shoe(i) > 0 Then Rem: player can hit more card, if sum is small
        eH2 = 0
        For j = 1 To 10
          eH2t = 0
          eSum2 = eSum + j
          If eSum2 > 21 Then
              eH2t = eH2t - 1
          Else
            For k = 17 To 21
              If k > eSum2 Then
                  eH2t = eH2t - dUF(uCard, k)
              Else
                If k < eSum2 Then
                    eH2t = eH2t + dUF(uCard, k)
                End If
              End If
            Next
            eH2t = eH2t + dUF(uCard, 22)
          End If
          If i = j Then
            eH2 = eH2 + eH2t * (Shoe(j) - 1) / (lstDistribute.ListCount - 1)
          Else
            eH2 = eH2 + eH2t * Shoe(j) / (lstDistribute.ListCount - 1)
          End If
        Next
      End If
      If eH2 > eH1 Then
        eH = eH + eH2
      Else
        eH = eH + eH1
      End If
    Next
    
    Rem: if player stays, .....
    For i = 17 To 21
      If i > CardSum(p) Then
        eS = eS - dUF(uCard, i)
      Else
        If i < CardSum(p) Then
          eS = eS + dUF(uCard, i)
        End If
      End If
    Next
    eS = eS + dUF(uCard, 22)
  End If
    
  If pStay(p) = 0 Then
    If CardSum(p) < 12 Then
      Hit (p)
    Else
      If eH > eS Then
        Hit (p)
      Else
        FinalResult(p) = CardSum(p)
        nStay = nStay + 1
        pStay(p) = 1
        If CardSum(p) > 21 Then
          nBust(p) = nBust(p) + 1
        End If
        lblBst(p).Caption = nBust(p)
        WL(p) = nWin(p) - nLose(p)
        lblWL(p).Caption = WL(p)
      End If
    End If
  End If
  
End Sub

Private Sub StrategyC2(p As Integer) Rem: guess from dealer's up card

  Dim eSum As Single Rem: expected value after a hit
  Dim eSum2 As Single Rem: temporary variable, when hit two cards,...
  Dim eH As Single Rem: expect of Win,  when hit
  Dim eH1 As Single  Rem: temporary number when hit one card
  Dim eH2 As Single Rem: ..when hit two cards, ..
  Dim eH2t As Single  Rem: temp number
  Dim eS As Single Rem: expect of Win,  when stay
  Dim i, j As Integer
  Dim pF, pN As Single Rem: cf. B2's description
  
  eH = 0
  eS = 0
  
  If CardSum(p) > 11 And CardSum(p) < 21 And pStay(p) = 0 Then
    
    ShoeCount
      pF = Shoe(10) / lstDistribute.ListCount
      pN = (lstDistribute.ListCount - Shoe(10)) / (9 * lstDistribute.ListCount)
    
    Rem: if player hits
    For i = 1 To 10
      eH1 = 0
      eH2 = -1
      eSum = CardSum(p) + i
      If eSum > 21 Then
        eH1 = eH1 - 1
      Else
        For j = 17 To 21
          If j > eSum Then
            eH1 = eH1 - dUF(uCard, j)
          Else
            If j < eSum Then
              eH1 = eH1 + dUF(uCard, j)
            End If
          End If
        Next
        eH1 = eH1 + dUF(uCard, 22)
      End If
      If i = 10 Then
        eH1 = eH1 * pF
      Else
        eH1 = eH1 * pN
      End If
     
      If eSum < 21 And Shoe(i) > 0 Then Rem: player can hit more card, if sum is small
        eH2 = 0
        For j = 1 To 10
          eH2t = 0
          eSum2 = eSum + j
          If eSum2 > 21 Then
              eH2t = eH2t - 1
          Else
            For k = 17 To 21
              If k > eSum2 Then
                  eH2t = eH2t - dUF(uCard, k)
              Else
                If k < eSum2 Then
                    eH2t = eH2t + dUF(uCard, k)
                End If
              End If
            Next
            eH2t = eH2t + dUF(uCard, 22)
          End If
          If j = 10 Then
            eH2 = eH2 + eH2t * pF
          Else
            eH2 = eH2 + eH2t * pN
          End If
        Next
      End If
      If eH2 > eH1 Then
        eH = eH + eH2
      Else
        eH = eH + eH1
      End If
    Next
    
    Rem: if player stays, .....
    For i = 17 To 21
      If i > CardSum(p) Then
        eS = eS - dUF(uCard, i)
      Else
        If i < CardSum(p) Then
          eS = eS + dUF(uCard, i)
        End If
      End If
    Next
    eS = eS + dUF(uCard, 22)
  End If
    
  If pStay(p) = 0 Then
    If CardSum(p) < 12 Then
      Hit (p)
    Else
      If eH > eS Then
        Hit (p)
      Else
        FinalResult(p) = CardSum(p)
        nStay = nStay + 1
        pStay(p) = 1
        If CardSum(p) > 21 Then
          nBust(p) = nBust(p) + 1
        End If
        lblBst(p).Caption = nBust(p)
        WL(p) = nWin(p) - nLose(p)
        lblWL(p).Caption = WL(p)
      End If
    End If
  End If
  
End Sub

Private Sub StrategyC3(p As Integer)
  
  If pStay(p) = 0 Then
    If CardSum(p) < 12 Then
      Hit (p)
    Else
      If sD6(uCard, CardSum(p)) = 1 And CardSum(p) < 21 Then
        Hit (p)
      Else
        FinalResult(p) = CardSum(p)
        nStay = nStay + 1
        pStay(p) = 1
        If CardSum(p) > 21 Then
          nBust(p) = nBust(p) + 1
        End If
        WL(p) = nWin(p) - nLose(p)
        lblBst(p).Caption = nBust(p)
        lblWL(p).Caption = WL(p)
      End If
    End If
  End If
  
End Sub

Ìá¤ë