smly/mjai.app

`tehai_mjai()` gets incorrect output when there is aka dora

Closed this issue · 1 comments

Describe the bug

@property
def tehai_mjai(self) -> list[str]:
"""
Player's hand as a list of tile strings in mjai format.
Example:
>>> bot.tehai_mjai
["1m", "2m", "6m", "9m", "1p", "3p", "4p", "3s", "4s", "5s",
"7s", "9s", "5z", "6z"]
"""
zi_map = ["E", "S", "W", "N", "P", "F", "C"]
ms, ps, ss, zis, akas = [], [], [], [], []
tiles = []
for tile_idx, tile_count in enumerate(self.player_state.tehai):
if tile_count and tile_idx == 4 and self.akas_in_hand[0]:
akas.append("5mr")
elif tile_count and tile_idx == 4 + 9 and self.akas_in_hand[1]:
akas.append("5pr")
elif tile_count and tile_idx == 4 + 18 and self.akas_in_hand[2]:
akas.append("5sr")
elif tile_count and tile_idx < 9:
ms += [f"{tile_idx + 1}m"] * tile_count
elif tile_count and tile_idx < 18:
ps += [f"{tile_idx - 9 + 1}p"] * tile_count
elif tile_count and tile_idx < 27:
ss += [f"{tile_idx - 18 + 1}s"] * tile_count
else:
for _ in range(tile_count):
zis.append(zi_map[tile_idx - 27])
tiles = ms + ps + ss + zis + akas
return tiles

When both 5mr and 5m in hand, output only gives '5mr'.

To Reproduce
Example contidion:

self.player_state.tehai = [
    2,1,1,1,4,1,1,1,1,
    0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0
]
self.akas_in_hand = [True, False, False]

Output:

['1m', '1m', '2m', '3m', '4m', '6m', '7m', '8m', '9m', '5mr']

Expected behavior
Should output:

['1m', '1m', '2m', '3m', '4m', '5m', '5m', '5m', '6m', '7m', '8m', '9m', '5mr']

Additional context
A simple fix:

     @property 
     def tehai_mjai(self) -> list[str]: 
         """ 
         Player's hand as a list of tile strings in mjai format. 
  
         Example: 
             >>> bot.tehai_mjai 
             ["1m", "2m", "6m", "9m", "1p", "3p", "4p", "3s", "4s", "5s", 
              "7s", "9s", "5z", "6z"] 
         """ 
         zi_map = ["E", "S", "W", "N", "P", "F", "C"] 
         ms, ps, ss, zis, akas = [], [], [], [], [] 
         tiles = [] 
         for tile_idx, tile_count in enumerate(self.player_state.tehai): 
             if tile_count and tile_idx == 4 and self.akas_in_hand[0]: 
                 ms += [f"{tile_idx + 1}m"] * (tile_count-1)
                 akas.append("5mr") 
             elif tile_count and tile_idx == 4 + 9 and self.akas_in_hand[1]: 
                 ms += [f"{tile_idx + 1}p"] * (tile_count-1)
                 akas.append("5pr") 
             elif tile_count and tile_idx == 4 + 18 and self.akas_in_hand[2]: 
                 ms += [f"{tile_idx + 1}s"] * (tile_count-1)
                 akas.append("5sr") 
             elif tile_count and tile_idx < 9: 
                 ms += [f"{tile_idx + 1}m"] * tile_count 
             elif tile_count and tile_idx < 18: 
                 ps += [f"{tile_idx - 9 + 1}p"] * tile_count 
             elif tile_count and tile_idx < 27: 
                 ss += [f"{tile_idx - 18 + 1}s"] * tile_count 
             else: 
                 for _ in range(tile_count): 
                     zis.append(zi_map[tile_idx - 27]) 
  
         tiles = ms + ps + ss + zis + akas 
         return tiles 
smly commented

Nice catch! Thanks for your contribution. It seems like this part should be refactored: remove ms, ps, ss, zis, akas variables and add tiles to tiles variable. I will fix it few hours later.