Files
LerningCSW/MainPage/Abdelaziz/CrossWords.html

232 lines
10 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CROSSWORD</title>
<style>
body { font-family: 'Courier New', monospace; font-size: 12px; padding: 16px; }
.dir-btn { font-family: 'Courier New', monospace; background: none; border: none; cursor: pointer; color: #999; padding-right: 6px; }
.dir-btn.active { color: #000; font-weight: bold; }
.layout { display: flex; gap: 24px; flex-wrap: wrap; margin-top: 8px; }
.crossword-grid {
display: grid;
grid-template-columns: repeat(21, 26px);
border-top: 1px solid #000;
border-left: 1px solid #000;
}
.cell { width: 26px; height: 26px; border-right: 1px solid #000; border-bottom: 1px solid #000; position: relative; }
.cell.blocked { background: #000; }
.cell.hl-word { background: #c8d8f0; }
.cell.hl-active { background: #3a6fd8; }
.cell.hl-active input, .cell.hl-active .cell-number { color: #fff; }
.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; }
.cell-number { position: absolute; top: 1px; left: 2px; font-size: 7px; pointer-events: none; }
.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>
</head>
<body>
<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>
<div style="min-width:220px;flex:1">
<div class="clues">
<div class="clues-section"><h3>Across</h3><div id="cluesAcross"></div></div>
<div class="clues-section"><h3>Down</h3><div id="cluesDown"></div></div>
</div>
</div>
</div>
<script>
const GRID_SIZE = 21;
let direction = 'across';
let activeRow = -1, activeCol = -1;
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');
let n = 1;
for (let r = 0; r < GRID_SIZE; r++) {
for (let c = 0; c < GRID_SIZE; c++) {
const cell = document.createElement('div');
cell.className = 'cell';
if (isBlocked(r, c)) {
cell.classList.add('blocked');
cellData[r][c] = { blocked: true, el: cell };
} else {
const sa = (c === 0 || isBlocked(r, c-1)) && (c < GRID_SIZE-1 && !isBlocked(r, c+1));
const sd = (r === 0 || isBlocked(r-1, c)) && (r < GRID_SIZE-1 && !isBlocked(r+1, c));
if (sa || sd) {
const ns = document.createElement('span');
ns.className = 'cell-number'; ns.textContent = n;
if (sa) clueMap.across[n] = {r, c};
if (sd) clueMap.down[n] = {r, c};
cell.appendChild(ns); n++;
}
const input = document.createElement('input');
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);
cellData[r][c] = { blocked: false, el: cell, input };
}
grid.appendChild(cell);
}
}
buildClues();
}
function buildClues() {
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);
});
}
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>
</body>
</html>