-
Notifications
You must be signed in to change notification settings - Fork 2
feat(classic-games): 新增 Mini N-Queens 小游戏 #1236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wyf027
wants to merge
1
commit into
main
Choose a base branch
from
codex/classic-game-20260718-0554-n-queens-lite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+188
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Mini N-Queens</title> | ||
| <script src="https://cdn.tailwindcss.com"></script> | ||
| </head> | ||
| <body class="min-h-screen bg-slate-950 text-slate-100"> | ||
| <main class="mx-auto flex min-h-screen max-w-5xl flex-col gap-5 px-4 py-6"> | ||
| <header class="flex flex-col gap-3 border-b border-slate-800 pb-4 sm:flex-row sm:items-end sm:justify-between"> | ||
| <div> | ||
| <p class="text-sm font-semibold uppercase tracking-wide text-cyan-300">Classic puzzle</p> | ||
| <h1 class="text-3xl font-black text-white">Mini N-Queens</h1> | ||
| <p class="mt-1 max-w-2xl text-sm text-slate-300">Place 6 queens on the board so no two share a row, column, or diagonal.</p> | ||
| </div> | ||
| <div class="flex gap-2"> | ||
| <button id="hintBtn" class="rounded bg-cyan-400 px-4 py-2 text-sm font-bold text-slate-950 hover:bg-cyan-300">Hint</button> | ||
| <button id="resetBtn" class="rounded border border-slate-600 px-4 py-2 text-sm font-bold text-slate-100 hover:bg-slate-800">Reset</button> | ||
| </div> | ||
| </header> | ||
|
|
||
| <section class="grid gap-4 lg:grid-cols-[1fr_18rem]"> | ||
| <div class="rounded border border-slate-800 bg-slate-900 p-3"> | ||
| <div id="board" class="grid grid-cols-6 gap-2"></div> | ||
| </div> | ||
|
|
||
| <aside class="flex flex-col gap-3"> | ||
| <div class="rounded border border-slate-800 bg-slate-900 p-4"> | ||
| <p class="text-sm font-semibold text-slate-400">Status</p> | ||
| <p id="statusText" class="mt-2 text-xl font-black text-white">Place your queens.</p> | ||
| </div> | ||
| <div class="grid grid-cols-2 gap-3"> | ||
| <div class="rounded border border-slate-800 bg-slate-900 p-4"> | ||
| <p class="text-xs font-semibold uppercase text-slate-500">Queens</p> | ||
| <p id="queenCount" class="mt-1 text-2xl font-black text-white">0/6</p> | ||
| </div> | ||
| <div class="rounded border border-slate-800 bg-slate-900 p-4"> | ||
| <p class="text-xs font-semibold uppercase text-slate-500">Conflicts</p> | ||
| <p id="conflictCount" class="mt-1 text-2xl font-black text-white">0</p> | ||
| </div> | ||
| </div> | ||
| <div class="rounded border border-slate-800 bg-slate-900 p-4 text-sm text-slate-300"> | ||
| <p class="font-bold text-white">Rules</p> | ||
| <p class="mt-2">Click a square to add or remove a queen. Red queens are attacking another queen. Solve the puzzle when all 6 queens are safe.</p> | ||
| </div> | ||
| </aside> | ||
| </section> | ||
| </main> | ||
|
|
||
| <script> | ||
| const size = 6; | ||
| const solution = [1, 3, 5, 0, 2, 4]; | ||
| const queens = new Set(); | ||
| const boardEl = document.getElementById("board"); | ||
| const statusText = document.getElementById("statusText"); | ||
| const queenCount = document.getElementById("queenCount"); | ||
| const conflictCount = document.getElementById("conflictCount"); | ||
| const resetBtn = document.getElementById("resetBtn"); | ||
| const hintBtn = document.getElementById("hintBtn"); | ||
|
|
||
| function keyOf(row, col) { | ||
| return `${row},${col}`; | ||
| } | ||
|
|
||
| function parseKey(key) { | ||
| return key.split(",").map(Number); | ||
| } | ||
|
|
||
| function conflictsFor(row, col) { | ||
| const conflicts = []; | ||
| for (const key of queens) { | ||
| const [queenRow, queenCol] = parseKey(key); | ||
| if (queenRow === row && queenCol === col) { | ||
| continue; | ||
| } | ||
| const sameRow = queenRow === row; | ||
| const sameCol = queenCol === col; | ||
| const sameDiag = Math.abs(queenRow - row) === Math.abs(queenCol - col); | ||
| if (sameRow || sameCol || sameDiag) { | ||
| conflicts.push(key); | ||
| } | ||
| } | ||
| return conflicts; | ||
| } | ||
|
|
||
| function boardStats() { | ||
| let conflictPairs = 0; | ||
| const unsafeQueens = new Set(); | ||
| const placed = [...queens]; | ||
| for (let first = 0; first < placed.length; first += 1) { | ||
| const [rowA, colA] = parseKey(placed[first]); | ||
| for (let second = first + 1; second < placed.length; second += 1) { | ||
| const [rowB, colB] = parseKey(placed[second]); | ||
| const attacking = rowA === rowB || colA === colB || Math.abs(rowA - rowB) === Math.abs(colA - colB); | ||
| if (attacking) { | ||
| conflictPairs += 1; | ||
| unsafeQueens.add(placed[first]); | ||
| unsafeQueens.add(placed[second]); | ||
| } | ||
| } | ||
| } | ||
| return { | ||
| placed: queens.size, | ||
| conflictPairs, | ||
| unsafeQueens, | ||
| solved: queens.size === size && conflictPairs === 0 | ||
| }; | ||
| } | ||
|
|
||
| function toggleQueen(row, col) { | ||
| const key = keyOf(row, col); | ||
| if (queens.has(key)) { | ||
| queens.delete(key); | ||
| } else if (queens.size < size) { | ||
| queens.add(key); | ||
| } | ||
| render(); | ||
| } | ||
|
|
||
| function placeHint() { | ||
| for (let row = 0; row < size; row += 1) { | ||
| const rowHasQueen = [...queens].some((key) => parseKey(key)[0] === row); | ||
| if (!rowHasQueen) { | ||
| queens.add(keyOf(row, solution[row])); | ||
| render(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function resetGame() { | ||
| queens.clear(); | ||
| render(); | ||
| } | ||
|
|
||
| function renderBoard(stats) { | ||
| boardEl.innerHTML = ""; | ||
| for (let row = 0; row < size; row += 1) { | ||
| for (let col = 0; col < size; col += 1) { | ||
| const key = keyOf(row, col); | ||
| const hasQueen = queens.has(key); | ||
| const unsafe = stats.unsafeQueens.has(key); | ||
| const button = document.createElement("button"); | ||
| button.type = "button"; | ||
| button.setAttribute("aria-label", `row ${row + 1}, column ${col + 1}`); | ||
| button.className = [ | ||
| "aspect-square rounded border text-xl font-black transition focus:outline-none focus:ring-2 focus:ring-cyan-300", | ||
| (row + col) % 2 === 0 ? "border-slate-700 bg-slate-800" : "border-slate-700 bg-slate-700", | ||
| hasQueen && unsafe ? "border-rose-300 bg-rose-500 text-white" : "", | ||
| hasQueen && !unsafe ? "border-amber-200 bg-amber-300 text-slate-950" : "", | ||
| !hasQueen ? "hover:bg-cyan-900" : "" | ||
| ].join(" "); | ||
| button.textContent = hasQueen ? "Q" : ""; | ||
| button.addEventListener("click", () => toggleQueen(row, col)); | ||
| boardEl.appendChild(button); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function render() { | ||
| const stats = boardStats(); | ||
| renderBoard(stats); | ||
| queenCount.textContent = `${stats.placed}/${size}`; | ||
| conflictCount.textContent = String(stats.conflictPairs); | ||
| if (stats.solved) { | ||
| statusText.textContent = "Solved. Every queen is safe."; | ||
| statusText.className = "mt-2 text-xl font-black text-emerald-300"; | ||
| } else if (stats.conflictPairs > 0) { | ||
| statusText.textContent = "Resolve the attacking queens."; | ||
| statusText.className = "mt-2 text-xl font-black text-rose-300"; | ||
| } else if (stats.placed === size) { | ||
| statusText.textContent = "All queens placed safely."; | ||
| statusText.className = "mt-2 text-xl font-black text-emerald-300"; | ||
| } else { | ||
| statusText.textContent = "Place your queens."; | ||
| statusText.className = "mt-2 text-xl font-black text-white"; | ||
| } | ||
| hintBtn.disabled = stats.solved; | ||
| hintBtn.classList.toggle("opacity-50", stats.solved); | ||
| } | ||
|
|
||
| resetBtn.addEventListener("click", resetGame); | ||
| hintBtn.addEventListener("click", placeHint); | ||
| render(); | ||
| </script> | ||
| </body> | ||
| </html> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the board already contains six queens but they are not one per row,
Hintremains enabled because the puzzle is unsolved; clicking it finds an empty row and adds another queen here, bypassing thequeens.size < sizeguard used by manual placement. This lets the UI reach states like7/6queens and makes the puzzle impossible to complete until the player manually removes extra pieces.Useful? React with 👍 / 👎.