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

Commit 2b3c134

Browse files
committed
fix shanten calculation rules in regard with chiitoitsu consideration
1 parent 33b663a commit 2b3c134

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

project/game/ai/first_version/hand_builder.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,35 @@ def discard_tile(self, tiles, closed_hand, melds, print_log=True):
114114

115115
return self.process_discard_option(selected_tile, closed_hand, print_log=print_log)
116116

117+
def calculate_shanten(self, tiles_34, open_sets_34=None):
118+
shanten_with_chiitoitsu = self.ai.shanten_calculator.calculate_shanten(tiles_34,
119+
open_sets_34,
120+
chiitoitsu=True)
121+
shanten_without_chiitoitsu = self.ai.shanten_calculator.calculate_shanten(tiles_34,
122+
open_sets_34,
123+
chiitoitsu=False)
124+
125+
if shanten_with_chiitoitsu == 0 and shanten_without_chiitoitsu >= 1:
126+
shanten = shanten_with_chiitoitsu
127+
use_chiitoitsu = True
128+
elif shanten_with_chiitoitsu == 1 and shanten_without_chiitoitsu >= 3:
129+
shanten = shanten_with_chiitoitsu
130+
use_chiitoitsu = True
131+
else:
132+
shanten = shanten_without_chiitoitsu
133+
use_chiitoitsu = False
134+
135+
return shanten, use_chiitoitsu
136+
117137
def calculate_waits(self, tiles_34, open_sets_34=None):
118138
"""
119139
:param tiles_34: array of tiles in 34 formant, 13 of them (this is important)
120140
:param open_sets_34: array of array with tiles in 34 format
121141
:return: array of waits in 34 format and number of shanten
122142
"""
123-
shanten = self.ai.shanten_calculator.calculate_shanten(tiles_34, open_sets_34, chiitoitsu=self.ai.use_chitoitsu)
143+
144+
shanten, use_chiitoitsu = self.calculate_shanten(tiles_34, open_sets_34)
145+
124146
waiting = []
125147
for j in range(0, 34):
126148
if tiles_34[j] == 4:
@@ -131,7 +153,7 @@ def calculate_waits(self, tiles_34, open_sets_34=None):
131153
key = '{},{},{}'.format(
132154
''.join([str(x) for x in tiles_34]),
133155
';'.join([str(x) for x in open_sets_34]),
134-
self.ai.use_chitoitsu and 1 or 0
156+
use_chiitoitsu and 1 or 0
135157
)
136158

137159
if key in self.ai.hand_cache:
@@ -140,7 +162,7 @@ def calculate_waits(self, tiles_34, open_sets_34=None):
140162
new_shanten = self.ai.shanten_calculator.calculate_shanten(
141163
tiles_34,
142164
open_sets_34,
143-
chiitoitsu=self.ai.use_chitoitsu
165+
chiitoitsu=use_chiitoitsu
144166
)
145167
self.ai.hand_cache[key] = new_shanten
146168

@@ -188,10 +210,9 @@ def find_discard_options(self, tiles, closed_hand, melds=None):
188210
if is_agari:
189211
shanten = Shanten.AGARI_STATE
190212
else:
191-
shanten = self.ai.shanten_calculator.calculate_shanten(
213+
shanten, _ = self.calculate_shanten(
192214
tiles_34,
193-
open_sets_34,
194-
chiitoitsu=self.ai.use_chitoitsu
215+
open_sets_34
195216
)
196217

197218
return results, shanten

project/game/ai/first_version/main.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class ImplementationAI(InterfaceAI):
4444

4545
current_strategy = None
4646
last_discard_option = None
47-
use_chitoitsu = False
4847

4948
hand_cache = {}
5049

@@ -70,7 +69,6 @@ def erase_state(self):
7069

7170
self.current_strategy = None
7271
self.last_discard_option = None
73-
self.use_chitoitsu = False
7472

7573
self.hand_cache = {}
7674

@@ -81,9 +79,7 @@ def init_hand(self):
8179
'Hand: {}'.format(self.player.format_hand_for_print()),
8280
])
8381

84-
self.shanten = self.shanten_calculator.calculate_shanten(
85-
TilesConverter.to_34_array(self.player.tiles)
86-
)
82+
self.shanten, _ = self.hand_builder.calculate_shanten(TilesConverter.to_34_array(self.player.tiles))
8783

8884
def draw_tile(self, tile_136):
8985
self.determine_strategy(self.player.tiles)
@@ -111,11 +107,8 @@ def try_to_call_meld(self, tile_136, is_kamicha_discard):
111107
return None, None
112108

113109
tiles_34 = TilesConverter.to_34_array(tiles_136)
114-
previous_shanten = self.shanten_calculator.calculate_shanten(
115-
tiles_34,
116-
self.player.meld_34_tiles,
117-
chiitoitsu=self.use_chitoitsu
118-
)
110+
111+
previous_shanten, _ = self.hand_builder.calculate_shanten(tiles_34, self.player.meld_34_tiles)
119112

120113
if previous_shanten == Shanten.AGARI_STATE and not self.current_strategy.can_meld_into_agari():
121114
return None, None
@@ -133,8 +126,6 @@ def try_to_call_meld(self, tile_136, is_kamicha_discard):
133126
return meld, discard_option
134127

135128
def determine_strategy(self, tiles_136):
136-
self.use_chitoitsu = False
137-
138129
# for already opened hand we don't need to give up on selected strategy
139130
if self.player.is_open_hand and self.current_strategy:
140131
return False
@@ -160,8 +151,6 @@ def determine_strategy(self, tiles_136):
160151
self.current_strategy = strategy
161152

162153
if self.current_strategy:
163-
self.use_chitoitsu = self.current_strategy.type == BaseStrategy.CHIITOITSU
164-
165154
if not old_strategy or self.current_strategy.type != old_strategy.type:
166155
DecisionsLogger.debug(
167156
log.STRATEGY_ACTIVATE,

project/game/ai/first_version/tests/strategies/tests_chiitoitsu.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,15 @@ def test_dont_call_meld(self):
5050
tile = self._string_to_136_tile(man='9')
5151
meld, _ = player.try_to_call_meld(tile, True)
5252
self.assertEqual(meld, None)
53+
54+
def test_keep_chiitoitsu_tempai(self):
55+
table = Table()
56+
player = table.player
57+
58+
tiles = self._string_to_136_array(sou='113355', man='22669', pin='99')
59+
player.init_hand(tiles)
60+
61+
player.draw_tile(self._string_to_136_tile(man='6'))
62+
63+
discard = player.discard_tile()
64+
self.assertEqual(self._to_string([discard]), '6m')

0 commit comments

Comments
 (0)