summaryrefslogtreecommitdiff
path: root/src/bcdd/DDTable.py
blob: a97af48ead6fa8be1b38ee8ecfaecee0749d2d95 (plain)
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
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]])))