Added Clues/Input as Columns/Columns,rows Detection

This commit is contained in:
abdelaziz
2026-02-17 11:25:11 +01:00
parent e053310c53
commit 02940021e2

View File

@@ -2,171 +2,231 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CROSSWORD</title>
<title>Crossword Puzzle</title>
<style> <style>
* { body { font-family: 'Courier New', monospace; font-size: 12px; padding: 16px; }
margin: 0;
padding: 0;
box-sizing: border-box;
}
body { .dir-btn { font-family: 'Courier New', monospace; background: none; border: none; cursor: pointer; color: #999; padding-right: 6px; }
font-family: Arial, sans-serif; .dir-btn.active { color: #000; font-weight: bold; }
background: #fff;
padding: 20px;
}
.container { .layout { display: flex; gap: 24px; flex-wrap: wrap; margin-top: 8px; }
max-width: 1200px;
margin: 0 auto;
}
h1 {
text-align: center;
color: #000;
margin-bottom: 20px;
font-size: 24px;
}
.grid-wrapper {
display: flex;
justify-content: center;
margin: 20px 0;
}
.crossword-grid { .crossword-grid {
display: inline-grid;
gap: 0;
border: 2px solid #000;
}
.cell {
width: 30px;
height: 30px;
border: 1px solid #000;
background: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
font-weight: bold;
text-transform: uppercase;
position: relative;
}
.cell.blocked {
background: #000;
}
.cell input {
width: 100%;
height: 100%;
border: none;
text-align: center;
font-size: 14px;
font-weight: bold;
text-transform: uppercase;
background: transparent;
outline: none;
}
.cell-number {
position: absolute;
top: 1px;
left: 2px;
font-size: 8px;
font-weight: normal;
color: #000;
}
.clues {
display: grid; display: grid;
grid-template-columns: 1fr 1fr; grid-template-columns: repeat(21, 26px);
gap: 30px; border-top: 1px solid #000;
margin-top: 30px; border-left: 1px solid #000;
} }
.clues-section h3 { .cell { width: 26px; height: 26px; border-right: 1px solid #000; border-bottom: 1px solid #000; position: relative; }
font-size: 18px; .cell.blocked { background: #000; }
margin-bottom: 10px; .cell.hl-word { background: #c8d8f0; }
border-bottom: 2px solid #000; .cell.hl-active { background: #3a6fd8; }
padding-bottom: 5px; .cell.hl-active input, .cell.hl-active .cell-number { color: #fff; }
}
.clue { .cell input { width: 100%; height: 100%; border: none; background: transparent; font-family: 'Courier New', monospace; font-size: 12px; font-weight: bold; text-align: center; text-transform: uppercase; outline: none; padding-top: 4px; caret-color: transparent; }
padding: 5px 0;
font-size: 14px; .cell-number { position: absolute; top: 1px; left: 2px; font-size: 7px; pointer-events: none; }
line-height: 1.5;
} .clues { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 20px; }
.clues-section h3 { font-size: 11px; border-bottom: 1px solid #000; margin-bottom: 4px; }
.clue { display: flex; gap: 4px; font-size: 11px; line-height: 1.6; cursor: pointer; }
.clue.active { background: #c8d8f0; }
.clue-num { min-width: 18px; flex-shrink: 0; }
</style> </style>
</head> </head>
<body> <body>
<div class="container">
<h1>Crossword Puzzle - 21×21</h1>
<div class="grid-wrapper"> <b>CROSSWORD</b>
<div style="margin:6px 0">
<button class="dir-btn active" id="btnAcross" onclick="setDirection('across')">ACROSS</button>
<button class="dir-btn" id="btnDown" onclick="setDirection('down')">DOWN</button>
</div>
<div class="layout">
<div>
<div class="crossword-grid" id="grid"></div> <div class="crossword-grid" id="grid"></div>
</div> </div>
<div style="min-width:220px;flex:1">
<div class="clues"> <div class="clues">
<div class="clues-section"> <div class="clues-section"><h3>Across</h3><div id="cluesAcross"></div></div>
<h3>Across</h3> <div class="clues-section"><h3>Down</h3><div id="cluesDown"></div></div>
<div id="cluesAcross">
</div>
</div>
<div class="clues-section">
<h3>Down</h3>
<div id="cluesDown">
</div>
</div> </div>
</div> </div>
</div> </div>
<script> <script>
const GRID_SIZE = 21; const GRID_SIZE = 21;
let direction = 'across';
let activeRow = -1, activeCol = -1;
function createGrid() { const cellData = Array.from({length: GRID_SIZE}, () => Array(GRID_SIZE).fill(null));
const clueMap = { across: {}, down: {} };
const acrossClues = {1:"Moves like water",2:"Ancient writing surface",3:"Celestial body",4:"Swift journey",5:"Old musical key",6:"Grain storage structure",7:"Feline sound",8:"Desert wanderer",9:"Poetic foot",10:"City in Tuscany",11:"Measured pace",12:"Spiral galaxy centre",13:"Feudal estate",14:"Nautical term",15:"Musical interval",16:"Volcanic output",17:"Cured hide",18:"Silent agreement",19:"River mouth deposit",20:"Geometric solid",21:"Norse figure"};
const downClues = {1:"Flowing script",2:"Tidal zone",3:"Mountain ridge",4:"Distant signal",5:"Ancient measure",6:"Harbour light",7:"Frozen mass",8:"River bend",9:"Winged deity",10:"Cavern feature",11:"Woven fabric",12:"Sacred text",13:"Coastal feature",14:"Solar flare",15:"Deep valley",16:"Stone formation",17:"Ocean current",18:"Wind direction",19:"Compass point",20:"Tide creature",21:"Storm surge"};
function isBlocked(r, c) { return (r + c) % 7 === 0 && r % 3 === 0; }
function buildGrid() {
const grid = document.getElementById('grid'); const grid = document.getElementById('grid');
grid.style.gridTemplateColumns = `repeat(${GRID_SIZE}, 30px)`; let n = 1;
let clueNumber = 1;
for (let r = 0; r < GRID_SIZE; r++) { for (let r = 0; r < GRID_SIZE; r++) {
for (let c = 0; c < GRID_SIZE; c++) { for (let c = 0; c < GRID_SIZE; c++) {
const cell = document.createElement('div'); const cell = document.createElement('div');
cell.className = 'cell'; cell.className = 'cell';
if (isBlocked(r, c)) {
const isBlocked = (r + c) % 7 === 0 && r % 3 === 0;
if (isBlocked) {
cell.classList.add('blocked'); cell.classList.add('blocked');
cellData[r][c] = { blocked: true, el: cell };
} else { } else {
const needsNumber = (c === 0 || (c > 0 && isBlackSquare(r, c-1))) || const sa = (c === 0 || isBlocked(r, c-1)) && (c < GRID_SIZE-1 && !isBlocked(r, c+1));
(r === 0 || (r > 0 && isBlackSquare(r-1, c))); const sd = (r === 0 || isBlocked(r-1, c)) && (r < GRID_SIZE-1 && !isBlocked(r+1, c));
if (sa || sd) {
if (needsNumber && (c < GRID_SIZE-1 || r < GRID_SIZE-1)) { const ns = document.createElement('span');
const num = document.createElement('span'); ns.className = 'cell-number'; ns.textContent = n;
num.className = 'cell-number'; if (sa) clueMap.across[n] = {r, c};
num.textContent = clueNumber++; if (sd) clueMap.down[n] = {r, c};
cell.appendChild(num); cell.appendChild(ns); n++;
} }
const input = document.createElement('input'); const input = document.createElement('input');
input.maxLength = 1; input.type = 'text'; input.maxLength = 1;
input.setAttribute('autocomplete','off'); input.setAttribute('autocorrect','off'); input.setAttribute('spellcheck','false');
input.addEventListener('focus', () => onFocus(r, c));
input.addEventListener('click', () => onClick(r, c));
input.addEventListener('keydown', e => onKey(e, r, c));
input.addEventListener('input', e => onInput(e, r, c));
cell.appendChild(input); cell.appendChild(input);
cellData[r][c] = { blocked: false, el: cell, input };
} }
grid.appendChild(cell); grid.appendChild(cell);
} }
} }
buildClues();
} }
function isBlackSquare(r, c) { function buildClues() {
return (r + c) % 7 === 0 && r % 3 === 0; const ac = document.getElementById('cluesAcross');
const dn = document.getElementById('cluesDown');
Object.keys(clueMap.across).map(Number).sort((a,b)=>a-b).forEach(n => {
const d = document.createElement('div');
d.className = 'clue'; d.id = `clue-across-${n}`;
d.innerHTML = `<span class="clue-num">${n}.</span><span>${acrossClues[n]||'Clue '+n}</span>`;
d.addEventListener('click', () => jumpToClue('across', n)); ac.appendChild(d);
});
Object.keys(clueMap.down).map(Number).sort((a,b)=>a-b).forEach(n => {
const d = document.createElement('div');
d.className = 'clue'; d.id = `clue-down-${n}`;
d.innerHTML = `<span class="clue-num">${n}.</span><span>${downClues[n]||'Clue '+n}</span>`;
d.addEventListener('click', () => jumpToClue('down', n)); dn.appendChild(d);
});
} }
createGrid(); function getWord(r, c, dir) {
const cells = [];
if (dir === 'across') {
let sc = c; while (sc > 0 && !isBlocked(r, sc-1)) sc--;
while (sc < GRID_SIZE && !isBlocked(r, sc)) { cells.push([r, sc]); sc++; }
} else {
let sr = r; while (sr > 0 && !isBlocked(sr-1, c)) sr--;
while (sr < GRID_SIZE && !isBlocked(sr, c)) { cells.push([sr, c]); sr++; }
}
return cells;
}
function applyHL(wc, ar, ac2) {
for (let r = 0; r < GRID_SIZE; r++)
for (let c = 0; c < GRID_SIZE; c++) {
const d = cellData[r][c];
if (d && !d.blocked) d.el.classList.remove('hl-word','hl-active');
}
wc.forEach(([r,c]) => cellData[r][c].el.classList.add('hl-word'));
if (cellData[ar]?.[ac2] && !cellData[ar][ac2].blocked) cellData[ar][ac2].el.classList.add('hl-active');
}
function onFocus(r, c) {
activeRow = r; activeCol = c;
applyHL(getWord(r, c, direction), r, c);
updateClueHL(r, c);
}
function onClick(r, c) {
if (activeRow === r && activeCol === c) {
direction = direction === 'across' ? 'down' : 'across';
document.getElementById('btnAcross').classList.toggle('active', direction==='across');
document.getElementById('btnDown').classList.toggle('active', direction==='down');
applyHL(getWord(r, c, direction), r, c);
updateClueHL(r, c);
}
activeRow = r; activeCol = c;
}
function onKey(e, r, c) {
if (e.key==='ArrowRight') { e.preventDefault(); move(r,c,0,1); }
else if (e.key==='ArrowLeft') { e.preventDefault(); move(r,c,0,-1); }
else if (e.key==='ArrowDown') { e.preventDefault(); move(r,c,1,0); }
else if (e.key==='ArrowUp') { e.preventDefault(); move(r,c,-1,0); }
else if (e.key==='Tab') { e.preventDefault(); advWord(e.shiftKey?-1:1); }
else if (e.key==='Backspace' && cellData[r][c].input.value==='') { e.preventDefault(); stepBack(r, c); }
}
function onInput(e, r, c) {
const d = cellData[r][c];
if (d.input.value.length > 0) {
d.input.value = d.input.value.slice(-1).toUpperCase();
const wc = getWord(r, c, direction);
const idx = wc.findIndex(([wr,wc2])=>wr===r&&wc2===c);
if (idx !== -1 && idx < wc.length-1) cellData[wc[idx+1][0]][wc[idx+1][1]].input.focus();
}
}
function move(r, c, dr, dc) {
const nr=r+dr, nc=c+dc;
if (nr>=0&&nr<GRID_SIZE&&nc>=0&&nc<GRID_SIZE) { const d=cellData[nr][nc]; if (d&&!d.blocked) d.input.focus(); }
}
function stepBack(r, c) {
const wc=getWord(r,c,direction), idx=wc.findIndex(([wr,wc2])=>wr===r&&wc2===c);
if (idx>0) { cellData[wc[idx-1][0]][wc[idx-1][1]].input.value=''; cellData[wc[idx-1][0]][wc[idx-1][1]].input.focus(); }
}
function advWord(delta) {
const map=direction==='across'?clueMap.across:clueMap.down;
const keys=Object.keys(map).map(Number).sort((a,b)=>a-b);
const wc=getWord(activeRow,activeCol,direction), [sr,sc]=wc[0];
const cur=keys.find(n=>map[n].r===sr&&map[n].c===sc);
jumpToClue(direction, keys[((keys.indexOf(cur)+delta)+keys.length)%keys.length]);
}
function jumpToClue(dir, num) {
direction=dir;
document.getElementById('btnAcross').classList.toggle('active', direction==='across');
document.getElementById('btnDown').classList.toggle('active', direction==='down');
const map=dir==='across'?clueMap.across:clueMap.down;
if (map[num]) cellData[map[num].r][map[num].c].input.focus();
}
function updateClueHL(r, c) {
document.querySelectorAll('.clue').forEach(el=>el.classList.remove('active'));
const wc=getWord(r,c,direction); if (!wc.length) return;
const [sr,sc]=wc[0], map=direction==='across'?clueMap.across:clueMap.down;
for (const [num,pos] of Object.entries(map)) {
if (pos.r===sr&&pos.c===sc) {
const el=document.getElementById(`clue-${direction}-${num}`);
if (el) { el.classList.add('active'); el.scrollIntoView({block:'nearest',behavior:'smooth'}); }
break;
}
}
}
function setDirection(dir) {
direction=dir;
document.getElementById('btnAcross').classList.toggle('active',dir==='across');
document.getElementById('btnDown').classList.toggle('active',dir==='down');
if (activeRow>=0) { applyHL(getWord(activeRow,activeCol,direction),activeRow,activeCol); updateClueHL(activeRow,activeCol); }
}
buildGrid();
</script> </script>
</body> </body>
</html> </html>