当前位置: 首页>>代码示例>>Python>>正文


Python Trie.tr_has_key方法代码示例

本文整理汇总了Python中trie.Trie.tr_has_key方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.tr_has_key方法的具体用法?Python Trie.tr_has_key怎么用?Python Trie.tr_has_key使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trie.Trie的用法示例。


在下文中一共展示了Trie.tr_has_key方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import tr_has_key [as 别名]
class OptimalGhost:
    def __init__(self):
        self.tr = Trie()

    # NOTE: Assumes file_name has one word per line as found in WORD.LST.
    def load_words(self, file_name):
        """Load Trie data structure with words of len >= 4."""

        ins_key = self.tr.insert_key
        with open(file_name, 'r') as f:
            for line in f.readlines():
                line = line.strip()
                if len(line) >= 4:
                    ins_key(line, True)

    def computer_letter_choice(self, curr_word, turn):
        """Optimally choose the next letter in the game.

        At the current game state we will keep track of the possible
        letter choices to advance using Trie.start_with_prefix. Our initial
        choice is to choose a letter in a word(odd length) that will increase
        our chances of winning, otherwise we will choose a word(even length)
        with maximal length to keep the game going.
        """

        print "Computer's Turn ->"
        sleep(1.0)
        win_wrds = self.tr.start_with_prefix(curr_word)
        odd_wrds = [w for w in win_wrds if len(w) % 2 == 1]
        evn_wrds = [w for w in win_wrds if len(w) % 2 == 0]
        evn_wrds.sort(key=len, reverse=True)

        word = ''
        if odd_wrds:
            word = choice(odd_wrds)
            if len(word) == turn + 1:
                word = choice(odd_wrds)
        elif evn_wrds:
            word = evn_wrds[0]
        else:
            word = choice(ascii_lowercase)

        next_letter = word[turn] if len(word) > 1 else word[0]

        return next_letter

    def boot_game(self):
        """Start an interactive game of Optimal Ghost.

        A real player starts first and afterwards it is the computer's
        turn. After either entity has entered a choice we will check whether
        they have completed a word or if they have entered a letter that
        doesn't allow to advance a game.
        """

        player_turn = 'Player'
        curr_word = ''
        turn = 0
        print "\nWelcome to Optimal Ghost!\n"

        while(True):
            print 'Current Word: {0}'.format(curr_word)

            if player_turn == 'Player':
                next_letter = raw_input("Enter a letter -> ").strip()
                if next_letter not in ascii_lowercase:
                    print "Please enter a lower case letter."
                    continue
                elif len(next_letter) != 1:
                    print "Please enter a single letter."
                    continue
                curr_word = curr_word + next_letter
            else:  # Computer turn
                next_letter = self.computer_letter_choice(curr_word, turn)
                print 'Computer chooses letter: {0}'.format(next_letter)
                curr_word = curr_word + next_letter

            # check valid word
            if self.tr.tr_has_key(curr_word):
                print '*** {0} Loses ***'.format(player_turn)
                print '*** Completed word: {0}'.format(curr_word)
                break

            # check invalid word + cannot advance, eg allx
            if not self.tr.start_with_prefix(curr_word):
                print '*** {0} Loses ***'.format(player_turn)
                print '*** Invalid word, cannot advance: {0}'.format(curr_word)
                break

            turn = turn + 1
            player_turn = 'Computer' if player_turn == 'Player' else 'Player'
开发者ID:ccarlos,项目名称:optimal-ghost,代码行数:93,代码来源:ghost.py


注:本文中的trie.Trie.tr_has_key方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。