Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Issue #35. Fix crash after called chankan
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihisil committed Jul 22, 2017
1 parent 18bfb00 commit 45a509f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 10 additions & 0 deletions project/mahjong/ai/tests/tests_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,18 @@ def test_upgrade_opened_pon_to_kan(self):

player.add_called_meld(self._make_meld(Meld.PON, self._string_to_136_array(man='444')))

self.assertEqual(len(player.melds), 1)
self.assertEqual(len(player.tiles), 14)
self.assertEqual(player.can_call_kan(tile, False), Meld.CHANKAN)

player.discard_tile()
player.draw_tile(tile)
player.add_called_meld(self._make_meld(Meld.CHANKAN, self._string_to_136_array(man='4444')))

self.assertEqual(len(player.melds), 1)
self.assertEqual(player.melds[0].type, Meld.CHANKAN)
self.assertEqual(len(player.tiles), 13)

def test_call_closed_kan(self):
table = Table()
player = table.player
Expand Down
6 changes: 4 additions & 2 deletions project/mahjong/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def erase_state(self):
def add_called_meld(self, meld: Meld):
# we already added chankan as a pon set
if meld.type == Meld.CHANKAN:
return
tile_34 = meld.tiles[0] // 4
pon_set = [x for x in self.melds if x.type == Meld.PON and (x.tiles[0] // 4) == tile_34]
self.melds.remove(pon_set[0])

self.melds.append(meld)

Expand Down Expand Up @@ -210,7 +212,7 @@ def enemy_called_riichi(self):

def add_called_meld(self, meld: Meld):
# we had to remove tile from the hand for closed kan set
if (meld.type == Meld.KAN or meld.type == Meld.CHANKAN) and not meld.opened:
if (meld.type == Meld.KAN and not meld.opened) or meld.type == Meld.CHANKAN:
self.tiles.remove(meld.called_tile)

super().add_called_meld(meld)
Expand Down
4 changes: 2 additions & 2 deletions project/tenhou/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def start_game(self):
else:
meld_type = 4
self._send_message('<N type="{}" hai="{}" />'.format(meld_type, drawn_tile))
logger.info('We called a kan set!')
logger.info('We called a closed kan\chankan set!')
continue

discarded_tile = self.player.discard_tile()
Expand Down Expand Up @@ -374,7 +374,7 @@ def start_game(self):
if 't="3"' in message:
if self.player.can_call_kan(tile, True):
self._send_message('<N type="2" />')
logger.info('We called a kan set!')
logger.info('We called an open kan set!')
continue

# chi or pon
Expand Down

0 comments on commit 45a509f

Please sign in to comment.