fix solve
This commit is contained in:
parent
047d8704af
commit
54ce23e7a5
23
app.py
23
app.py
@ -31,12 +31,16 @@ COLOR_GREEN = 'green'
|
|||||||
COLOR_YELLOW = 'yellow'
|
COLOR_YELLOW = 'yellow'
|
||||||
|
|
||||||
ERROR_GAME_ID_RANGE = "game_id must be between 0 and {}".format(NUM_GAMES)
|
ERROR_GAME_ID_RANGE = "game_id must be between 0 and {}".format(NUM_GAMES)
|
||||||
ERROR_NEED_WORD = 'need a word'
|
ERROR_NEED_WORD = 'Need a word'
|
||||||
ERROR_GUESS_LENGTH = "Make a {} letter guess.".format(WORD_LENGTH)
|
ERROR_GUESS_LENGTH = "Need {} letters.".format(WORD_LENGTH)
|
||||||
ERROR_WORD_NOT_IN_LIST = 'Not in word list: {}'
|
ERROR_WORD_NOT_IN_LIST = 'Not in word list: {}'
|
||||||
ERROR_NO_GAME = 'No game started.'
|
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):
|
def init_game(game_id, word):
|
||||||
session[KEY_GAME_ID] = game_id
|
session[KEY_GAME_ID] = game_id
|
||||||
session[KEY_WORD] = word
|
session[KEY_WORD] = word
|
||||||
@ -75,6 +79,7 @@ def build_game_state():
|
|||||||
KEY_GUESS=KEY_GUESS,
|
KEY_GUESS=KEY_GUESS,
|
||||||
state=session[KEY_STATE])
|
state=session[KEY_STATE])
|
||||||
|
|
||||||
|
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
# ROUTES
|
# ROUTES
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
@ -86,6 +91,18 @@ def home():
|
|||||||
return render_template('home.html', num_games=num_games, KEY_GAME_ID=KEY_GAME_ID)
|
return render_template('home.html', num_games=num_games, KEY_GAME_ID=KEY_GAME_ID)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/find/<word>')
|
||||||
|
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')
|
@app.route('/init/random')
|
||||||
def init_random_game():
|
def init_random_game():
|
||||||
word = random.choice(WORDS)
|
word = random.choice(WORDS)
|
||||||
@ -131,7 +148,7 @@ def make_guess():
|
|||||||
return redirect(url_for('home'))
|
return redirect(url_for('home'))
|
||||||
guess = request.form.get(KEY_GUESS)
|
guess = request.form.get(KEY_GUESS)
|
||||||
guess = guess.lower()
|
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)
|
flash(ERROR_GUESS_LENGTH)
|
||||||
return build_game_state()
|
return build_game_state()
|
||||||
if guess not in WORDS:
|
if guess not in WORDS:
|
||||||
|
25
solve.py
25
solve.py
@ -3,14 +3,14 @@ import random
|
|||||||
from lib import cat_words, INSTRUCTIONS, BLANK, ROUNDS, DEBUG, DICTIONARY
|
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:
|
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 True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def remove_positional_letters(word, positional_letters, ):
|
def remove_positional_letters(word, positional_letters):
|
||||||
rv, unconsumed_letters = False, ''
|
rv, unconsumed_letters = False, ''
|
||||||
for pos_idx, positional_letter in enumerate(positional_letters):
|
for pos_idx, positional_letter in enumerate(positional_letters):
|
||||||
if positional_letter == BLANK:
|
if positional_letter == BLANK:
|
||||||
@ -24,14 +24,16 @@ def remove_positional_letters(word, positional_letters, ):
|
|||||||
|
|
||||||
|
|
||||||
def remove_non_positional_letters(word, non_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):
|
for non_pos_idx, non_positional_letter in enumerate(non_positional_letters):
|
||||||
if non_positional_letter == BLANK:
|
if non_positional_letter == BLANK:
|
||||||
continue
|
continue
|
||||||
|
unconsumed_letters += word[non_pos_idx]
|
||||||
if non_positional_letter not in word:
|
if non_positional_letter not in word:
|
||||||
return True
|
rv = True
|
||||||
if word[non_pos_idx] == non_positional_letter:
|
elif word[non_pos_idx] == non_positional_letter:
|
||||||
return True
|
rv = True
|
||||||
return False
|
return rv, unconsumed_letters
|
||||||
|
|
||||||
|
|
||||||
def round_words(non_letters, all_positional_letters, all_non_positional_letters, dictionary=DICTIONARY):
|
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
|
continue
|
||||||
# Find non-positional letters, e.g. h-s--
|
# Find non-positional letters, e.g. h-s--
|
||||||
for non_positional_letters in all_non_positional_letters:
|
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:
|
if skip:
|
||||||
break
|
break
|
||||||
if skip:
|
if skip:
|
||||||
continue
|
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:
|
if skip:
|
||||||
continue
|
continue
|
||||||
if not skip:
|
if not skip:
|
||||||
@ -152,12 +154,15 @@ def find_word(search):
|
|||||||
|
|
||||||
|
|
||||||
def do_test():
|
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'], ['-----'], dictionary=['slily'])] == ['slily']
|
||||||
assert [w for w in round_words('', ['----y'], ['y----'], dictionary=['slyly'])] == ['slyly']
|
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'], ['y----'], dictionary=['slyyy'])] == ['slyyy']
|
||||||
assert [w for w in round_words('', ['---yy'], ['-----'], 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('', ['-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():
|
def main():
|
||||||
|
Loading…
Reference in New Issue
Block a user