diff --git a/app.py b/app.py index c87f43b..13f4210 100644 --- a/app.py +++ b/app.py @@ -31,12 +31,16 @@ COLOR_GREEN = 'green' COLOR_YELLOW = 'yellow' ERROR_GAME_ID_RANGE = "game_id must be between 0 and {}".format(NUM_GAMES) -ERROR_NEED_WORD = 'need a word' -ERROR_GUESS_LENGTH = "Make a {} letter guess.".format(WORD_LENGTH) +ERROR_NEED_WORD = 'Need a word' +ERROR_GUESS_LENGTH = "Need {} letters.".format(WORD_LENGTH) ERROR_WORD_NOT_IN_LIST = 'Not in word list: {}' ERROR_NO_GAME = 'No game started.' +def verify_word_length(word): + return len(word) == WORD_LENGTH and all([x in ascii_lowercase for x in word]) + + def init_game(game_id, word): session[KEY_GAME_ID] = game_id session[KEY_WORD] = word @@ -75,6 +79,7 @@ def build_game_state(): KEY_GUESS=KEY_GUESS, state=session[KEY_STATE]) + # ############################################################################# # ROUTES # ############################################################################# @@ -86,6 +91,18 @@ def home(): return render_template('home.html', num_games=num_games, KEY_GAME_ID=KEY_GAME_ID) +@app.route('/find/') +def find_word(word): + if not verify_word_length(word): + flash(ERROR_GUESS_LENGTH) + return redirect(url_for('home')) + if word not in WORDS: + flash(ERROR_WORD_NOT_IN_LIST.format(word)) + return redirect(url_for('home')) + game_id = WORDS.index(word) + return redirect(url_for('init_deterministic_game_id', game_id=game_id)) + + @app.route('/init/random') def init_random_game(): word = random.choice(WORDS) @@ -131,7 +148,7 @@ def make_guess(): return redirect(url_for('home')) guess = request.form.get(KEY_GUESS) guess = guess.lower() - if len(guess) != WORD_LENGTH and not all([x in ascii_lowercase for x in guess]): + if not verify_word_length(guess): flash(ERROR_GUESS_LENGTH) return build_game_state() if guess not in WORDS: diff --git a/solve.py b/solve.py index bca6c72..fe479e2 100644 --- a/solve.py +++ b/solve.py @@ -3,14 +3,14 @@ import random from lib import cat_words, INSTRUCTIONS, BLANK, ROUNDS, DEBUG, DICTIONARY -def remove_non_letters(word, non_letters, unconsumed_letters): +def remove_non_letters(word, non_letters, unconsumed_letters, non_pos_unconsumed_letters): for non_letter in non_letters: - if non_letter in word and non_letter in unconsumed_letters: + if non_letter in word and non_letter in unconsumed_letters and non_letter in non_pos_unconsumed_letters: return True return False -def remove_positional_letters(word, positional_letters, ): +def remove_positional_letters(word, positional_letters): rv, unconsumed_letters = False, '' for pos_idx, positional_letter in enumerate(positional_letters): if positional_letter == BLANK: @@ -24,14 +24,16 @@ def remove_positional_letters(word, positional_letters, ): def remove_non_positional_letters(word, non_positional_letters): + rv, unconsumed_letters = False, '' for non_pos_idx, non_positional_letter in enumerate(non_positional_letters): if non_positional_letter == BLANK: continue + unconsumed_letters += word[non_pos_idx] if non_positional_letter not in word: - return True - if word[non_pos_idx] == non_positional_letter: - return True - return False + rv = True + elif word[non_pos_idx] == non_positional_letter: + rv = True + return rv, unconsumed_letters def round_words(non_letters, all_positional_letters, all_non_positional_letters, dictionary=DICTIONARY): @@ -46,12 +48,12 @@ def round_words(non_letters, all_positional_letters, all_non_positional_letters, continue # Find non-positional letters, e.g. h-s-- for non_positional_letters in all_non_positional_letters: - skip = remove_non_positional_letters(word, non_positional_letters) + skip, non_pos_unconsumed_letters = remove_non_positional_letters(word, non_positional_letters) if skip: break if skip: continue - skip = remove_non_letters(word, non_letters, unconsumed_letters) + skip = remove_non_letters(word, non_letters, unconsumed_letters, non_pos_unconsumed_letters) if skip: continue if not skip: @@ -152,12 +154,15 @@ def find_word(search): def do_test(): + assert [w for w in round_words('', ['towns'], ['-----'], dictionary=['towns'])] == ['towns'] + assert [w for w in round_words('', ['-----'], ['stown'], dictionary=['towns'])] == ['towns'] assert [w for w in round_words('', ['----y'], ['-----'], dictionary=['slily'])] == ['slily'] assert [w for w in round_words('', ['----y'], ['y----'], dictionary=['slyly'])] == ['slyly'] assert [w for w in round_words('', ['---yy'], ['y----'], dictionary=['slyyy'])] == ['slyyy'] assert [w for w in round_words('', ['---yy'], ['-----'], dictionary=['slyyy'])] == ['slyyy'] assert [w for w in round_words('', ['-y---'], ['--y--'], dictionary=['sysys'])] == ['sysys'] - assert [w for w in round_words('nu', ['-o-ns'], ['-----'], dictionary=['towns'])] == ['towns'] + assert [w for w in round_words('nu', ['-o-ns'], ['-----'], dictionary=['towns'])] == ['towns'] # nouns + assert [w for w in round_words('fos', ['-l---'], ['---s-'], dictionary=['slung'])] == ['slung'] # typed basil floss def main():