import random import json import os from flask import Flask, request, render_template_string app = Flask(__name__) # History to store (period, (color, size)) for observation history = [] # Load history from file if exists (backend) HISTORY_FILE = 'color_history.json' if os.path.exists(HISTORY_FILE): with open(HISTORY_FILE, 'r') as f: history_data = json.load(f) history = [(item[0], tuple(item[1])) for item in history_data] # Predefined patterns (backend) patterns = [ [('Red', 'Small'), ('Green', 'Big'), ('Red', 'Small'), ('Green', 'Big')], [('Red', 'Big'), ('Green', 'Small'), ('Red', 'Big'), ('Green', 'Small')], [('Green', 'Big'), ('Red', 'Small'), ('Green', 'Big'), ('Red', 'Small')], [('Red', 'Big'), ('Red', 'Big'), ('Green', 'Small'), ('Green', 'Small')], [('Green', 'Small'), ('Green', 'Small'), ('Red', 'Big'), ('Red', 'Big')], [('Red', 'Big'), ('Red', 'Big'), ('Red', 'Big'), ('Violet', 'Small')], [('Green', 'Small'), ('Green', 'Small'), ('Green', 'Small'), ('Violet', 'Big')], [('Red', 'Small'), ('Green', 'Small'), ('Green', 'Small'), ('Red', 'Big')], [('Green', 'Big'), ('Red', 'Big'), ('Red', 'Big'), ('Green', 'Small')], [('Red', 'Big'), ('Red', 'Big'), ('Violet', 'Small'), ('Red', 'Big')], [('Green', 'Small'), ('Green', 'Small'), ('Violet', 'Big'), ('Green', 'Small')], [('Violet', 'Small'), ('Red', 'Big'), ('Green', 'Big'), ('Violet', 'Small')], [('Red', 'Big'), ('Violet', 'Small'), ('Green', 'Big'), ('Violet', 'Small')], [('Red', 'Small'), ('Red', 'Small'), ('Violet', 'Big'), ('Green', 'Big')], [('Green', 'Big'), ('Green', 'Big'), ('Violet', 'Small'), ('Red', 'Small')], [('Violet', 'Big'), ('Red', 'Small'), ('Green', 'Small'), ('Violet', 'Big')], [('Red', 'Big'), ('Green', 'Big'), ('Violet', 'Small'), ('Red', 'Small')], [('Green', 'Small'), ('Red', 'Small'), ('Violet', 'Big'), ('Green', 'Big')], ] # Function to save history (backend) def save_history(): with open(HISTORY_FILE, 'w') as f: json.dump([(per, list(out)) for per, out in history], f) # Function to select best matching pattern (backend) def select_best_pattern(): if not history: return random.choice(patterns) max_pat_len = max(len(p) for p in patterns) recent = [outcome for _, outcome in history[-max_pat_len:]] if history else [] best_score = -1 best_pattern = None for pattern in patterns: pat_len = len(pattern) if len(recent) < 1: continue for shift in range(pat_len): score = 0 for i in range(min(len(recent), pat_len)): rec_color, rec_size = recent[-i-1] if i < len(recent) else (None, None) pat_color, pat_size = pattern[(shift - i) % pat_len] if rec_color == pat_color: score += 2 if rec_size == pat_size and rec_color is not None: score += 1 if score > best_score: best_score = score best_pattern = pattern return best_pattern if best_pattern else random.choice(patterns) # Initial pattern selection (backend) current_pattern = select_best_pattern() # Function to analyze history and generate strong predictions (backend) def analyze_history(): if len(history) < 2: return {'color': None, 'size': None} combined_trans = {} for i in range(len(history) - 1): prev = history[i][1] next_out = history[i+1][1] if prev not in combined_trans: combined_trans[prev] = {} if next_out not in combined_trans[prev]: combined_trans[prev][next_out] = 0 combined_trans[prev][next_out] += 1 strong_color = None strong_size = None if history: last = history[-1][1] if last in combined_trans: counts = combined_trans[last] total = sum(counts.values()) if total > 0: probs = {k: v / total for k, v in counts.items()} max_prob = max(probs.values()) if max_prob > 0.55: best_next = max(probs, key=probs.get) strong_color, strong_size = best_next return {'color': strong_color, 'size': strong_size} # Flask route for the website @app.route('/', methods=['GET', 'POST']) def index(): prediction_color = "" prediction_size = "" show_actual_form = False period = None message = "" if request.method == 'POST': if 'period' in request.form: period_str = request.form.get('period') try: period = int(period_str) # Validate period number must be 2 or more digits if len(str(period)) < 2: message = "Period number must be 2 or more digits." else: strong_preds = analyze_history() strong_color = strong_preds['color'] strong_size = strong_preds['size'] if strong_color and strong_size: prediction_color = strong_color prediction_size = strong_size elif strong_color: prediction_color = strong_color index = (period - 1) % len(current_pattern) _, prediction_size = current_pattern[index] elif strong_size: index = (period - 1) % len(current_pattern) prediction_color, _ = current_pattern[index] prediction_size = strong_size else: index = (period - 1) % len(current_pattern) prediction_color, prediction_size = current_pattern[index] show_actual_form = True except ValueError: message = "Invalid period number." elif 'actual_color' in request.form and 'actual_size' in request.form: period = int(request.form.get('period')) actual_color = request.form.get('actual_color') actual_size = request.form.get('actual_size') if actual_color in ['Red', 'Green', 'Violet'] and actual_size in ['Big', 'Small']: history.append((period, (actual_color, actual_size))) save_history() predicted_color = request.form.get('predicted_color') predicted_size = request.form.get('predicted_size') if actual_color == predicted_color and actual_size == predicted_size: message = "Prediction CORRECT!" else: message = "Prediction WRONG!" current_pattern = select_best_pattern() show_actual_form = False return render_template_string(''' BDG Colour Predictor
{% if prediction_color %}
Color: {{ prediction_color }}
Bet: {{ prediction_size }}
{% endif %} {% if show_actual_form %}
Red Green Violet
Big Small
{% endif %} {% if message %} {% if "Invalid" in message or "must" in message %}

{{ message }}

{% else %}

{{ message }}

{% endif %} {% endif %}
''', prediction_color=prediction_color, prediction_size=prediction_size, show_actual_form=show_actual_form, period=period, message=message) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)