Example #1
0
    def decide_discard(self) -> Tile:
        # discard the orphan tile
        start = time()
        tile = self.decide_discard_by_loneliest_orphan()
        duration = time() - start
        self.record_strategies_time('decide_discard_by_loneliest_orphan',
                                    duration)
        if tile:
            self.record_strategies('decide_discard_by_loneliest_orphan')
            return tile

        # check for ready chance
        test = self._concealed[:-1]
        is_ready = Rule.is_ready(test)

        if not is_ready:
            start = time()
            tile = self.decide_discard_by_remove_melds()
            duration = time() - start
            self.record_strategies_time('decide_discard_by_remove_melds',
                                        duration)
            if tile:
                self.record_strategies('decide_discard_by_remove_melds')
                return tile

        if is_ready:
            start = time()
            tile = self.decide_discard_by_is_ready()
            duration = time() - start
            self.record_strategies_time('decide_discard_by_is_ready', duration)
            if tile:
                self.record_strategies('decide_discard_by_is_ready')
                return tile

        # finally, decide by random
        start = time()
        tile = self.decide_discard_random()
        duration = time() - start
        self.record_strategies_time('decide_discard_random', duration)
        self.record_strategies('decide_discard_random')
        return tile
Example #2
0
    def decide_discard(self, players_count: int = 4, wall: list = []) -> Tile:
        start = time()
        if len(self._concealed) <= 5:
            tiles = self.decide_discard_by_value(wall=None,
                                                 players_count=players_count,
                                                 deep=2)
            duration = time() - start
            self.record_strategies_time('decide_discard_by_value_2_last',
                                        duration)
            self.record_strategies('decide_discard_by_value_2_last')
            if tiles:
                return choice(tiles)

        # discard the orphan tile
        # check for ready chance
        # discard the orphan tile
        start = time()
        test = self._concealed[:-1]
        is_ready = Rule.is_ready(test)
        if is_ready:
            tile = self.decide_discard_by_loneliest_orphan(
                keep_one_orphan=True)
        else:
            tile = self.decide_discard_by_loneliest_orphan(
                keep_one_orphan=False)
        duration = time() - start
        self.record_strategies('decide_discard_by_loneliest_orphan')
        self.record_strategies_time('decide_discard_by_loneliest_orphan',
                                    duration)
        if tile:
            return tile

        start = time()
        tiles = self.decide_discard_by_value(wall=wall,
                                             players_count=players_count,
                                             deep=1)
        duration = time() - start
        self.record_strategies_time('decide_discard_by_value_1', duration)
        self.record_strategies('decide_discard_by_value_1')
        if tiles:
            return choice(tiles)

        start = time()
        tiles = self.decide_discard_by_left_meld_and_eye()
        duration = time() - start
        self.record_strategies_time('decide_discard_by_left_meld_and_eye',
                                    duration)
        self.record_strategies('decide_discard_by_left_meld_and_eye')
        if tiles:
            return choice(tiles)

        start = time()
        arr = Rule.convert_tiles_to_arr(self.concealed)
        combins = MjMath.get_best_meld_combins_from_arr(arr)
        if combins and len(combins) > 0 and len(combins[0]) > 3:
            tiles = self.decide_discard_by_value(wall=None,
                                                 players_count=players_count,
                                                 deep=2)
            duration = time() - start
            self.record_strategies_time('decide_discard_by_value_2', duration)
            self.record_strategies('decide_discard_by_value_2')
            if tiles:
                return choice(tiles)

        # finally, decide by random
        start = time()
        tile = self.decide_discard_random()
        duration = time() - start
        self.record_strategies_time('decide_discard_random', duration)
        self.record_strategies('decide_discard_random')
        return tile