Add puzzle hints and per-word descriptions with sources

Hint is revealed on demand below the board while playing. After the game
ends, each word's description and source link are listed below the
puzzle, and hovering a solved word highlights its entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aidan Dona 2026-10-13 16:44:07 -05:00
parent 4a2309b589
commit b2f8e879ab
7 changed files with 141 additions and 21 deletions

View file

@ -6,7 +6,9 @@ const $game = document.getElementById('game');
const idMatch = location.pathname.match(/^\/play\/(\d+)/);
if (!idMatch) document.getElementById('nav-today').setAttribute('aria-current', 'page');
const state = { game: null, words: [], selected: new Set(), solved: [], mistakes: 0, guesses: [], tried: new Set(), done: false, won: false };
const state = { game: null, words: [], selected: new Set(), solved: [], mistakes: 0, guesses: [], tried: new Set(), done: false, won: false, showHint: false };
const icon = (name) => el('i', { class: `ph-bold ph-${name}`, 'aria-hidden': 'true' });
async function load() {
const { ok, data } = await api(idMatch ? `/api/games/${idMatch[1]}` : '/api/daily');
@ -26,7 +28,8 @@ async function load() {
function render() {
const board = el('div', { class: 'board' },
state.solved.map((g) => el('div', { class: `solved ${g.color}` }, el('strong', {}, g.name), el('span', {}, g.words.join(', ')))),
state.solved.map((g) => el('div', { class: `solved ${g.color}` }, el('strong', {}, g.name),
el('span', {}, g.words.flatMap((w, i) => [i ? ', ' : '', el('span', { class: 'word', 'data-word': w }, w)])))),
state.words.length ? el('div', { class: 'row4' }, state.words.map((w) =>
el('button', { class: 'tile', 'aria-pressed': String(state.selected.has(w)), disabled: state.done, onclick: () => toggle(w) }, w))) : null,
);
@ -34,15 +37,22 @@ function render() {
if (state.done) {
kids.push(resultPanel());
const details = detailsPanel();
if (details) kids.push(details);
} else {
kids.push(
el('div', { class: 'mistakes', 'aria-label': `${MAX_MISTAKES - state.mistakes} mistakes remaining` },
'Mistakes remaining:', [...Array(MAX_MISTAKES)].map((_, i) => el('i', { class: i < state.mistakes ? 'used' : '' }))),
el('div', { class: 'actions' },
el('button', { class: 'btn', onclick: shuffleWords }, el('i', { class: 'ph-bold ph-shuffle', 'aria-hidden': 'true' }), 'Shuffle'),
el('button', { class: 'btn', onclick: shuffleWords }, icon('shuffle'), 'Shuffle'),
el('button', { class: 'btn', disabled: !state.selected.size, onclick: () => { state.selected.clear(); render(); } }, 'Deselect all'),
el('button', { class: 'btn primary', disabled: state.selected.size !== 4, onclick: submit }, 'Submit')),
);
if (state.game.hint) {
kids.push(state.showHint
? el('p', { class: 'hint' }, icon('lightbulb'), el('span', {}, el('strong', {}, 'Hint: '), state.game.hint))
: el('div', { class: 'hint-toggle' }, el('button', { class: 'btn', onclick: () => { state.showHint = true; render(); } }, icon('lightbulb'), 'Show hint')));
}
}
// Re-rendering replaces the buttons, so put keyboard focus back on the same one.
const focused = $game.contains(document.activeElement) ? document.activeElement.textContent : null;
@ -122,10 +132,37 @@ function resultPanel() {
el('p', { class: 'sub' }, state.won ? `Finished with ${state.mistakes} mistake${state.mistakes === 1 ? '' : 's'}.` : 'Here are the groups.'),
el('pre', { 'aria-label': 'Your guesses' }, grid),
el('div', { class: 'actions' },
el('button', { class: 'btn primary', onclick: () => share(text) }, el('i', { class: 'ph-bold ph-copy', 'aria-hidden': 'true' }), 'Share results'),
el('button', { class: 'btn primary', onclick: () => share(text) }, icon('copy'), 'Share results'),
el('a', { class: 'btn', href: '/puzzles' }, 'More puzzles')));
}
function detailsPanel() {
const hasAny = state.solved.some((g) => g.details?.some((d) => d.description || d.source));
if (!hasAny) return null;
const host = (url) => { try { return new URL(url).hostname.replace(/^www\./, ''); } catch { return 'Source'; } };
return el('section', { class: 'details', 'aria-labelledby': 'details-h' },
el('h2', { id: 'details-h' }, 'About the words'),
state.solved.map((g) => el('div', { class: 'detail-group' },
el('h3', {}, el('span', { class: `swatch ${g.color}` }), g.name),
el('dl', {}, g.words.map((w, i) => {
const d = g.details?.[i] || {};
return el('div', { class: 'detail', 'data-word': w },
el('dt', {}, w),
el('dd', {}, d.description || null,
d.source ? el('a', { class: 'source', href: d.source, target: '_blank', rel: 'noopener noreferrer' }, icon('arrow-square-out'), host(d.source)) : null));
})))));
}
// After the game ends, hovering a word in a solved row highlights its description.
function highlight(e, on) {
const word = state.done && e.target.closest?.('.solved .word');
if (!word) return;
word.classList.toggle('hl', on);
$game.querySelector(`.detail[data-word="${CSS.escape(word.dataset.word)}"]`)?.classList.toggle('hl', on);
}
$game.addEventListener('mouseover', (e) => highlight(e, true));
$game.addEventListener('mouseout', (e) => highlight(e, false));
async function share(text) {
try {
await navigator.clipboard.writeText(text);