コード例 #1
0
class LargerTestCase(unittest.TestCase):
    """Set up for larger test cases."""
    
    def setUp(self, num_players):
        """Make a lex and game tries for a game with 'num_players'
        players. The words are the same as in the file short.txt."""

        self.lextrie = LexTrie()
        self.lextrie.insert("cool", "Cool's Data")
        self.lextrie.insert("coo", "Coo's Data")
        self.lextrie.insert("bar", "Bar's Data")
        self.lextrie.insert("cooler", "Cooler's Data")
        self.lextrie.insert("baristas", "Baristas's Data")
        self.lextrie.insert("cook", "Cook's Data")
        self.lextrie.insert("banner", "Banner's Data")
        self.lextrie.insert("bag", "Bag's Data")
        self.lextrie.insert("bank", "Bank's Data")
        self.gametrie = GameTrie(self.lextrie._root, num_players)
        
    def tearDown(self):
        """Clean up."""
        
        self.lextrie = None
        self.gametrie = None

    def winning(self, word):
        """Return whether playing 'word' results in a winning position."""
        
        return self.gametrie.is_winning(self.gametrie.find_node(word))
コード例 #2
0
    def setUp(self):
        """Make a simle lex trie."""

        self.lextrie = LexTrie()
        self.lextrie.insert("a", "AAA")
        self.lextrie.insert("bb", "BBB")
        self.gametrie = GameTrie(self.lextrie._root, 2)
コード例 #3
0
ファイル: player.py プロジェクト: jianghong/code-samples
    def play(self, prefix, num_players):
        """Return the tuple (challenge?, the character to append to
        string 'prefix') in the current game."""

        # get to the node in my lexicon trie that corresponds to
        # prefix 'prefix'
        prefix_node = self._lexicon.find_node(prefix)

        # if I do not know a word that begins with 'prefix',
        # challenge -- nothing to lose!
        if (not prefix_node) or prefix_node.is_leaf():
            return (True, "")

        # if I don't yet have a game trie, make one
        if not self._game_trie:
            self._prefix = prefix
            self._game_trie = GameTrie(prefix_node, num_players)

        # find where I am in my game trie
        current_state = self._game_trie.find_node(prefix[len(self._prefix) :])

        # if there is a winning child, return its key
        # otherwise, if there is a child which does not
        # immediately lose, return its key
        # otherwise, any child's key would do
        (nexts, nxt) = (current_state.children(), None)
        for key in nexts:
            if self._game_trie.is_winning(nexts[key]):
                return (False, key)
            elif not nxt or not self._lexicon.is_word(prefix + key):
                nxt = key

        return (False, nxt)
コード例 #4
0
    def setUp(self, num_players):
        """Make a lex and game tries for a game with 'num_players'
        players. The words are the same as in the file short.txt."""

        self.lextrie = LexTrie()
        self.lextrie.insert("cool", "Cool's Data")
        self.lextrie.insert("coo", "Coo's Data")
        self.lextrie.insert("bar", "Bar's Data")
        self.lextrie.insert("cooler", "Cooler's Data")
        self.lextrie.insert("baristas", "Baristas's Data")
        self.lextrie.insert("cook", "Cook's Data")
        self.lextrie.insert("banner", "Banner's Data")
        self.lextrie.insert("bag", "Bag's Data")
        self.lextrie.insert("bank", "Bank's Data")
        self.gametrie = GameTrie(self.lextrie._root, num_players)
コード例 #5
0
class SingleTestCase(unittest.TestCase):
    """Game trie based on a single one-letter word lex trie, root is
    not winning."""

    def setUp(self):
        """Make a single one-letter word lex trie."""
        
        self.lextrie = LexTrie()
        self.lextrie.insert("a", "AAA")
        self.gametrie = GameTrie(self.lextrie._root, 2)
        
    def tearDown(self):
        """Clean up."""
        
        self.lextrie = None
        self.gametrie = None

    def test(self):
        """Test: the root of this trie should not be winning."""
        
        win = self.gametrie.is_winning(self.gametrie._root)
        assert not win,\
               "root should not be winning!"
コード例 #6
0
class SimpleTestCase(unittest.TestCase):
    """Game trie based on a very simple lex trie, root is winning."""

    def setUp(self):
        """Make a simle lex trie."""

        self.lextrie = LexTrie()
        self.lextrie.insert("a", "AAA")
        self.lextrie.insert("bb", "BBB")
        self.gametrie = GameTrie(self.lextrie._root, 2)
        
    def tearDown(self):
        """Clean up."""
        
        self.lextrie = None
        self.gametrie = None

    def test(self):
        """Test: the root of this trie should be winning."""

        win = self.gametrie.is_winning(self.gametrie._root)
        assert win,\
               "root should be winning!"
コード例 #7
0
 def setUp(self):
     """Make a single one-letter word lex trie."""
     
     self.lextrie = LexTrie()
     self.lextrie.insert("a", "AAA")
     self.gametrie = GameTrie(self.lextrie._root, 2)