Example #1
0
def process_the_words() -> 'html':
    session['end_time'] = time.perf_counter()
    we_have_a_winner = True
    seven_words = request.form['seven_words'].lower().split(' ')
    seven_words = [w for w in seven_words if len(w) >= 1]
    # Now that we have the data, let's perform the checks.
    nw = len(seven_words)
    if nw != 7:
        we_have_a_winner = False
        flash('You have an incorrect number of words: {}, not 7.'.format(nw))
    session['the_words'] = seven_words
    disallowed_letters = []
    for word in seven_words:
        disallowed = [
            letter
            for (letter,
                 ok) in word_utils.check_letters(session['sourceword'], word)
            if not ok
        ]
        disallowed_letters.extend(disallowed)
    if disallowed_letters:
        we_have_a_winner = False
        flash('You used these invalid letters: ' +
              ' '.join(set(disallowed_letters)))
    misspelt_words = [
        word for (word, ok) in check_spellings(seven_words) if not ok
    ]
    if misspelt_words:
        we_have_a_winner = False
        flash('You misspelt these words: ' + ' '.join(sorted(misspelt_words)))
    short_words = [
        word for (word, ok) in word_utils.check_size(seven_words) if not ok
    ]
    if short_words:
        we_have_a_winner = False
        flash('These words are too small: ' + ' '.join(sorted(short_words)))
    if word_utils.duplicates(seven_words):
        we_have_a_winner = False
        flash('You have duplicates in your list: ' +
              ' '.join(sorted(seven_words)))
    if word_utils.check_not_sourceword(seven_words, session['sourceword']):
        we_have_a_winner = False
        flash('You cannot use the source word: ' + session['sourceword'])
    # With all the checks performed, how did we do?
    time_taken = round(session['end_time'] - session['start_time'], 2)
    if time_taken < 5.0:
        we_have_a_winner = False
        flash("Skullduggery is afoot.  There's no way you were that quick!")
    if we_have_a_winner:
        session['how_long'] = str(time_taken)
        flash('Congratulations! You took ' + session['how_long'] + ' seconds.')
        session['done'] = False
        return render_template('winner.html', title="You're a winner!")
    else:
        return render_template('loser.html', title='Better luck next time.')
Example #2
0
def check_words(sourceword, seven_words):
    seven_words = seven_words.strip().split(' ')
    errors = []
    winner = True
    if seven_words[0] == '':
        winner = False
        error = ("No words entered. You've gotta gimme something to work with"
                 " here!!.", [])
        errors.append(error)
        return (winner, errors)
    if len(seven_words) != 7:
        winner = False
        error = ('You did not provide seven words, maybe more, maybe '
                 'less.', [])
        errors.append(error)
        return (winner, errors)
    disallowed_letters = []
    for word in seven_words:
        disallowed = [
            letter
            for (letter, ok) in word_utils.check_letters(sourceword, word)
            if not ok
        ]
        disallowed_letters.extend(disallowed)  # Here
    if disallowed_letters:
        winner = False
        error = ('Not allowed letters: ', set(disallowed_letters))
        errors.append(error)
    misspelt_words = [
        word for (word, ok) in check_spellings(seven_words) if not ok
    ]
    if misspelt_words:
        winner = False
        error = ('Misspelt words:', sorted(misspelt_words))
        errors.append(error)
    short_words = [
        word for (word, ok) in word_utils.check_size(seven_words) if not ok
    ]
    if short_words:
        winner = False
        error = ('These words are too small: ', sorted(short_words))
        errors.append(error)
    if word_utils.duplicates(seven_words):
        winner = False
        error = ('You have duplicates:', sorted(seven_words))
        errors.append(error)
    if word_utils.check_not_sourceword(seven_words, sourceword):
        winner = False
        error = ('You cannot use the source word: ', [sourceword])
        errors.append(error)
    if winner:
        return (winner, [])
    return (winner, errors)
