ssho88
ssho88
  • Threads: 59
  • Posts: 684
Joined: Oct 16, 2011
September 27th, 2024 at 6:55:23 PM permalink
In order to test the ability of AI, I just asked AI to write a Baccarat simulation software, but the results given are somewhat different from the theoretical results. Can anyone help check it?
BankerWins: 45.173%
PlayerWins: 45.594%
TieWins: 9.233%

Option Explicit

Const DECK_SIZE As Integer = 52
Const TOTAL_DECKS As Integer = 8
Const MAX_ROUNDS_PER_SHOE As Long = 10
Const SHOES_TO_RUN As Long = 1000000

Dim shoe() As Integer
Dim cardIndex As Integer
Dim playerWins As Long
Dim bankerWins As Long
Dim ties As Long
Dim totalPlayerWins As Long
Dim totalBankerWins As Long
Dim totalTies As Long

' Initialize the shoe with 8 decks of cards
Sub InitializeShoe()
Dim i As Integer, j As Integer, k As Integer
Dim cardValue As Integer

ReDim shoe(1 To TOTAL_DECKS * DECK_SIZE)
k = 1
For i = 1 To TOTAL_DECKS
For j = 1 To DECK_SIZE
cardValue = j Mod 13 ' Ace = 1, 2-9, 10, J, Q, K = 0
If cardValue > 9 Then cardValue = 0 ' Face cards are 0
shoe(k) = cardValue
k = k + 1
Next j
Next i
ShuffleShoe
cardIndex = 1 ' Reset card index
End Sub

' Fisher-Yates Shuffle algorithm to shuffle the shoe
Sub ShuffleShoe()
Dim i As Integer, j As Integer
Dim temp As Integer
For i = UBound(shoe) To 2 Step -1
j = Int(Rnd * i) + 1
temp = shoe(i)
shoe(i) = shoe(j)
shoe(j) = temp
Next i
End Sub

' Draw a card from the shoe
Function DrawCard() As Integer
If cardIndex > UBound(shoe) Then
DrawCard = -1 ' No more cards to draw
Else
DrawCard = shoe(cardIndex)
cardIndex = cardIndex + 1
End If
End Function

' Calculate the total value of a hand
Function CalculateHandValue(cards As Collection) As Integer
Dim total As Integer
Dim card As Variant
total = 0
For Each card In cards
total = (total + card) Mod 10 ' Baccarat only cares about last digit
Next card
CalculateHandValue = total
End Function

' Simulate a round of Baccarat
Function PlayBaccaratRound() As Boolean
Dim playerHand As New Collection
Dim bankerHand As New Collection
Dim playerValue As Integer
Dim bankerValue As Integer
Dim playerThirdCard As Integer
Dim bankerDraw As Boolean
Dim card As Integer

' Deal two cards to Player
card = DrawCard
If card = -1 Then PlayBaccaratRound = False: Exit Function
playerHand.Add card

card = DrawCard
If card = -1 Then PlayBaccaratRound = False: Exit Function
playerHand.Add card

' Deal two cards to Banker
card = DrawCard
If card = -1 Then PlayBaccaratRound = False: Exit Function
bankerHand.Add card

card = DrawCard
If card = -1 Then PlayBaccaratRound = False: Exit Function
bankerHand.Add card

' Calculate initial hand values
playerValue = CalculateHandValue(playerHand)
bankerValue = CalculateHandValue(bankerHand)

' Check for natural hand (8 or 9) or if both hands have 6 or 7
If playerValue >= 8 Or bankerValue >= 8 Or (playerValue = 6 Or playerValue = 7) And (bankerValue = 6 Or bankerValue = 7) Then
GoTo DetermineWinner ' No more drawing; check winner directly
End If

' Player draws a third card if needed
If playerValue <= 5 Then
playerThirdCard = DrawCard
If playerThirdCard = -1 Then PlayBaccaratRound = False: Exit Function
playerHand.Add playerThirdCard
playerValue = CalculateHandValue(playerHand)
Else
playerThirdCard = -1 ' No third card drawn
End If

