from copy import copy from .PBNBoard import PBNBoard 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 def __init__(self, board): self._board = board; 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[0] player_id = PBNBoard.PLAYERS.index(player) denom_id = 4 for tricks in player_ability[1]: 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 = PBNBoard.PLAYERS.index(player) denom_id = PBNBoard.DENOMINATIONS.index(denom) result[player_id][denom_id] = tricks return self._validate_table(result) def get_dd_table(self, show_banner=True): try: return self.get_jfr_table() except FieldNotFoundException: try: return self.get_pbn_table() except FieldNotFoundException: return self.get_bcalc_table(show_banner) def print_table(self, dd_table): print('\t' + '\t'.join(PBNBoard.DENOMINATIONS)) for i in range(0, 4): print('%s%s' % ( PBNBoard.PLAYERS[i], ''.join(['\t' + str(tricks) for tricks in dd_table[i]])))