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

Commit

Permalink
Don't keep honor tiles in hand with small number of shanten
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihisil committed Mar 4, 2017
1 parent 87229f4 commit 58d9c82
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 8 additions & 3 deletions project/mahjong/ai/discard.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, player, tile_to_discard, waiting, tiles_count):
self.waiting = waiting
self.tiles_count = tiles_count

self._calculate_value()
self.calculate_value()

def find_tile_in_hand(self, closed_hand):
"""
Expand All @@ -54,15 +54,20 @@ def find_tile_in_hand(self, closed_hand):

return TilesConverter.find_34_tile_in_136_array(self.tile_to_discard, closed_hand)

def _calculate_value(self):
def calculate_value(self):
# base is 100 for ability to mark tiles as not needed (like set value to 50)
value = 100
honored_value = 20

# we don't need to keep honor tiles in almost completed hand
if self.player.ai.previous_shanten <= 2:
honored_value = 0

if is_honor(self.tile_to_discard):
if self.tile_to_discard in self.player.ai.valued_honors:
count_of_winds = [x for x in self.player.ai.valued_honors if x == self.tile_to_discard]
# for west-west, east-east we had to double tile value
value += 20 * len(count_of_winds)
value += honored_value * len(count_of_winds)
else:
# suits
suit_tile_grades = [10, 20, 30, 40, 50, 40, 30, 20, 10]
Expand Down
4 changes: 4 additions & 0 deletions project/mahjong/ai/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def discard_tile(self):
results, shanten = self.calculate_outs(self.player.tiles,
self.player.closed_hand,
self.player.is_open_hand)
# we had to update tiles value
# because it is related with shanten number
self.previous_shanten = shanten
for result in results:
result.calculate_value()

if shanten == 0:
self.player.in_tempai = True
Expand Down
11 changes: 11 additions & 0 deletions project/mahjong/ai/tests/tests_discards.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,14 @@ def test_keep_aka_dora_in_hand(self):
# we had to keep red five and discard just 5s
discarded_tile = player.discard_tile()
self.assertNotEqual(discarded_tile, FIVE_RED_SOU)

def test_dont_keep_honor_with_small_number_of_shanten(self):
table = Table()
player = Player(0, 0, table)

tiles = self._string_to_136_array(sou='11445', pin='55699', man='246')
player.init_hand(tiles)
player.draw_tile(self._string_to_136_tile(honors='7'))

discarded_tile = player.discard_tile()
self.assertEqual(self._to_string([discarded_tile]), '7z')

0 comments on commit 58d9c82

Please sign in to comment.