def main(): if request.method == 'POST': rounds = request.form['rounds'] results = fizzbuzz(rounds) return render_template('fizzbuzz.html', results=results) return render_template('fizzbuzz.html')
def play_fizzbuzz(players): """ Main loop of Fizz Buzz game. Fizz buzz is a counting game where each player speaks a number from 1 to n in sequence, but with a few exceptions: - if the would-be spoken number is divisible by 3 the player must say fizz instead - if the would-be spoken number is divisible by 5 the player must say buzz instead - if the would-be spoken number is divisible by 3 and 5 the player must say fizzbuzz instead """ result = {'rounds': []} active_players = [player for player in players] current_round = 1 current_number = 1 game = Game(datetime.now(), players) while len(active_players) > 1 and current_round < MAX_ROUNDS: # During each round round_dict = {'round_number': current_round, 'moves': []} loosers = [] for player in active_players: # Each player has its own answer answer = generate_answer_for(player, current_number) move = {"player_name": player.player_name, "answer": answer} # check answer if answer == fizzbuzz(current_number): move['is_correct'] = True else: loosers.append(player) move['is_correct'] = False current_number = current_number + 1 round_dict['moves'].append(move) # play further in this round only if more players are in if len(active_players) - len(loosers) < 2: break # Loosers are not in the game anymore for looser in loosers: active_players.remove(looser) result['rounds'].append(round_dict) current_round = current_round + 1 if len(active_players) == 1: # Save game results and update winner status game.end_date = datetime.now() winner = active_players[0] winner.number_games_won = winner.number_games_won + 1 game.winner = winner game.number_of_rounds = current_round db.session.add(game) db.session.commit() result["winner"] = winner else: # Draw game - no winner result["winner"] = None return result