Sample code - coding and decoding BBA hand number
Code example for encoding and decoding hexadecimal BBA hand number.
Imports System
'Imports System.Diagnostics.Eventing
'Imports System.Numerics
Module Program
Public Const C_CLUBS As Integer = 0
Public Const C_DIAMONDS As Integer = 1
Public Const C_HEARTS As Integer = 2
Public Const C_SPADES As Integer = 3
Public Const C_NT As Integer = 4
Public Const C_NORTH As Integer = 0
Public Const C_EAST As Integer = 1
Public Const C_SOUTH As Integer = 2
Public Const C_WEST As Integer = 3
Public Const C_NONE As Long = 0
Public Const C_NS As Long = 2
Public Const C_WE As Long = 1
Public Const C_BOTH As Long = 3
Public Const C_LONGER As String = "AKQJT98765432"
Public board(0 To 3, 0 To 3) As Integer
Public dealers(0 To 15) As Integer
Public vulnerability(15) As Integer
Private Structure TYPE_HAND
Dim suit() As String
End Structure
Private Sub set_board()
'---standard number of a board
board(C_NORTH, C_NONE) = 1
board(C_EAST, C_NS) = 2
board(C_SOUTH, C_WE) = 3
board(C_WEST, C_BOTH) = 4
board(C_NORTH, C_NS) = 5
board(C_EAST, C_WE) = 6
board(C_SOUTH, C_BOTH) = 7
board(C_WEST, C_NONE) = 8
board(C_NORTH, C_WE) = 9
board(C_EAST, C_BOTH) = 10
board(C_SOUTH, C_NONE) = 11
board(C_WEST, C_NS) = 12
board(C_NORTH, C_BOTH) = 13
board(C_EAST, C_NONE) = 14
board(C_SOUTH, C_NS) = 15
board(C_WEST, C_WE) = 16
End Sub
Private Sub set_dealers()
Dim k As Integer
For k = 0 To 12 Step 4
dealers(0 + k) = C_NORTH
dealers(1 + k) = C_EAST
dealers(2 + k) = C_SOUTH
dealers(3 + k) = C_WEST
Next k
End Sub
Private Sub set_vulnerability()
vulnerability(0) = C_NONE
vulnerability(1) = C_NS
vulnerability(2) = C_WE
vulnerability(3) = C_BOTH
vulnerability(4) = C_NS
vulnerability(5) = C_WE
vulnerability(6) = C_BOTH
vulnerability(7) = C_NONE
vulnerability(8) = C_WE
vulnerability(9) = C_BOTH
vulnerability(10) = C_NONE
vulnerability(11) = C_NS
vulnerability(12) = C_BOTH
vulnerability(13) = C_NONE
vulnerability(14) = C_NS
vulnerability(15) = C_WE
End Sub
Sub Main(args As String())
Dim i As Integer, j As Integer, k As Integer, encryption_byte As Integer, board_extension As Integer
Dim deal As Integer, dealer As Integer, vulnerable As Integer, number As Integer
Dim str_card As String, str_Deal As String, str_number As String
Dim hand() As TYPE_HAND
Dim lbloki(3) As Integer
ReDim hand(3)
For k = 0 To 3
ReDim hand(k).suit(3)
Next
'---example deal
hand(0).suit(C_CLUBS) = "987"
hand(0).suit(C_DIAMONDS) = "KQJT7"
hand(0).suit(C_HEARTS) = "A74"
hand(0).suit(C_SPADES) = "A6"
hand(1).suit(C_CLUBS) = "T652"
hand(1).suit(C_DIAMONDS) = "3"
hand(1).suit(C_HEARTS) = "QJ82"
hand(1).suit(C_SPADES) = "J732"
hand(2).suit(C_CLUBS) = "J3"
hand(2).suit(C_DIAMONDS) = "A9842"
hand(2).suit(C_HEARTS) = "K3"
hand(2).suit(C_SPADES) = "T984"
hand(3).suit(C_CLUBS) = "AKQ4"
hand(3).suit(C_DIAMONDS) = "65"
hand(3).suit(C_HEARTS) = "T965"
hand(3).suit(C_SPADES) = "KQ5"
set_board()
set_dealers()
set_vulnerability()
'---coding max number = 256
deal = 234
j = (deal - 1) Mod 16
dealer = dealers(j)
vulnerable = vulnerability(j)
Console.WriteLine("coding deal = " & deal)
board_extension = ((deal - 1) \ 16) Mod 16
str_Deal = Hex(board_extension) & Hex(dealer * 4 + vulnerable)
encryption_byte = board(dealer, vulnerable)
For j = 1 To 13
number = 0
str_card = Mid$(C_LONGER, j, 1)
For i = 0 To 3
For k = 0 To 3
If InStr(1, hand(k).suit(i), str_card) > 0 Then
number = number + k * (4 ^ i)
End If
Next k
Next i
'---additional coding
number = encryption_byte Xor number
str_card = Hex(number)
If Len(str_card) = 1 Then
str_card = "0" & str_card
End If
str_Deal = str_Deal & str_card
Next j
Console.WriteLine("str_deal = " & str_Deal)
'---decoding
For k = 0 To 3
ReDim hand(k).suit(3)
Next
str_number = Left$(str_Deal, 1)
board_extension = CLng("&H" & str_number)
str_number = Mid$(str_Deal, 2, 1)
number = CLng("&H" & str_number)
dealer = number \ 4
vulnerable = number Mod 4
deal = board_extension * 16 + board(dealer, vulnerable)
encryption_byte = board(dealer, vulnerable)
For j = 1 To 13
str_card = Mid$(C_LONGER, j, 1)
str_number = Mid$(str_Deal, 2 * j + 1, 2)
'---0-15
number = CLng("&H" & str_number)
number = encryption_byte Xor number
lbloki(0) = number Mod 4
lbloki(1) = (number \ 4) Mod 4
lbloki(2) = (number \ 16) Mod 4
lbloki(3) = number \ 64
For i = 0 To 3
k = lbloki(i)
hand(k).suit(i) = hand(k).suit(i) & str_card
Next i
Next j
Console.WriteLine("decoding deal = " & deal)
For k = 0 To 3
For i = 3 To 0 Step -1
Console.Write(hand(k).suit(i))
If (i > 0) Then
Console.Write(".")
End If
Next
Console.WriteLine()
Next
End Sub
End Module