Enable "cheating" on lights out game.

This commit is contained in:
Yaro Kasear 2025-08-11 15:09:18 -05:00
parent 63c503c84a
commit 109176573c
2 changed files with 25 additions and 12 deletions

View file

@ -11,30 +11,30 @@ from ..utils.load import eager_load_worklog_relationships, eager_load_inventory_
def generate_solvable_matrix(level, seed_clicks=None): def generate_solvable_matrix(level, seed_clicks=None):
size = level + 3 size = level + 3
matrix = [[True for _ in range(size)] for _ in range(size)] matrix = [[True for _ in range(size)] for _ in range(size)]
presses = []
def toggle(x, y): def press(x, y):
directions = [(0, 0), (-1, 0), (1, 0), (0, -1), (0, 1)] # self, N, S, W, E # record the press (once)
for dx, dy in directions: presses.append((x, y))
# apply its effect
for dx, dy in [(0,0),(-1,0),(1,0),(0,-1),(0,1)]:
nx, ny = x + dx, y + dy nx, ny = x + dx, y + dy
if 0 <= nx < size and 0 <= ny < size: if 0 <= nx < size and 0 <= ny < size:
matrix[nx][ny] = not matrix[nx][ny] matrix[nx][ny] = not matrix[nx][ny]
# Pick a number of random "clicks"
num_clicks = seed_clicks if seed_clicks is not None else random.randint(size, size * 2) num_clicks = seed_clicks if seed_clicks is not None else random.randint(size, size * 2)
for _ in range(num_clicks): for _ in range(num_clicks):
x = random.randint(0, size - 1) x = random.randint(0, size - 1)
y = random.randint(0, size - 1) y = random.randint(0, size - 1)
toggle(x, y) press(x, y)
return matrix
return matrix, presses # return the PRESS LIST as the “solution”
@main.route("/12648243") @main.route("/12648243")
def coffee(): def coffee():
level = request.args.get('level', 0, int) level = request.args.get('level', 0, int)
matrix = generate_solvable_matrix(level) matrix, clicked = generate_solvable_matrix(level)
return render_template("coffee.html", matrix=matrix, level=level) return render_template("coffee.html", matrix=matrix, level=level, clicked=clicked)
@main.route("/") @main.route("/")
def index(): def index():

View file

@ -11,7 +11,7 @@
{% for x in range(level + 3) %} {% for x in range(level + 3) %}
<div class="row" style="min-height: {{ 80 / (level + 3) }}vh;"> <div class="row" style="min-height: {{ 80 / (level + 3) }}vh;">
{% for y in range(level + 3) %} {% for y in range(level + 3) %}
<div class="col m-2 p-0 align-items-center d-flex justify-content-center border border-black rounded light shadow" id="light-{{ x }}-{{ y }}"> <div class="col m-2 p-0 align-items-center d-flex justify-content-center border border-{% if (x, y) in clicked and false %}danger border-2{% else %}black{% endif %} rounded light shadow" id="light-{{ x }}-{{ y }}">
<div class="form-check"> <div class="form-check">
<input type="checkbox" class="form-check-input d-none" id="checkbox-{{ x }}-{{ y }}"{% if matrix[x][y] %} checked{% endif %}> <input type="checkbox" class="form-check-input d-none" id="checkbox-{{ x }}-{{ y }}"{% if matrix[x][y] %} checked{% endif %}>
</div> </div>
@ -24,8 +24,21 @@
{% block script %} {% block script %}
const gridSize = {{ level + 3 }}; const gridSize = {{ level + 3 }};
var clickOrder = {};
var clickCounter = 0;
updateLights(); updateLights(); // fine, whatever that does
{{ clicked | tojson }}.forEach(([x, y]) => {
clickCounter++;
const key = `${x}-${y}`;
(clickOrder[key] ??= []).push(clickCounter); // initialize if missing, then push
});
Object.entries(clickOrder).forEach(([key, value]) => {
const light = document.querySelector(`#light-${key}`);
// light.innerHTML += value;
});
function updateLights() { function updateLights() {
document.querySelectorAll('.light').forEach(light => { document.querySelectorAll('.light').forEach(light => {