' Banker draws a third card based on Baccarat rules
bankerDraw = False
If bankerValue <= 2 Then
bankerDraw = True
ElseIf bankerValue = 3 Then
If playerThirdCard <> 8 Then bankerDraw = True
ElseIf bankerValue = 4 Then
If playerThirdCard >= 2 And playerThirdCard <= 7 Then bankerDraw = True
ElseIf bankerValue = 5 Then
If playerThirdCard >= 4 And playerThirdCard <= 7 Then bankerDraw = True
ElseIf bankerValue = 6 Then
If playerThirdCard = 6 Or playerThirdCard = 7 Then bankerDraw = True
End If

' Banker draws third card if needed
If bankerDraw Then
card = DrawCard
If card = -1 Then PlayBaccaratRound = False: Exit Function
bankerHand.Add card
bankerValue = CalculateHandValue(bankerHand)
End If

DetermineWinner:
' Determine winner
If playerValue > bankerValue Then
playerWins = playerWins + 1
ElseIf bankerValue > playerValue Then
bankerWins = bankerWins + 1
Else
ties = ties + 1
End If

PlayBaccaratRound = True
End Function

' Main simulation procedure for a single shoe
Sub BaccaratSimulation()
Dim result As Boolean
Dim roundsPlayed As Long

' Initialize shoe
InitializeShoe

' Reset results for this shoe
playerWins = 0
bankerWins = 0
ties = 0
roundsPlayed = 0

' Play rounds until reaching 70 rounds
Do While roundsPlayed < MAX_ROUNDS_PER_SHOE
result = PlayBaccaratRound
If result Then
roundsPlayed = roundsPlayed + 1
Else
Exit Do
End If
Loop

' Add the results of this shoe to the totals
totalPlayerWins = totalPlayerWins + playerWins
totalBankerWins = totalBankerWins + bankerWins
totalTies = totalTies + ties

' Prepare for next shoe
cardIndex = 1 ' Reset card index
ShuffleShoe ' Shuffle for new shoe
End Sub

' Run simulation for a specified number of shoes
Sub RunMultipleShoes()
Dim shoeNum As Long

' Initialize totals
totalPlayerWins = 0
totalBankerWins = 0
totalTies = 0

' Run simulation for each shoe
For shoeNum = 1 To SHOES_TO_RUN
BaccaratSimulation
Next shoeNum

' Display final results after all shoes
MsgBox "Simulation complete!" & vbCrLf & _
"Shoes Played: " & SHOES_TO_RUN & vbCrLf & _
"Total Player Wins: " & totalPlayerWins & vbCrLf & _
"Total Banker Wins: " & totalBankerWins & vbCrLf & _
"Total Ties: " & totalTies & vbCrLf & _
"Tie Percentage: " & Format((totalTies / (totalPlayerWins + totalBankerWins + totalTies)) * 100, "0.00") & "%"
End Sub


ThatDonGuy
ThatDonGuy
  • Threads: 122
  • Posts: 6742
Joined: Jun 22, 2011
September 27th, 2024 at 7:09:08 PM permalink
Based on a quick look, I think the error is, if the player has a 5 or 6, the banker will not draw a card under any circumstances.

If you change the line:
playerThirdCard = -1 ' No third card drawn
to
playerThirdCard = 5 ' No third card drawn; this is treated as if the player's third card is a 5
then it should work.
ssho88
ssho88
  • Threads: 59
  • Posts: 684
Joined: Oct 16, 2011
September 27th, 2024 at 7:44:39 PM permalink
Quote: ThatDonGuy

Based on a quick look, I think the error is, if the player has a 5 or 6, the banker will not draw a card under any circumstances.

If you change the line:
playerThirdCard = -1 ' No third card drawn
to
playerThirdCard = 5 ' No third card drawn; this is treated as if the player's third card is a 5
then it should work.
link to original post



Thank you !

For playerThirdCard = X ' No third card drawn, I think it should work if 4<= X <= 7 , agree ?
ThatDonGuy
ThatDonGuy
  • Threads: 122
  • Posts: 6742
Joined: Jun 22, 2011
September 28th, 2024 at 6:40:43 AM permalink
Quote: ssho88

For playerThirdCard = X ' No third card drawn, I think it should work if 4<= X <= 7 , agree ?
link to original post


X = 4 or 5 should work. If X = 6 or 7, banker will not take a card with 5.

I may have discovered why the AI made its mistake; the Venetian baccarat rules web page doesn't mention what happens when the player doesn't take a third card.
ssho88
ssho88
  • Threads: 59
  • Posts: 684
Joined: Oct 16, 2011
September 28th, 2024 at 10:18:23 AM permalink
Quote: ThatDonGuy

Quote: ssho88

For playerThirdCard = X ' No third card drawn, I think it should work if 4<= X <= 7 , agree ?
link to original post


X = 4 or 5 should work. If X = 6 or 7, banker will not take a card with 5.

I may have discovered why the AI made its mistake; the Venetian baccarat rules web page doesn't mention what happens when the player doesn't take a third card.
link to original post




ElseIf bankerValue = 5 Then
If playerThirdCard >= 4 And playerThirdCard <= 7 Then bankerDraw = True

If Banker first two cards total is 5, and we set X = 6 or 7, Banker will take a third card. Am I missing something ?
ThatDonGuy
ThatDonGuy
  • Threads: 122
  • Posts: 6742
Joined: Jun 22, 2011
September 28th, 2024 at 3:49:29 PM permalink
Quote: ssho88

Quote: ThatDonGuy

Quote: ssho88

For playerThirdCard = X ' No third card drawn, I think it should work if 4<= X <= 7 , agree ?
link to original post


X = 4 or 5 should work. If X = 6 or 7, banker will not take a card with 5.

I may have discovered why the AI made its mistake; the Venetian baccarat rules web page doesn't mention what happens when the player doesn't take a third card.
link to original post




ElseIf bankerValue = 5 Then
If playerThirdCard >= 4 And playerThirdCard <= 7 Then bankerDraw = True

If Banker first two cards total is 5, and we set X = 6 or 7, Banker will take a third card. Am I missing something ?
link to original post


No, but there is still a problem with X = 6 or 7; the banker will take a third card with a 6.
ssho88
ssho88
  • Threads: 59
  • Posts: 684
Joined: Oct 16, 2011
September 28th, 2024 at 6:22:13 PM permalink
Quote: ThatDonGuy

Quote: ssho88

Quote: ThatDonGuy

Quote: ssho88

For playerThirdCard = X ' No third card drawn, I think it should work if 4<= X <= 7 , agree ?
link to original post


X = 4 or 5 should work. If X = 6 or 7, banker will not take a card with 5.

I may have discovered why the AI made its mistake; the Venetian baccarat rules web page doesn't mention what happens when the player doesn't take a third card.
link to original post




ElseIf bankerValue = 5 Then
If playerThirdCard >= 4 And playerThirdCard <= 7 Then bankerDraw = True

If Banker first two cards total is 5, and we set X = 6 or 7, Banker will take a third card. Am I missing something ?
link to original post


No, but there is still a problem with X = 6 or 7; the banker will take a third card with a 6.
link to original post




If playerValue >= 8 Or bankerValue >= 8 Or (playerValue = 6 Or playerValue = 7) And (bankerValue = 6 Or bankerValue = 7) Then
GoTo DetermineWinner ' No more drawing; check winner directly
End If


Disagree, Banker with a 6 have no chance to draw third card even X=6 or 7 due to above codes .
Last edited by: ssho88 on Sep 28, 2024
  • Jump to: