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
|
from ctypes import c_char_p
from copy import copy
from .BCalcWrapper import BCalcWrapper
from .Exceptions import DDTableInvalidException, FieldNotFoundException
class DDTable(object):
def _get_empty_table(self):
table = []
row = [-1] * 5
for col in range(0, 4):
table.append(copy(row))
return table
def _validate_table(self, table):
for row in table:
for t in row:
if (t > 13) or (t < 0):
raise DDTableInvalidException(
'Invalid number of tricks: %d' % (t)
)
return table
_banner_displayed = False
def __init__(self, board):
self._board = board;
self._wrapper = BCalcWrapper()
def get_bcalc_table(self):
if not DDTable._banner_displayed:
print('Double dummy analysis provided by BCalc.')
print('BCalc is awesome, check it out: http://bcalc.w8.pl')
DDTable._banner_displayed = True
result = self._get_empty_table()
deal = self._board.get_layout()
solver = self._wrapper.new(b"PBN", deal.encode(), 0, 0)
for denom in range(0, 5):
self._wrapper.setTrumpAndReset(solver, denom)
for player in range(0, 4):
leader = self._wrapper.declarerToLeader(player)
self._wrapper.setPlayerOnLeadAndReset(solver, leader)
result[player][denom] = 13 - self._wrapper.getTricksToTake(
solver)
error = self._wrapper.getLastError(solver)
if error:
raise DDTableInvalidException(
'BCalc error: %s' % (c_char_p(error).value.decode('ascii')))
self._wrapper.delete(solver);
return self._validate_table(result)
def get_jfr_table(self):
result = self._get_empty_table()
ability = self._board.get_ability()
abilities = self._board.validate_ability(ability)
for player_ability in abilities:
player = player_ability.groups(1)[0]
player_id = BCalcWrapper.PLAYERS.index(player)
denom_id = 4
for tricks in player_ability.group(2):
result[player_id][denom_id] = int(tricks, 16)
denom_id -= 1
return self._validate_table(result)
def get_pbn_table(self):
table = self._board.get_optimum_result_table()
parsed_table = self._board.validate_optimum_result_table(table)
result = self._get_empty_table()
for line_match in parsed_table:
player = line_match.group(1)[0]
denom = line_match.group(2)[0]
tricks = int(line_match.group(3))
player_id = BCalcWrapper.PLAYERS.index(player)
denom_id = BCalcWrapper.DENOMINATIONS.index(denom)
result[player_id][denom_id] = tricks
return self._validate_table(result)
def get_dd_table(self):
try:
return self.get_jfr_table()
except FieldNotFoundException:
try:
return self.get_pbn_table()
except FieldNotFoundException:
return self.get_bcalc_table()
def print_table(self, dd_table):
print('\t' + '\t'.join(BCalcWrapper.DENOMINATIONS))
for i in range(0, 4):
print('%s%s' % (
self._wrapper.PLAYERS[i],
''.join(['\t' + str(tricks) for tricks in dd_table[i]])))
|