fix upper/lowercase

This commit is contained in:
Daniel Tsvetkov 2022-01-18 22:33:49 +01:00
parent 00c493e492
commit 390ea9e702
2 changed files with 13 additions and 11 deletions

21
app.py
View File

@ -14,6 +14,7 @@ GUESS_SPLITTER = '|'
STATE_PLAYING = 'PLAYING' STATE_PLAYING = 'PLAYING'
STATE_WIN = 'WIN' STATE_WIN = 'WIN'
STATE_LOSE = 'LOSE' STATE_LOSE = 'LOSE'
RANDOM_GAME_ID = -1
@app.route('/') @app.route('/')
@ -22,7 +23,8 @@ def home():
return render_template('home.html', num_games=num_games) return render_template('home.html', num_games=num_games)
def init_game(word): def init_game(game_id, word):
session['game_id'] = game_id
session['word'] = word session['word'] = word
session['guesses'] = '' session['guesses'] = ''
session['state'] = STATE_PLAYING session['state'] = STATE_PLAYING
@ -31,25 +33,23 @@ def init_game(word):
@app.route('/init/random') @app.route('/init/random')
def init_random_game(): def init_random_game():
word = random.choice([w for w in cat_words()]) word = random.choice([w for w in cat_words()])
session['game_id'] = -1 init_game(RANDOM_GAME_ID, word)
init_game(word)
return redirect(url_for('random_game')) return redirect(url_for('random_game'))
@app.route('/init/deterministic', methods=['post']) @app.route('/init/deterministic', methods=['post'])
def init_deterministic_game(): def init_deterministic_game():
game_id = request.form.get('game_id') game_id = request.form.get('game_id')
if not game_id.isdigit() or not 0 <= int(game_id) <= NUM_GAMES:
flash("game_id must be between 0 and {}".format(NUM_GAMES))
return redirect(url_for('home'))
return redirect(url_for('init_deterministic_game_id', game_id=game_id)) return redirect(url_for('init_deterministic_game_id', game_id=game_id))
@app.route('/init/deterministic/<int:game_id>') @app.route('/init/deterministic/<int:game_id>')
def init_deterministic_game_id(game_id): def init_deterministic_game_id(game_id):
if not 0 <= int(game_id) <= NUM_GAMES:
flash("game_id must be between 0 and {}".format(NUM_GAMES))
return redirect(url_for('home'))
word = [w for w in cat_words()][int(game_id)] word = [w for w in cat_words()][int(game_id)]
session['game_id'] = game_id init_game(game_id, word)
init_game(word)
return redirect(url_for('deterministic_game_id', game_id=game_id)) return redirect(url_for('deterministic_game_id', game_id=game_id))
@ -92,7 +92,7 @@ def random_game():
@app.route('/deterministic/<int:game_id>') @app.route('/deterministic/<int:game_id>')
def deterministic_game_id(game_id): def deterministic_game_id(game_id):
if 'word' not in session or 'state' not in session: if 'word' not in session or 'state' not in session or ('game_id' in session and session['game_id'] != game_id):
return redirect(url_for('init_deterministic_game_id', game_id=game_id)) return redirect(url_for('init_deterministic_game_id', game_id=game_id))
return build_game_state() return build_game_state()
@ -106,8 +106,9 @@ def make_guess():
if len(guess) != 5 and not all([x in ascii_lowercase for x in guess]): if len(guess) != 5 and not all([x in ascii_lowercase for x in guess]):
flash("Make a 5 letter guess.") flash("Make a 5 letter guess.")
return build_game_state() return build_game_state()
guess = guess.lower()
if guess not in cat_words(): if guess not in cat_words():
flash('Not in word list') flash('Not in word list: {}'.format(guess))
return build_game_state() return build_game_state()
word = session['word'] word = session['word']
round = len(session['guesses'].split(GUESS_SPLITTER)) round = len(session['guesses'].split(GUESS_SPLITTER))

View File

@ -13,7 +13,8 @@
</ul> </ul>
{% if state_playing %} {% if state_playing %}
<form method="post" action="{{ url_for('make_guess') }}"> <form method="post" action="{{ url_for('make_guess') }}">
<input maxlength="5" minlength="5" size="5" name="guess" autofocus autocomplete="off"/> <input maxlength="5" minlength="5" size="5" name="guess" autofocus autocomplete="off"
style="text-transform:uppercase"/>
<input type="submit"> <input type="submit">
</form> </form>
{% else %} {% else %}