class LexTrieSimpleTestCase(TrieSimpleTestCase): '''Test inserting several words, with some common prefixes, into a Trie and retrieving them.''' def setUp(self): """Set up a simple trie. The words are the same as in the file short.txt.""" self.trie = LexTrie() self.populate() def testFindAll0(self): """Test the find_all() method with valid one-letter prefix.""" found = self.trie.find_all("c") found.sort() assert found == ["coo", "cook", "cool", "cooler"],\ ("Found " + str(found) + ", but expected some permulation of [coo,cook,cool,cooler]") def testFindAll1(self): """Test the find_all() method with valid word.""" found = self.trie.find_all("bar") found.sort() assert found == ["bar", "baristas"],\ ("Found " + str(found) + ", but expected some permulation of [bar,baristas]")
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)
class Player(object): """A player in the game Ghost. Player has the following attributes: -- _lexicon: a LexTrie; the words that this Player knows, with their definitions. -- _gameTrie: a GameTrie; this Player's mental image of the game, built at the time the game prefix was '_prefix'. -- _prefix: this was the game prefix string at the time the _game_trie was built. """ def __init__(self, path): """Create a new Player whose lexicon is based on the vocabulary from the input word file 'path'. -- path: a string; the path to an input file; see LexTrie for the format of the input file.""" self._lexicon = LexTrie(path) self._game_trie = None self._prefix = "" def words(self, prefix): """Return the list of all words which begin with string 'prefix' that this Player knows.""" return self._lexicon.find_all(prefix)
def __init__(self, path): """Create a new Player whose lexicon is based on the vocabulary from the input word file 'path'. -- path: a string; the path to an input file; see LexTrie for the format of the input file.""" self._lexicon = LexTrie(path) self._game_trie = None self._prefix = ""
class LexTrieEmptyTestCase(TrieEmptyTestCase): '''Test retrieving from empty Trie.''' def setUp(self): """Set up an empty trie.""" self.trie = LexTrie() def testFindAll(self): """Test the find_all() method.""" assert self.trie.find_all("coo") == [], \ 'found words in an empty tree!'
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!"
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)
class LexTrieSingleTestCase(TrieSingleTestCase): '''Test inserting a single word into an empty Trie and retrieving it.''' def setUp(self): """Set up a single word trie.""" self.trie = LexTrie() self.populate() def testFindAllWord(self): """Test the find_all() method with valid word.""" found = self.trie.find_all("coo") assert found == ["coo"],\ "found " + str(found) + ", but expected [coo]"
def __init__(self, players, path): """Create a new Ghost game with the dictionary from the word input file 'path'. The (is_human?, iq) list 'players' specifies the Players in this Game.""" self._dictionary = LexTrie(path) self._prefix = "" self._players = [] for (is_human, iq) in players: if is_human: self._players.append(HumanPlayer(path)) else: lexicon = _make_lexicon(path, iq) self._players.append(ComputerPlayer(lexicon)) # remove the lexicon file # you may want to comment this line for debugging purposes os.remove(lexicon)
def setUp(self): """Set up a simple trie. The words are the same as in the file short.txt.""" self.trie = LexTrie() self.populate()
def setUp(self): """Set up a single word trie.""" self.trie = LexTrie() self.populate()
class Ghost(object): """A game of Ghost. Ghost has the following attriburtes: -- _dictionary: a wordfile (see LextTrie for format) of all known words. -- _prefix: the current prefix in the game. -- _players: the list of Players in the game. """ def __init__(self, players, path): """Create a new Ghost game with the dictionary from the word input file 'path'. The (is_human?, iq) list 'players' specifies the Players in this Game.""" self._dictionary = LexTrie(path) self._prefix = "" self._players = [] for (is_human, iq) in players: if is_human: self._players.append(HumanPlayer(path)) else: lexicon = _make_lexicon(path, iq) self._players.append(ComputerPlayer(lexicon)) # remove the lexicon file # you may want to comment this line for debugging purposes os.remove(lexicon) def play(self): """Play Ghost.""" while(True): for (p, player) in enumerate(self._players): try: self._play_round(p, player) except GameOver: print "Game Over!" return def _play_round(self, p, player): """Play one round of Ghost: player 'p' plays.""" print ">>> " + self._prefix + " <<<" (challenge, next_char) = player.play(self._prefix, len(self._players)) if challenge: self._play_challenge(p) raise GameOver print "Player " + str(p) + " added '" + next_char + "'." self._prefix += next_char data = self._dictionary.data(self._prefix) if data: print (self._prefix + "::" + data + "\nPlayer " + str(p) + " loses!") raise GameOver def _play_challenge(self, p): """Player 'p' challenges.""" print "Player " + str(p) + " challenges!" try: word = self._dictionary.find_all(self._prefix)[0] data = self._dictionary.data(word) print (word + "::" + data + "\nPlayer " + str(p) + " loses!\n") except IndexError: print ("No such word! Player " + str((p - 1) % len(self._players)) + " loses!")
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)
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))
def setUp(self): """Set up an empty trie.""" self.trie = LexTrie()
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: