Spaces:
Runtime error
Runtime error
File size: 3,835 Bytes
de49ebf 3f1a1ab de49ebf 3f1a1ab de49ebf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
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
"""
DD_DIRECTIONS = ['N', 'S', 'E', 'W']
DD_SUITS = ['NT', 'S', 'H', 'D', 'C']
def get_result_table(pbn_deal_string):
dd_tricks_string = get_dd_tricks(pbn_deal_string)
s = '[OptimumResultTable "Declarer;Denomination\\2R;Result\\2R"]\n'
for i, char in enumerate(dd_tricks_string):
direction = DD_DIRECTIONS[i//5]
suit = DD_SUITS[i%5]
tricks = int(char, 16)
s += f'{direction} {suit} {tricks}\n'
return s
|