Coverage for project/utils/general.py : 94%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import random
2import string
3from typing import List
5from mahjong.constants import EAST
6from mahjong.utils import is_honor, is_man, is_pin, is_sou, simplify
9# TODO move to mahjong lib
10def is_sangenpai(tile_34):
11 return tile_34 >= 31
14# TODO move to mahjong lib
15def is_tiles_same_suit(first_tile_34, second_tile_34):
16 if is_pin(first_tile_34) and is_pin(second_tile_34):
17 return True
18 if is_man(first_tile_34) and is_man(second_tile_34):
19 return True
20 if is_sou(first_tile_34) and is_sou(second_tile_34):
21 return True
22 return False
25# TODO move to mahjong lib
26def is_dora_connector(tile_136: int, dora_indicators_136: List[int]) -> bool:
27 tile_34 = tile_136 // 4
28 if is_honor(tile_34):
29 return False
31 for dora_indicator in dora_indicators_136:
32 dora_indicator_34 = dora_indicator // 4
33 if not is_tiles_same_suit(dora_indicator_34, tile_34):
34 continue
36 simplified_tile = simplify(tile_34)
37 simplified_dora_indicator = simplify(dora_indicator_34)
39 if simplified_dora_indicator - 1 == simplified_tile:
40 return True
42 if simplified_dora_indicator + 1 == simplified_tile:
43 return True
45 return False
48def make_random_letters_and_digit_string(length=15):
49 random_chars = string.ascii_lowercase + string.digits
50 return "".join(random.choice(random_chars) for _ in range(length))
53def revealed_suits_tiles(player, tiles_34):
54 """
55 Return all reviled tiles separated by suits for provided tiles list
56 """
57 return _suits_tiles_helper(
58 tiles_34, lambda _tile_34_index, _tiles_34: player.number_of_revealed_tiles(_tile_34_index, _tiles_34)
59 )
62def separate_tiles_by_suits(tiles_34):
63 """
64 Return tiles separated by suits for provided tiles list
65 """
66 return _suits_tiles_helper(tiles_34, lambda _tile_34_index, _tiles_34: _tiles_34[_tile_34_index])
69def _suits_tiles_helper(tiles_34, total_tiles_lambda):
70 """
71 Separate tiles by suit
72 """
73 suits = [
74 [0] * 9,
75 [0] * 9,
76 [0] * 9,
77 ]
79 for tile_34_index in range(0, EAST):
80 total_tiles = total_tiles_lambda(tile_34_index, tiles_34)
81 if not total_tiles:
82 continue
84 suit_index = None
85 simplified_tile = simplify(tile_34_index)
87 if is_man(tile_34_index):
88 suit_index = 0
90 if is_pin(tile_34_index):
91 suit_index = 1
93 if is_sou(tile_34_index):
94 suit_index = 2
96 suits[suit_index][simplified_tile] += total_tiles
98 return suits