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

Commit

Permalink
add tests for known bugs (#84, #85)
Browse files Browse the repository at this point in the history
  • Loading branch information
bogachev-pa committed Sep 3, 2018
1 parent bd05483 commit 25ab8d8
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 26 deletions.
58 changes: 58 additions & 0 deletions project/game/ai/first_version/tests/strategies/tests_chinitsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest

from mahjong.tests_mixin import TestMixin
from mahjong.meld import Meld

from game.ai.first_version.strategies.chinitsu import ChinitsuStrategy
from game.ai.first_version.strategies.main import BaseStrategy
Expand Down Expand Up @@ -108,3 +109,60 @@ def test_suitable_tiles(self):

tile = self._string_to_136_tile(honors='1')
self.assertEqual(strategy.is_tile_suitable(tile), False)

# issue #84
@unittest.expectedFailure
def test_open_suit_same_shanten(self):
table = Table()
player = table.player
player.scores = 25000
table.count_of_remaining_tiles = 100

tiles = self._string_to_136_array(man='1134556999', pin='3', sou='78')
player.init_hand(tiles)

meld = self._make_meld(Meld.CHI, man='345')
player.add_called_meld(meld)

strategy = ChinitsuStrategy(BaseStrategy.CHINITSU, player)
self.assertEqual(strategy.should_activate_strategy(player.tiles), True)

tile = self._string_to_136_tile(man='1')
meld, _ = player.try_to_call_meld(tile, True)
self.assertNotEqual(meld, None)
self.assertEqual(self._to_string(meld.tiles), '111m')

def test_correct_discard_agari_no_yaku(self):
table = Table()
player = table.player

tiles = self._string_to_136_array(man='111234677889', sou='1', pin='')
player.init_hand(tiles)

meld = self._make_meld(Meld.CHI, man='789')
player.add_called_meld(meld)

tile = self._string_to_136_tile(sou='1')
player.draw_tile(tile)
discard = player.discard_tile()
self.assertEqual(self._to_string([discard]), '1s')

def test_open_suit_agari_no_yaku(self):
table = Table()
player = table.player
player.scores = 25000
table.count_of_remaining_tiles = 100

tiles = self._string_to_136_array(man='11123455589', pin='22')
player.init_hand(tiles)

meld = self._make_meld(Meld.CHI, man='234')
player.add_called_meld(meld)

strategy = ChinitsuStrategy(BaseStrategy.CHINITSU, player)
self.assertEqual(strategy.should_activate_strategy(player.tiles), True)

tile = self._string_to_136_tile(man='7')
meld, _ = player.try_to_call_meld(tile, True)
self.assertNotEqual(meld, None)
self.assertEqual(self._to_string(meld.tiles), '789m')
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from mahjong.tests_mixin import TestMixin
from mahjong.tile import Tile
from mahjong.meld import Meld

from game.ai.first_version.strategies.formal_tempai import FormalTempaiStrategy
from game.ai.first_version.strategies.main import BaseStrategy
Expand All @@ -23,13 +24,8 @@ def test_should_activate_strategy(self):
self.player.init_hand(tiles)
self.assertEqual(strategy.should_activate_strategy(self.player.tiles), False)

self.player.draw_tile(self._string_to_136_tile(honors='1'))
# to calculate hand shanten number
self.player.discard_tile()

# Let's move to 10th round step, one tile was already discarded, 9 more
# to go
for i in range(0, 9):
# Let's move to 10th round step
for i in range(0, 10):
self.player.add_discarded_tile(Tile(0, False))

self.assertEqual(strategy.should_activate_strategy(self.player.tiles), False)
Expand All @@ -38,3 +34,45 @@ def test_should_activate_strategy(self):
# we should go for formal tempai
self.player.add_discarded_tile(Tile(0, True))
self.assertEqual(strategy.should_activate_strategy(self.player.tiles), True)

def test_get_tempai(self):
tiles = self._string_to_136_array(man='2379', sou='4568', pin='22299')
self.player.init_hand(tiles)

# Let's move to 15th round step
for i in range(0, 15):
self.player.add_discarded_tile(Tile(0, False))

tile = self._string_to_136_tile(man='8')
meld, _ = self.player.try_to_call_meld(tile, True)
self.assertNotEqual(meld, None)
self.assertEqual(self._to_string(meld.tiles), '789m')

tile_to_discard = self.player.discard_tile()
self.assertEqual(self._to_string([tile_to_discard]), '8s')

# We shouldn't open when we are already in tempai expect for some
# special cases
@unittest.expectedFailure
def test_dont_meld_agari(self):
strategy = FormalTempaiStrategy(BaseStrategy.FORMAL_TEMPAI, self.player)

tiles = self._string_to_136_array(man='2379', sou='4568', pin='22299')
self.player.init_hand(tiles)

# Let's move to 15th round step
for i in range(0, 15):
self.player.add_discarded_tile(Tile(0, False))

self.assertEqual(strategy.should_activate_strategy(self.player.tiles), True)

tiles = self._string_to_136_array(man='23789', sou='456', pin='22299')
self.player.init_hand(tiles)
meld = self._make_meld(Meld.CHI, man='789')
self.player.add_called_meld(meld)

self.assertEqual(strategy.should_activate_strategy(self.player.tiles), True)

tile = self._string_to_136_tile(man='4')
meld, _ = self.player.try_to_call_meld(tile, True)
self.assertEqual(meld, None)
23 changes: 22 additions & 1 deletion project/game/ai/first_version/tests/strategies/tests_honitsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def test_should_activate_strategy(self):
player.init_hand(tiles)
self.assertEqual(strategy.should_activate_strategy(player.tiles), False)


def test_suitable_tiles(self):
table = Table()
player = table.player
Expand Down Expand Up @@ -182,3 +181,25 @@ def test_open_yakuhai_same_shanten(self):
meld, _ = player.try_to_call_meld(tile, True)
self.assertNotEqual(meld, None)
self.assertEqual(self._to_string(meld.tiles), '777z')

# issue #84
@unittest.expectedFailure
def test_open_suit_same_shanten(self):
table = Table()
player = table.player
player.scores = 25000
table.count_of_remaining_tiles = 100

tiles = self._string_to_136_array(man='1134556', pin='3', sou='78', honors='777')
player.init_hand(tiles)

meld = self._make_meld(Meld.CHI, man='345')
player.add_called_meld(meld)

strategy = HonitsuStrategy(BaseStrategy.HONITSU, player)
self.assertEqual(strategy.should_activate_strategy(player.tiles), True)

tile = self._string_to_136_tile(man='1')
meld, _ = player.try_to_call_meld(tile, True)
self.assertNotEqual(meld, None)
self.assertEqual(self._to_string(meld.tiles), '111m')
26 changes: 26 additions & 0 deletions project/game/ai/first_version/tests/strategies/tests_tanyao.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,32 @@ def test_determine_strategy_when_we_try_to_call_meld(self):

self._assert_tanyao(self.player)

def test_correct_discard_agari_no_yaku(self):
tiles = self._string_to_136_array(man='23567', sou='456', pin='22244')
self.player.init_hand(tiles)

meld = self._make_meld(Meld.CHI, man='567')
self.player.add_called_meld(meld)

tile = self._string_to_136_tile(man='1')
self.player.draw_tile(tile)
discard = self.player.discard_tile()
self.assertEqual(self._to_string([discard]), '1m')

# In case we are in temporary furiten, we can't call ron, but can still
# make chi. We assume this chi to be bad, so let's not call it.
@unittest.expectedFailure
def test_dont_meld_agari(self):
tiles = self._string_to_136_array(man='23567', sou='456', pin='22244')
self.player.init_hand(tiles)

meld = self._make_meld(Meld.CHI, man='567')
self.player.add_called_meld(meld)

tile = self._string_to_136_tile(man='4')
meld, _ = self.player.try_to_call_meld(tile, True)
self.assertEqual(meld, None)

def _make_table(self):
table = Table()
table.has_open_tanyao = True
Expand Down
18 changes: 0 additions & 18 deletions project/game/ai/first_version/tests/tests_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,3 @@ def test_closed_kan_and_not_necessary_call(self):
tile = self._string_to_136_tile(sou='9')

self.assertEqual(player.should_call_kan(tile, False), None)

def test_agari_without_yaku(self):
"""
Bot tried to call closed kan with 568m669p1478999s + 9s hand
"""
table = Table()
player = table.player

tiles = self._string_to_136_array(man='111234677889', sou='1', pin='')
player.init_hand(tiles)
tile = self._string_to_136_tile(sou='1')

meld = self._make_meld(Meld.CHI, man='789')
player.add_called_meld(meld)

player.draw_tile(tile)
discard = player.discard_tile()
self.assertEqual(self._to_string([discard]), '1s')

0 comments on commit 25ab8d8

Please sign in to comment.