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]")
示例#2
0
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)
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 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]"
示例#5
0
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!")