Visual Basic code to count the number of 15s in a cribbage hand
This section of code is a small but necessary part of my current project to write a Visual Basic program to play the card game cribbage.
I’m rather please with this, though younger brains might be able to come up with something slicker.
Sub HowMany15s(Cards() As Integer, Ncards As Integer, ByRef HowMany As Integer)
'
' How many 15s are there in the supplied cards
' (uses a binary mask to generate all combinations of cards)
'
Dim i, f As Integer
HowMany = 0
For i = 1 To 2 ^ Ncards - 1
f = 0
Select Case Ncards
Case 3
If (i And 1) > 0 Then f = f + Cards(2)
If (i And 2) > 0 Then f = f + Cards(1)
If (i And 4) > 0 Then f = f + Cards(0)
Case 4
If (i And 1) > 0 Then f = f + Cards(3)
If (i And 2) > 0 Then f = f + Cards(2)
If (i And 4) > 0 Then f = f + Cards(1)
If (i And 8) > 0 Then f = f + Cards(0)
Case 5
If (i And 1) > 0 Then f = f + Cards(4)
If (i And 2) > 0 Then f = f + Cards(3)
If (i And 4) > 0 Then f = f + Cards(2)
If (i And 8) > 0 Then f = f + Cards(1)
If (i And 16) > 0 Then f = f + Cards(0)
End Select
If f = 15 Then HowMany = HowMany + 1
Next
End Sub