It’s doing my head in…

It’s doing my head in trying to figure out the logic of how to play the cards in cribbage. My Visual Basic program is coming along but there’s been some tricky areas to figure out and this bit is proving difficult. The chart shows where I am with it but I can’t help but feel there’s an approach that would be simpler to code.

Visual Basic code to count the number of 15s in a cribbage hand

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

My Visual Basic code

I’ve re-written my reminder program AGAIN, the one I wrote for work many, many decades ago!

The first re-write was done using Python (see this post), and the second was using Fortran (see this post).

This time I’ve taught myself Visual Basic (well, some of it) using Microsoft Visual Studio. It’s a monster of a system and great fun trying to get to grips with. On the right is a screen shot of the running program (note the garish colours!) and below are the two code files. [Updated 28.3.23 to correct major sorting bug]

I tentatively have another project lined up, to write a program to play cribbage, my other newly discovered interest. It would be a pretty difficult task – watch this blog…..

Games with the kids

Games with the kids

A 5 mile circular stroll with the little ones as well as our Berlin son began just outside Reigate, (near a couple of fine pubs) and took us through woods into the centre of the town. A fine walk on a dull day. On another day we’ll do it again but make use of one of the pubs.

Back home for a late lunch followed by a long session playing Monopoly Deal, a great card game for young and old. Congratulations to 8-year-old Chloe on her wins!

I’ve recently become a cribbage fan and I’ve got Chloe playing the 5-card version, whilst for us grown-ups the 6-card version is a better challenge. I’m doing pretty good playing random Internet opponents.

What with the pre-dinner pints with my son at one of our local pubs, it’s been a lovely few days with the family.