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!"
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!"
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))
from lextrie import LexTrie from gametrie import GameTrie lextrie = LexTrie() lextrie.insert("a", "AAA") lextrie.insert("bb", "BBB") gametrie = GameTrie(lextrie._root, 2) def set_tree_again(node, num_players, players): if node._children: for child in node._children.keys(): if num_players == 1: set_tree_again(node._children[child], players, players) else: set_tree_again(node._children[child], num_players-1, players) if num_players == players: # me if node._data: node._data = None else: for child in node._children.keys(): if node._children[child]._data: node._data = 'is winning' else: for child in node._children.keys(): if node._children[child]._data: node._data = 'is winning' else: if num_players == players: