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

Commit ce0283b

Browse files
committed
Issue #34. Tsumogiri tile when it possible
1 parent 726ebd8 commit ce0283b

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

project/game/ai/discard.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ def find_tile_in_hand(self, closed_hand):
4848
"""
4949

5050
if self.player.table.has_aka_dora:
51+
tiles_five_of_suits = [4, 13, 22]
5152
# special case, to keep aka dora in hand
52-
if self.tile_to_discard in [4, 13, 22]:
53+
if self.tile_to_discard in tiles_five_of_suits:
5354
aka_closed_hand = closed_hand[:]
5455
while True:
5556
tile = TilesConverter.find_34_tile_in_136_array(self.tile_to_discard, aka_closed_hand)
56-
# we have only aka dora in the hand
57+
58+
# we have only aka dora in the hand, without simple five
5759
if not tile:
5860
break
5961

project/game/ai/first_version/main.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def discard_tile(self, discard_tile):
7474
if discard_tile is not None:
7575
if not self.last_discard_option:
7676
return discard_tile
77-
return self.process_discard_option(self.last_discard_option, self.player.closed_hand)
77+
78+
return self.process_discard_option(self.last_discard_option, self.player.closed_hand, True)
7879

7980
results, shanten = self.calculate_outs(self.player.tiles,
8081
self.player.closed_hand,
@@ -261,11 +262,20 @@ def sorting(x):
261262

262263
return selected_tile
263264

264-
def process_discard_option(self, discard_option, closed_hand):
265+
def process_discard_option(self, discard_option, closed_hand, force_discard=False):
265266
self.waiting = discard_option.waiting
266267
self.player.ai.previous_shanten = discard_option.shanten
267268
self.player.in_tempai = self.player.ai.previous_shanten == 0
268-
return discard_option.find_tile_in_hand(closed_hand)
269+
270+
# when we called meld we don't need "smart" discard
271+
if force_discard:
272+
return discard_option.find_tile_in_hand(closed_hand)
273+
274+
last_draw_34 = self.player.last_draw and self.player.last_draw // 4 or None
275+
if self.player.last_draw not in AKA_DORA_LIST and last_draw_34 == discard_option.tile_to_discard:
276+
return self.player.last_draw
277+
else:
278+
return discard_option.find_tile_in_hand(closed_hand)
269279

270280
def estimate_hand_value(self, win_tile, tiles=None, call_riichi=False):
271281
"""

project/game/ai/first_version/tests/tests_discards.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import unittest
33

4-
from mahjong.constants import EAST, SOUTH, WEST, NORTH, HAKU, HATSU, CHUN, FIVE_RED_SOU
4+
from mahjong.constants import EAST, SOUTH, WEST, NORTH, HAKU, HATSU, CHUN, FIVE_RED_SOU, FIVE_RED_PIN
55
from mahjong.tests_mixin import TestMixin
66

77
from game.ai.discard import DiscardOption
@@ -38,6 +38,30 @@ def test_discard_tile(self):
3838
self.assertEqual(self._to_string([discarded_tile]), '5m')
3939
self.assertEqual(player.ai.previous_shanten, 0)
4040

41+
def test_discard_tile_force_tsumogiri(self):
42+
table = Table()
43+
table.has_aka_dora = True
44+
player = table.player
45+
46+
tiles = self._string_to_136_array(sou='11134567', pin='456', man='45')
47+
tile = 57 # 6p
48+
49+
player.init_hand(tiles)
50+
player.draw_tile(tile)
51+
52+
discarded_tile = player.discard_tile()
53+
self.assertEqual(discarded_tile, tile)
54+
55+
tiles = self._string_to_136_array(sou='11134567', pin='46', man='45') + [53] # simple five pin
56+
tile = FIVE_RED_PIN
57+
58+
player.init_hand(tiles)
59+
player.draw_tile(tile)
60+
61+
discarded_tile = player.discard_tile()
62+
# WE DON'T NEED TO DISCARD RED FIVE
63+
self.assertNotEqual(discarded_tile, tile)
64+
4165
def test_calculate_suit_tiles_value(self):
4266
table = Table()
4367
player = table.player

0 commit comments

Comments
 (0)