134 lines
4.0 KiB
Python
134 lines
4.0 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
|
|
NUM_GAMES = len(list(cat_words())) - 1
|
|
GUESS_SPLITTER = '|'
|
|
STATE_PLAYING = 'PLAYING'
|
|
STATE_WIN = 'WIN'
|
|
STATE_LOSE = 'LOSE'
|
|
|
|
|
|
@app.route('/')
|
|
def home():
|
|
num_games = NUM_GAMES # len(list(cat_words())) - 1
|
|
return render_template('home.html', num_games=num_games)
|
|
|
|
|
|
def init_game(word):
|
|
session['word'] = word
|
|
session['guesses'] = ''
|
|
session['state'] = STATE_PLAYING
|
|
|
|
|
|
@app.route('/init/random')
|
|
def init_random_game():
|
|
word = random.choice([w for w in cat_words()])
|
|
session['game_id'] = -1
|
|
init_game(word)
|
|
return redirect(url_for('random_game'))
|
|
|
|
|
|
@app.route('/init/deterministic', methods=['post'])
|
|
def init_deterministic_game():
|
|
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))
|
|
|
|
|
|
@app.route('/init/deterministic/<int:game_id>')
|
|
def init_deterministic_game_id(game_id):
|
|
word = [w for w in cat_words()][int(game_id)]
|
|
session['game_id'] = game_id
|
|
init_game(word)
|
|
return redirect(url_for('deterministic_game_id', game_id=game_id))
|
|
|
|
|
|
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('/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:
|
|
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()
|
|
if guess not in cat_words():
|
|
flash('Not in word list')
|
|
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()
|