Example #3
0
         letter
         for (letter, ok) in word_utils.check_letters(sourceword, word)
         if not ok
     ]
     disallowed_letters.extend(disallowed)
 if disallowed_letters:
     we_have_a_winner = False
     print('Not allowed letters:', set(disallowed_letters))
 misspelt_words = [
     word for (word, ok) in check_spellings(seven_words) if not ok
 ]
 if misspelt_words:
     we_have_a_winner = False
     print('Misspelt words:', sorted(misspelt_words))
 short_words = [
     word for (word, ok) in word_utils.check_size(seven_words) if not ok
 ]
 if short_words:
     we_have_a_winner = False
     print('These words are too small:', sorted(short_words))
 if word_utils.duplicates(seven_words):
     we_have_a_winner = False
     print('You have duplicates:', sorted(seven_words))
 if word_utils.check_not_sourceword(seven_words, sourceword):
     we_have_a_winner = False
     print('You cannot use the source word:', sourceword)
 if we_have_a_winner:
     how_long = round(end_time - start_time, 2)
     print('You took', how_long, 'seconds.', end=' ')
     user = input("What's your name: ").strip()
     data_utils.add_to_scores(user, how_long)
Example #4
0
def processWords():
    if request.method == 'POST':
        wordList = list((request.form['wordList']).strip().split(' '))
        for words in wordList:
            words = words.lower()
        start = (datetime.now() - session['startTime']).total_seconds()
        session['timeTook'] = round(start, 2)
        winner = True
        errors = []
        # Rule Zero
        if len(wordList) != 7:
            winner = False
            if len(wordList) < 7:
                tmp = Markup(("You didn't type in enough words."
                              " Words Detected: <b>{0}</b> Words needed:"
                              " <b>7</b>").format(len(wordList)))
            else:
                tmp = Markup(('You typed in too many words.'
                              ' Words Detected: <b>{0}</b> Words needed:'
                              ' <b>7</b>').format(len(wordList)))
            flash(tmp)
        # Rule One
        for words in wordList:
            wrongLettersList = []
            isError = False
            tmp = word_utils.check_letters(session['sourceWord'], words)
            for letters in tmp:
                if letters[1] is False:
                    winner = False
                    isError = True
                    wrongLettersList.append(letters[0])
            if (isError):
                isError = False
                tmp = Markup(('You used these wrong letters'
                              ' <b>[{0}]</b> in this word: <b>{1}</b>'
                              '.').format(','.join(wrongLettersList), words))
                flash(tmp)
        # Rule Two
        spellCheck = wordgame.check_spellings(wordList)
        for words in spellCheck:
            if words[1] is False:
                winner = False
                tmp = Markup(('The word: <b>{0}</b> is not spelled'
                              ' correctly').format(words[0]))
                flash(tmp)
        # Rule Three/
        length = word_utils.check_size(wordList)
        for words in length:
            if words[1] is False:
                winner = False
                tmp = Markup(('The word: <b>{0}</b> is not three'
                              ' or more characters long').format(words[0]))
                flash(tmp)
        # Rule Four
        if word_utils.duplicates(wordList):
            winner = False
            wordListCopy = wordList.copy()
            for words in wordList:
                wordListCopy.remove(words)
                if words in wordListCopy:
                    tmp = Markup(('The word: <b>{0}</b>'
                                  ' was duplicated').format(words))
                    flash(tmp)
        # Rule Five
        sameSourceWord = []
        sameSourceWord = (word_utils.check_not_sourceword(
            wordList, session['sourceWord']))
        if len(sameSourceWord) != 0:
            winner = False
            tmp = Markup(('You tried to use the source word <b>{0}</b>'
                          ' time(s)').format(len(sameSourceWord)))
            flash(tmp)
        # End Rules
        if winner:
            return render_template('winner.html',
                                   the_title='Winner!',
                                   timeTook=session['timeTook'])
        return render_template('loser.html', the_title='Loser')