wordle/app.py
2022-01-18 22:42:38 +01:00

136 lines
4.1 KiB
Python

import random
from string import ascii_lowercase
from flask import Flask, render_template, session, request, redirect, url_for, flash
import sensitive
from lib import cat_words, ROUNDS
from play import build_non_letter_hint, build_letter_hint
app = Flask(__name__)
app.config['SECRET_KEY'] = sensitive.SECRET_KEY
WORDS = list(cat_words())
NUM_GAMES = len(WORDS) - 1
GUESS_SPLITTER = '|'
STATE_PLAYING = 'PLAYING'
STATE_WIN = 'WIN'
STATE_LOSE = 'LOSE'
RANDOM_GAME_ID = -1
def init_game(game_id, word):
session['game_id'] = game_id
session['word'] = word
session['guesses'] = ''
session['state'] = STATE_PLAYING
def resolve_guesses():
word = session['word']
guesses = session.get('guesses', '').split(GUESS_SPLITTER)
guesses_colored = []
for guess in guesses:
non_letters_hints = build_non_letter_hint(guess, word)
letters_hints = build_letter_hint(guess, word)
colored_guess = []
for pos, letter in enumerate(guess):
if letter in non_letters_hints:
color = 'grey'
elif letters_hints[pos] == letter.upper():
color = 'green'
elif letters_hints[pos] == letter:
color = 'yellow'
else:
color = 'grey'
colored_guess.append((letter, color))
guesses_colored.append(colored_guess)
return guesses_colored
def build_game_state():
guesses_colored = resolve_guesses()
return render_template('game.html',
guesses=guesses_colored,
state_playing=session['state'] == STATE_PLAYING,
state=session['state'])
@app.route('/')
def home():
num_games = NUM_GAMES
return render_template('home.html', num_games=num_games)
@app.route('/init/random')
def init_random_game():
word = random.choice(WORDS)
init_game(RANDOM_GAME_ID, word)
return redirect(url_for('random_game'))
@app.route('/init/deterministic', methods=['post'])
def init_deterministic_game():
game_id = request.form.get('game_id')
return redirect(url_for('init_deterministic_game_id', game_id=game_id))
@app.route('/init/deterministic/<int: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 = WORDS[int(game_id)]
init_game(game_id, word)
return redirect(url_for('deterministic_game_id', game_id=game_id))
@app.route('/random')
def random_game():
if 'word' not in session or 'state' not in session:
return redirect(url_for('init_random_game'))
return build_game_state()
@app.route('/deterministic/<int:game_id>')
def deterministic_game_id(game_id):
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 build_game_state()
@app.route('/guess', methods=['post'])
def make_guess():
if 'word' not in session or 'guesses' not in session:
flash('need a word')
return redirect(url_for('home'))
guess = request.form.get('guess')
if len(guess) != 5 and not all([x in ascii_lowercase for x in guess]):
flash("Make a 5 letter guess.")
return build_game_state()
guess = guess.lower()
if guess not in WORDS:
flash('Not in word list: {}'.format(guess))
return build_game_state()
word = session['word']
round = len(session['guesses'].split(GUESS_SPLITTER))
if round >= ROUNDS:
session['state'] = STATE_LOSE
if guess == word:
session['state'] = STATE_WIN
session['guesses'] += '{}{}'.format(guess, GUESS_SPLITTER)
if 'game_id' not in session:
flash('no game_id')
return redirect(url_for('home'))
game_id = session['game_id']
if game_id == -1:
return redirect(url_for('random_game'))
return redirect(url_for('deterministic_game_id', game_id=game_id))
def main():
app.run(debug=True)
if __name__ == "__main__":
main()