import ctypes from ctypes import POINTER, Structure, byref, c_char, c_int, c_uint import os import sys """ Adopted from Peter Ellington's PBNCreator https://github.com/Britwizard/PBNCreator """ DDS_HANDS = 4 DDS_SUITS = 5 DDS_STRAINS = 5 dcardSuit = ["S", "H", "D", "C", "NT"] convert_table1={'a':'10','b':'11','c':'12','d':'13'} convert_table2={'10':'a','11':'b','12':'c','13':'d'} vulnerability= ["None","NS","EW","All","NS","EW","All","None","EW","All","None","NS","All","None","NS","EW"] dealerlist=["N","E","S","W"] dd_dealer_point = ["N","S","E","W"] dd_suit=["NT","S","H","D","C"] dd_suit_wide=["NT"," S"," H"," D"," C"] class ddTableDeal(Structure): _fields_ = [("cards", c_uint * DDS_HANDS * DDS_SUITS)] class ddTableResults(Structure): # _fields_ = [("resTable", c_int * DDS_STRAINS * DDS_HANDS)] _fields_ = [("resTable", c_int * DDS_HANDS * DDS_STRAINS)] class ddTableDealPBN(Structure): _fields_ = [("cards", c_char * 80)] class ddTableResults(Structure): # _fields_ = [("resTable", c_int * DDS_STRAINS * DDS_HANDS)] _fields_ = [("resTable", c_int * DDS_HANDS * DDS_STRAINS)] tableDealPBN = ddTableDealPBN() table = ddTableResults() dll_name = DLL = None if os.name == "posix": dll_name = "libdds.so" DLL = ctypes.CDLL if dll_name: #dll_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),dll_name) dll_path = os.path.join(os.getcwd(),dll_name) #dll_path='C:\\Users\\peter\\Python\\PBNGenerator\\dds-64.dll' if dll_name and os.path.exists(dll_path): dll = DLL(dll_path) dll.CalcDDtable.argtypes = [ddTableDeal, POINTER(ddTableResults)] dll.ErrorMessage.argtypes = [c_int, POINTER(c_char)] if os.name == "posix": dll.SetMaxThreads(0) def _check_dll(name): return else: def _check_dll(name): raise Exception(f"Unable to load DDS; {name} is not available") def errorMessage(res): msg = ctypes.create_string_buffer(80) dll.ErrorMessage(res, msg) result_len = ctypes.c_size_t(len(msg)) return msg[:result_len.value] def calcDDtablePBN(tableDealPBN): myTable = ctypes.pointer(table) res = dll.CalcDDtablePBN(tableDealPBN, myTable) if res != 1: line = errorMessage(res) raise Exception("DDS error: {}".format(line.decode("utf-8"))) return myTable def get_ddstable(pbn): tableDealPBN.cards = pbn table = calcDDtablePBN(tableDealPBN) all = { "N" : {}, "S" : {}, "E" : {}, "W" : {} } # below doesn't work, why? #all = dict.fromkeys(["N","S","E","W"], {}) # print(all) for suit in range(0, DDS_SUITS): all["N"][dcardSuit[suit]] = table.contents.resTable[suit][0] all["S"][dcardSuit[suit]] = table.contents.resTable[suit][2] all["E"][dcardSuit[suit]] = table.contents.resTable[suit][1] all["W"][dcardSuit[suit]] = table.contents.resTable[suit][3] return all def get_dd_tricks(deal): byte_board=deal.encode('utf-8') # ddstable requires the board description to be in byte format dd_array = get_ddstable(byte_board) double_dummy_tricks='' for point in dd_array: for suit in dd_suit: temp = dd_array[point][suit] value=str(temp) if temp > 9: value=convert_table2[value] double_dummy_tricks = double_dummy_tricks + value return(double_dummy_tricks) """ End """