def runGuess(self, numGuesses): """ Gets and evaluates one guess and increases the score """ guess = self.screen.guess(numGuesses) # Get initial guess if WordValidator.correctWord(guess): self.score = self.score + WordGuessGame.SCORES[numGuesses] self.screen.winRound(guess, self.score) return True response = WordValidator.validate(guess) # Validate the guess self.screen.respond(response) # Has Screen display the response to the guess
class TextFilesParser(TextDataParser): def __init__(self, textDataFormatter, textResultsMapper): self.textResultsMapper=textResultsMapper self.textFormatter=textDataFormatter self.wordValidator=WordValidator() def Parse(self, textFiles): wordCounts = {} wordDocuments={} wordSentences={} wordTokenizer=RegexpTokenizer(r'\w+') for fileName in textFiles: with open( fileName, "r" ) as fileObject: rawText=fileObject.read() formattedRawText=self.textFormatter.Format(rawText) sentences = tokenize.sent_tokenize(formattedRawText.decode('utf-8')) for sentence in sentences: words = wordTokenizer.tokenize(sentence) for word in words: if (self.wordValidator.Validate(word.lower())): if (word in wordCounts.keys()): wordCounts[word]=wordCounts[word]+1 wordSentences[word].add(sentence) wordDocuments[word].add(fileName) else: wordCounts[word]=1 wordSentences[word]={sentence} wordDocuments[word]={fileName} output = self.textResultsMapper.MapTextResults(wordCounts, wordDocuments, wordSentences) return output
def start(self): """ Starts the game, runs until told to exit """ self.exit = False WordValidator.target = None while not self.exit: if not WordValidator.nextWord(): break self.gameLoop() self.screen.winGame()
def guess(self, numGuesses): """ Prompts for a guess, and then reads a guess """ while True: print "Enter your", len(WordValidator.target), "letter guess!" print "Guess correctly to score %d points!" % WordGuessGame.SCORES[numGuesses] if numGuesses == 0: """ Let user know no penalty for very first guess """ print "First try! You get a freebe!" elif numGuesses == WordGuessGame.ATTEMPTS -1: """ Let user know its the last try """ print "Last try!" else: """ Tell user the number of guesses they have left """ print "You have %d guesses left" % (WordGuessGame.ATTEMPTS - numGuesses) guess = raw_input() response = WordValidator.invalidGuess(guess) if not response: return guess.lower() print response
def testValidateGuess_Valid_Capitals(self): """ Recognize valid guesses with Capital letters """ guess = self.word.upper() assert not WordValidator.invalidGuess(guess), "Should be valid guess"
def testCheckMatch_incorrect(self): """ Recognize an incorrect match of characters """ assert not WordValidator.match('a', 'b'), "Should not be a match"
def testValidateGuess_Valid_Lower(self): """ Recognize valid guesses with Lowercase letters """ guess = self.word assert not WordValidator.invalidGuess(guess), "Should be valid guess"
def __init__(self, textDataFormatter, textResultsMapper): self.textResultsMapper=textResultsMapper self.textFormatter=textDataFormatter self.wordValidator=WordValidator()
def testCheckClose_closeDown(self): """ Recognize a character within 5 characters down of the target """ assert WordValidator.close("f", "a"), "Should be close"
def testCheckClose_notClose(self): """ Recognize a character within 5 characters down of the target """ assert not WordValidator.close("a", "z"), "Should not be close"
def testCheckUp_down(self): """ Recognize target character is down in the alphabet """ assert not WordValidator.up("a", "b"), "Should be down in the alphabet"
def testCheckClose_closeUp(self): """ Recognize a character within 5 characters up of the target """ assert WordValidator.close("a", "f"), "Should be close"
def testValidateGuess_Invalid_IncorrectLength(self): """ Recognize invalid guesses with incorrect length """ guess = "" assert WordValidator.invalidGuess(guess) == WordValidator.INVALID_LENGTH, "Should be invalid guess - >invalid length"
def testCheckUp_up(self): """ Recognize target character is up in the alphabet """ assert WordValidator.up("b", "a"), "Should be up in the alphabet"
def testCheckMatch_correct(self): """ Recognize a correct match of characters """ assert WordValidator.match('a', 'a'), "Should be a match"
def testCorrectWord_incorrect(self): """ Recognize incorrect matches between target and guess """ WordValidator.correctWord("abc"), "Should be incorrect word"
def testCorrectWord_correct(self): """ Recognize correct matches between target and guess """ assert WordValidator.correctWord("sky") , "Should be the correct word"
def testValidateGuess_Invalid_InvalidCharacters_Etc(self): """ Recognize invalid guesses with other characters """ guess = "sk!" assert WordValidator.invalidGuess(guess) == WordValidator.INVALID_CHARACTERS, "Should be invalid guess - >invalid characters(etc)"
def testValidateGuess_Invalid_InvalidCharacters_Numbers(self): """ Recognize invalid guesses with numbers """ guess = "sk1" assert WordValidator.invalidGuess(guess) == WordValidator.INVALID_CHARACTERS, "Should be invalid guess - >invalid characters(numbers)"