Color Sort Water Puzzle with 20 Levels – HTML, CSS & JS
Color Sort Water Puzzle · Games · Plain HTML, CSS & JS · Live preview
About this UI Snippet
Color Sort Water Puzzle — 20 Levels, Hidden Layers, a Hint Solver, Pour Animation and a Daily Puzzle

Color Sort, also known as water sort, is a puzzle where coloured liquid is poured between glass tubes until every tube holds a single colour. The rule is simple: a tube holds four units, a pour needs an empty tube or a tube whose top colour matches, and the whole run of that colour moves at once if it fits. This version is a complete game with 20 levels in four tiers, hidden layers, a hint solver, free undo, a Daily Puzzle with a streak and a pour animation, in plain HTML, CSS and JavaScript with no libraries, images or audio files.
Boards that are always solvable, generated backwards
A random deal can produce a board nobody can finish. Instead scramble() starts from the solved board and plays moves backwards. Each step is the exact inverse of a legal pour: take k units from the top of one tube and put them on a tube whose top colour is different (or empty). Played forwards in reverse order, those steps solve the board, so a solution is guaranteed, and the number of steps is the par. Boards that are not mixed enough, or that start already sorted, are thrown away and the next seed is tried. A seeded random generator (mulberry32) means level i is the same board for every player.
Levels as data
The LEVELS array holds, for each level, the number of colours, the number of empty tubes, how many scramble steps to apply and whether layers are hidden. The four tiers (Splash, Stream, Torrent, Flood) grow from 3 colours to 12. From level 11, only the top unit of each tube is visible and the layers underneath show a question mark until they reach the top, so you have to plan with partial information. Adding a level means adding one row.
A hint solver, not a canned answer
The Hint button runs a greedy best-first search (solve()) from the current board, with a small heuristic: how many units sit on top of a different colour, and how many tubes start with the same colour. It uses a binary heap, a canonical key that ignores the order of the tubes, and caps on the number of nodes and the time. The first move of the solution found is highlighted: the source tube in amber and the target tube in green. A hint costs one star, and each level gives three.
Pouring you can see and hear
When you pour, the source tube lifts, tilts toward the target and a stream of the right colour flows down before the units change tubes. The new layers grow into place, and the sound is a run of short bubble tones whose length matches the number of units moved. A completed tube glows green and is locked. Pours that would only waste a move, such as emptying a single-colour tube into an empty tube, are refused with a short explanation.
Stars, undo and the daily puzzle
Moves are counted against the par. Three stars for par plus about 25 percent, two stars for about 70 percent more, and one star for any solution, minus one star if you used a hint. Undo is free and removes the move from the counter, because it restores a snapshot of the tubes and the hidden layers. Winning a level unlocks the next. The Daily Puzzle builds its board from the date, so everyone plays the same one, and clearing it on consecutive days builds a streak. Progress, best moves and the streak are saved to localStorage inside try/catch.
Accessible and responsive
Every colour can show a symbol (a circle, triangle, square and so on), so the game does not depend on telling colours apart. Tubes are focusable buttons with labels such as "Tube 3: colour 2, colour 5", and the keyboard works: arrow keys move focus, Enter or Space selects and pours, the number keys pick a tube, and Z, H and R undo, hint and restart. The whole game scales to short preview areas, and on phones the layout stacks and the page scrolls.
What's included
Features
Step by step
How to Use
- 1Pick a level or the Daily PuzzleThe menu shows 20 levels in four tiers, with your stars on each. Level 1 is open at first and each solved level unlocks the next. The gold Daily Puzzle card starts the puzzle of the day, which is the same for everyone.
- 2Select a tube and pourTap a tube to lift it, then tap another tube to pour into it. You can pour onto an empty tube or onto a tube whose top colour matches, as long as there is room. The whole top run of that colour moves at once if it fits.
- 3Sort every colourYou win when every colour fills a single tube. A finished tube glows green and locks, and the progress bar above the board shows how many colours are done.
- 4Watch for hidden layersFrom level 11 only the top unit of each tube is visible. The layers below show a question mark and are revealed when they reach the top, so plan your pours with what you can see.
- 5Use undo and hintsUndo (Z) is free and takes the move back off the counter. Hint (H) shows a good next pour, highlighting the source in amber and the target in green. You get three hints per level and each one costs a star. Restart (R) starts the level again.
- 6Earn stars, turn on symbolsFinish within about 25 percent of the par for three stars. The Symbols button shows a different shape on every colour, which helps if colours are hard to tell apart. Your stars, best moves and daily streak are saved in this browser.
Real-world uses
Common Use Cases
Got questions?
Frequently Asked Questions
The board is not dealt at random. scramble() starts from the solved board and applies inverse pours: it takes k units from the top of a tube and puts them on a tube whose top is a different colour or empty. Each of those steps, played forwards, is a legal pour, so playing the steps in reverse order solves the board. The number of steps is the par shown in the HUD.
It would only waste a move, because the colour is already sorted and nothing changes about the puzzle. legal() refuses it and the game shows a short message. Full single-colour tubes are also locked, since taking units out of a finished tube is never useful.
solve() runs a greedy best-first search from the current board. The heuristic counts units that sit on a different colour and tubes that start with the same colour, a binary heap picks the most promising state, and a canonical key ignores the order of the tubes so equal boards are not searched twice. The first move of the path it finds is shown. Because a hint does part of the thinking, the star rating drops by one when you use it.
From level 11 each unit has a revealed flag. At the start only the top unit of every tube is revealed, and a unit is revealed as soon as it becomes the top of its tube. Units you pour keep being visible, and a finished tube reveals everything. Undo restores the flags together with the tubes.
Add a row to the LEVELS array with the number of colours (up to 12), the number of empty tubes, the scramble depth and whether layers are hidden. More scramble steps mix the board more and raise the par. The menu shows five levels per tier, so add levels in groups of five, or edit the TIERS array. Colours and their symbols are in the COLORS array.
Yes. All localStorage reads and writes are wrapped in try/catch, so a blocked storage only means that progress is not saved. The AudioContext is created when a level starts and every sound call is wrapped. Tubes respond to pointer events, and on narrow screens the layout stacks and the page scrolls.
Copy & paste
Source Code
<div class="cs-fit" id="cs-fit"><div class="cs-wrap" id="cs-wrap">
<div class="cs-panel" id="cs-panel">
<div class="cs-hud">
<div class="cs-hud-left">
<span class="cs-kicker" id="cs-kicker">Color Sort</span>
<span class="cs-name" id="cs-name">Pick a level</span>
</div>
<div class="cs-stat"><span class="cs-label">Moves</span><b id="cs-moves">0</b></div>
<div class="cs-stat"><span class="cs-label">Par</span><b id="cs-par">-</b></div>
<div class="cs-hud-btns">
<button class="cs-btn-sm" id="cs-symbols" type="button" aria-pressed="false" title="Show a symbol on every colour">Symbols off</button>
<button class="cs-btn-sm" id="cs-levels-btn" type="button" title="Levels (Esc)">Levels</button>
<button class="cs-btn-sm" id="cs-mute" type="button" aria-pressed="false">Sound on</button>
</div>
</div>
<div class="cs-progress"><div class="cs-progress-bar" id="cs-progress"></div></div>
<div class="cs-board"><div class="cs-tubes" id="cs-tubes"></div></div>
<p class="cs-msg" id="cs-msg">Tap a tube, then tap where to pour.</p>
<div class="cs-tools">
<button class="cs-tool" id="cs-undo" type="button"><span class="cs-tool-ico">↶</span>Undo <em>Z</em></button>
<button class="cs-tool" id="cs-hint" type="button"><span class="cs-tool-ico">💡</span>Hint <i id="cs-hint-count">3</i><em>H</em></button>
<button class="cs-tool" id="cs-restart" type="button"><span class="cs-tool-ico">↻</span>Restart <em>R</em></button>
</div>
<div class="cs-overlay show" id="cs-menu">
<p class="cs-logo">Color Sort</p>
<p class="cs-tagline">Pour the colours until every tube holds a single colour. Only matching colours can stack, and a tube holds four.</p>
<div class="cs-menu-row">
<button class="cs-daily" id="cs-daily" type="button">
<span class="cs-daily-title" id="cs-daily-title">Daily Puzzle</span>
<span class="cs-daily-sub" id="cs-daily-sub">The same puzzle for everyone</span>
</button>
<div class="cs-totals"><span><b id="cs-star-total">0</b> / 60 stars</span><span>Daily streak <b id="cs-streak">0</b></span></div>
</div>
<div class="cs-tiers" id="cs-tiers"></div>
</div>
<div class="cs-overlay" id="cs-result">
<p class="cs-result-title" id="cs-result-title">Sorted!</p>
<div class="cs-result-stars" id="cs-result-stars"><span>★</span><span>★</span><span>★</span></div>
<p class="cs-result-sub" id="cs-result-sub"></p>
<div class="cs-result-btns">
<button class="cs-btn" id="cs-next" type="button">Next level</button>
<button class="cs-btn cs-btn-ghost" id="cs-retry" type="button">Replay</button>
<button class="cs-btn cs-btn-ghost" id="cs-share" type="button">Copy result</button>
<button class="cs-btn cs-btn-ghost" id="cs-menu-back" type="button">Levels</button>
</div>
</div>
</div>
</div></div>* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: system-ui, -apple-system, 'Segoe UI', sans-serif; color: #e2e8f0;
background: radial-gradient(ellipse at top, #1d2457 0%, #0a0e24 60%, #060818 100%);
min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 12px 14px; overflow: hidden;
}
/* The game scales itself to the space it is given (fitToView): .cs-fit takes the scaled size, .cs-wrap is laid out at
its natural size and then shrunk with transform. */
.cs-fit { flex: none; }
.cs-wrap { width: 720px; transform-origin: top left; }
.cs-panel {
position: relative; display: flex; flex-direction: column; gap: 10px; padding: 16px; border-radius: 20px; overflow: hidden;
background: linear-gradient(180deg, rgba(28,34,86,0.72), rgba(10,14,36,0.94));
border: 1px solid rgba(148,163,184,0.16); box-shadow: 0 30px 80px rgba(0,0,0,0.55), 0 0 90px rgba(99,102,241,0.14);
}
.cs-hud { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.cs-hud-left { display: flex; flex-direction: column; line-height: 1.15; margin-right: auto; min-width: 0; }
.cs-kicker { font-size: 10px; font-weight: 800; letter-spacing: 0.09em; text-transform: uppercase; color: #fbbf24; }
.cs-name { font-size: 17px; font-weight: 800; color: #f8fafc; white-space: nowrap; }
.cs-stat { display: flex; flex-direction: column; align-items: center; line-height: 1.15; }
.cs-label { font-size: 9px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: #64748b; }
.cs-stat b { font-size: 17px; font-weight: 800; color: #f1f5f9; font-variant-numeric: tabular-nums; }
.cs-stat b.over { color: #fb923c; }
.cs-hud-btns { display: flex; gap: 5px; }
.cs-btn-sm { background: rgba(255,255,255,0.06); color: #cbd5e1; border: 1px solid rgba(255,255,255,0.1); border-radius: 8px; padding: 6px 10px; font: inherit; font-size: 11px; font-weight: 700; cursor: pointer; }
.cs-btn-sm:hover { background: rgba(255,255,255,0.14); }
.cs-btn-sm[aria-pressed="true"] { background: #4f46e5; border-color: #818cf8; color: #fff; }
.cs-progress { height: 4px; background: rgba(255,255,255,0.07); border-radius: 4px; overflow: hidden; }
.cs-progress-bar { height: 100%; width: 0; background: linear-gradient(90deg, #6366f1, #a78bfa, #f472b6, #fbbf24); transition: width 0.3s; }
.cs-board { padding: 26px 8px 8px; min-height: 250px; display: flex; align-items: center; justify-content: center; }
.cs-tubes { position: relative; display: flex; flex-wrap: wrap; gap: 26px 16px; justify-content: center; max-width: 660px; }
.cs-tube {
position: relative; width: 54px; height: 184px; padding: 5px 5px 7px; cursor: pointer; flex: none;
display: flex; flex-direction: column-reverse; gap: 3px; outline: none;
background: linear-gradient(90deg, rgba(255,255,255,0.1), rgba(255,255,255,0.03) 45%, rgba(255,255,255,0.08));
border: 2px solid rgba(226,232,240,0.4); border-top: none; border-radius: 0 0 28px 28px;
transition: transform 0.28s cubic-bezier(0.3, 0.7, 0.4, 1), box-shadow 0.2s, border-color 0.2s; z-index: 1;
}
.cs-tube::before { content: ''; position: absolute; left: -5px; right: -5px; top: -3px; height: 8px; border-radius: 8px; background: rgba(226,232,240,0.45); }
.cs-tube::after { content: ''; position: absolute; left: 7px; top: 12px; bottom: 26px; width: 4px; border-radius: 4px; background: linear-gradient(180deg, rgba(255,255,255,0.45), rgba(255,255,255,0)); pointer-events: none; z-index: 3; }
.cs-tube:hover { border-color: rgba(226,232,240,0.7); }
.cs-tube:focus-visible { box-shadow: 0 0 0 3px #fde68a; }
.cs-tube.sel { transform: translateY(-16px); border-color: #a5b4fc; box-shadow: 0 10px 22px rgba(129,140,248,0.4); }
.cs-tube.pouring { z-index: 20; transition: transform 0.32s ease-in-out; }
.cs-tube.done { border-color: #4ade80; box-shadow: 0 0 16px rgba(74,222,128,0.4); cursor: default; }
.cs-tube.done::before { background: #4ade80; }
.cs-tube.hintFrom { box-shadow: 0 0 0 3px #fbbf24, 0 0 18px rgba(251,191,36,0.6); }
.cs-tube.hintTo { box-shadow: 0 0 0 3px #4ade80, 0 0 18px rgba(74,222,128,0.6); }
.cs-tube.bad { animation: cs-shake 0.3s ease; }
@keyframes cs-shake { 25% { margin-left: -4px; } 75% { margin-left: 4px; } }
.cs-num { position: absolute; left: 0; right: 0; bottom: -20px; text-align: center; font-size: 10px; font-weight: 800; color: #64748b; pointer-events: none; }
.cs-slot {
flex: 0 0 39px; display: none; align-items: center; justify-content: center; border-radius: 6px; position: relative;
background: #475569; box-shadow: inset 0 3px 5px rgba(255,255,255,0.28), inset 0 -3px 5px rgba(0,0,0,0.25);
}
.cs-slot.on { display: flex; }
.cs-slot b { font-size: 17px; font-weight: 900; color: rgba(0,0,0,0.5); line-height: 1; pointer-events: none; text-shadow: 0 1px 0 rgba(255,255,255,0.2); }
.cs-slot.fresh { animation: cs-fill 0.3s ease-out; transform-origin: top; }
@keyframes cs-fill { from { transform: scaleY(0); opacity: 0.4; } to { transform: scaleY(1); opacity: 1; } }
.cs-stream { position: absolute; width: 7px; border-radius: 4px; z-index: 19; pointer-events: none; transform-origin: top; animation: cs-stream 0.22s ease-out; box-shadow: 0 0 8px rgba(255,255,255,0.3); }
@keyframes cs-stream { from { transform: scaleY(0); } to { transform: scaleY(1); } }
.cs-msg { min-height: 36px; font-size: 13px; line-height: 1.45; color: #94a3b8; text-align: center; padding: 0 4px; }
.cs-msg b { color: #5eead4; }
.cs-msg.warn { color: #fca5a5; }
.cs-tools { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.cs-tool { display: flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 0; border-radius: 12px; cursor: pointer; font: inherit; font-size: 12px; font-weight: 800; color: #cbd5e1; background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.09); transition: background 0.12s, border-color 0.12s; }
.cs-tool:hover:not(:disabled) { background: rgba(255,255,255,0.1); border-color: rgba(165,180,252,0.5); }
.cs-tool:disabled { opacity: 0.35; cursor: not-allowed; }
.cs-tool-ico { font-size: 15px; line-height: 1; }
.cs-tool em { font-style: normal; font-size: 9px; color: #64748b; }
.cs-tool i { font-style: normal; font-size: 10px; background: #6366f1; color: #fff; border-radius: 8px; padding: 0 6px; }
.cs-overlay { position: absolute; inset: 0; z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 18px; text-align: center; overflow-y: auto; background: radial-gradient(ellipse at center, rgba(10,14,36,0.965), rgba(5,7,20,0.985)); opacity: 0; pointer-events: none; transition: opacity 0.25s; }
.cs-overlay.show { opacity: 1; pointer-events: auto; }
.cs-logo { font-size: 44px; font-weight: 900; letter-spacing: -0.02em; line-height: 1.05; background: linear-gradient(90deg, #f87171, #fbbf24, #4ade80, #60a5fa, #c084fc); -webkit-background-clip: text; background-clip: text; color: transparent; }
.cs-tagline { font-size: 13px; color: #94a3b8; max-width: 440px; line-height: 1.5; }
.cs-menu-row { display: flex; align-items: center; justify-content: center; gap: 16px; flex-wrap: wrap; }
.cs-daily { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 10px 18px; border-radius: 12px; cursor: pointer; font: inherit; text-align: left; color: #fef3c7; border: 1px solid rgba(250,204,21,0.45); background: linear-gradient(135deg, rgba(250,204,21,0.18), rgba(251,146,60,0.1)); transition: transform 0.15s, box-shadow 0.15s; }
.cs-daily:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(250,204,21,0.2); }
.cs-daily-title { font-size: 14px; font-weight: 800; }
.cs-daily-sub { font-size: 11px; color: #fcd34d; }
.cs-totals { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: #94a3b8; text-align: left; }
.cs-totals b { color: #f8fafc; }
.cs-tiers { display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 640px; }
.cs-tier { display: grid; grid-template-columns: 110px 1fr; align-items: center; gap: 10px; }
.cs-tier-name { font-size: 12px; font-weight: 800; letter-spacing: 0.04em; text-transform: uppercase; color: var(--g); text-align: left; line-height: 1.2; }
.cs-tier-name small { display: block; font-size: 9px; font-weight: 600; color: #64748b; text-transform: none; letter-spacing: 0; }
.cs-tier-tiles { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.cs-tile { position: relative; height: 46px; border-radius: 10px; cursor: pointer; font: inherit; color: #e2e8f0; border: 1px solid rgba(148,163,184,0.22); background: rgba(255,255,255,0.04); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; transition: transform 0.12s, border-color 0.12s; }
.cs-tile:hover:not(:disabled) { transform: translateY(-2px); border-color: var(--g); }
.cs-tile::after { content: ''; position: absolute; left: 10px; right: 10px; bottom: 4px; height: 2px; border-radius: 2px; background: var(--g); opacity: 0.7; }
.cs-tile-num { font-size: 15px; font-weight: 800; }
.cs-tile-stars { font-size: 9px; letter-spacing: 1px; color: #334155; }
.cs-tile-stars .on { color: #facc15; }
.cs-tile:disabled { opacity: 0.32; cursor: not-allowed; }
.cs-tile.perfect { border-color: rgba(250,204,21,0.55); box-shadow: inset 0 0 12px rgba(250,204,21,0.15); }
.cs-result-title { font-size: 36px; font-weight: 900; color: #f8fafc; }
.cs-result-stars { display: flex; gap: 10px; font-size: 52px; line-height: 1; }
.cs-result-stars span { color: #1e293b; transform: scale(0.4); opacity: 0; }
.cs-result-stars span.on { color: #facc15; text-shadow: 0 0 22px rgba(250,204,21,0.75); }
.cs-overlay.show .cs-result-stars span { animation: cs-star 0.45s cubic-bezier(0.2, 1.6, 0.4, 1) forwards; }
.cs-overlay.show .cs-result-stars span:nth-child(2) { animation-delay: 0.18s; }
.cs-overlay.show .cs-result-stars span:nth-child(3) { animation-delay: 0.36s; }
@keyframes cs-star { to { transform: scale(1); opacity: 1; } }
.cs-result-sub { font-size: 14px; color: #94a3b8; line-height: 1.7; }
.cs-result-btns { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.cs-btn { padding: 10px 18px; border: none; border-radius: 10px; cursor: pointer; font: inherit; font-size: 14px; font-weight: 800; color: #fff; background: linear-gradient(135deg, #6366f1, #8b5cf6); box-shadow: 0 6px 18px rgba(99,102,241,0.35); transition: transform 0.12s; }
.cs-btn:hover { transform: translateY(-1px); }
.cs-btn-ghost { background: rgba(255,255,255,0.06); box-shadow: none; border: 1px solid rgba(255,255,255,0.12); color: #cbd5e1; }
.cs-btn[hidden] { display: none; }
.cs-confetti { position: absolute; top: -12px; width: 8px; height: 14px; border-radius: 2px; z-index: 40; pointer-events: none; animation: cs-fall linear forwards; }
@keyframes cs-fall { to { transform: translate(var(--dx), 700px) rotate(var(--rot)); opacity: 0.9; } }
.cs-narrow .cs-tier { grid-template-columns: 1fr; gap: 4px; }
.cs-narrow .cs-logo { font-size: 34px; }const CAP = 4; // units per tube
const COLORS = [
{ c: '#ef4444', g: '\u25CF' }, { c: '#3b82f6', g: '\u25B2' }, { c: '#f59e0b', g: '\u25A0' }, { c: '#22c55e', g: '\u25C6' },
{ c: '#a855f7', g: '\u2605' }, { c: '#ec4899', g: '\u2665' }, { c: '#14b8a6', g: '\u271A' }, { c: '#f97316', g: '\u2716' },
{ c: '#06b6d4', g: '\u263E' }, { c: '#a3e635', g: '\u2B22' }, { c: '#6366f1', g: '\u2600' }, { c: '#a16207', g: '\u266A' },
];
const TIERS = [
{ name: 'Splash', note: '3-5 colours', color: '#4ade80' },
{ name: 'Stream', note: '6-8 colours', color: '#60a5fa' },
{ name: 'Torrent', note: '8-10 colours, hidden layers', color: '#c084fc' },
{ name: 'Flood', note: '10-12 colours, hidden layers', color: '#fb923c' },
];
// colours, empty tubes, scramble depth (how far the board is mixed up), hidden (layers under the top are "?" until exposed)
const LEVELS = [
[3, 2, 10, false], [4, 2, 14, false], [4, 2, 18, false], [5, 2, 22, false], [5, 2, 26, false],
[6, 2, 30, false], [6, 2, 34, false], [7, 2, 38, false], [7, 2, 42, false], [8, 2, 46, false],
[8, 2, 50, true], [9, 2, 54, true], [9, 2, 58, true], [10, 2, 62, true], [10, 2, 66, true],
[10, 2, 70, true], [11, 2, 74, true], [11, 2, 78, true], [12, 2, 82, true], [12, 2, 90, true],
];
const LEVEL_COUNT = LEVELS.length;
const SAVE_KEY = 'color-sort-save-v1';
const DAY0 = Date.UTC(2026, 10, 11); // Daily Puzzle #1
const MAX_HINTS = 3;
const $ = id => document.getElementById(id);
const tubesEl = $('cs-tubes'), msgEl = $('cs-msg'), panelEl = $('cs-panel'), menuEl = $('cs-menu'), resultEl = $('cs-result');
const undoBtn = $('cs-undo'), hintBtn = $('cs-hint'), nextBtn = $('cs-next'), shareBtn = $('cs-share'), muteBtn = $('cs-mute'), symBtn = $('cs-symbols');
/* ---------- Saved progress (localStorage can throw in sandboxed iframes, so every access is guarded) ---------- */
function freshSave() {
return { stars: Array(LEVEL_COUNT).fill(0), best: Array(LEVEL_COUNT).fill(0), symbols: false, daily: { day: 0, stars: 0, moves: 0, streak: 0, lastDay: 0 } };
}
function loadSave() {
const f = freshSave();
try {
const s = JSON.parse(localStorage.getItem(SAVE_KEY) || 'null');
if (s && Array.isArray(s.stars)) {
f.stars = f.stars.map((_, i) => s.stars[i] || 0);
f.best = f.best.map((_, i) => (s.best && s.best[i]) || 0);
f.symbols = !!s.symbols;
Object.assign(f.daily, s.daily || {});
}
} catch (e) { /* storage blocked */ }
return f;
}
function persist() { try { localStorage.setItem(SAVE_KEY, JSON.stringify(save)); } catch (e) { /* storage blocked */ } }
let save = loadSave();
/* ---------- Seeded random: the same seed always gives the same board ---------- */
function mulberry32(a) {
return function () {
a |= 0; a = a + 0x6D2B79F5 | 0;
let t = Math.imul(a ^ a >>> 15, 1 | a);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
function todayNumber() {
const d = new Date();
return Math.max(1, Math.floor((Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) - DAY0) / 86400000) + 1);
}
/* ---------- Sound: a tiny Web Audio synth, no files ---------- */
let audioCtx = null, muted = false;
function getAudio() {
if (!audioCtx) { try { audioCtx = new (window.AudioContext || window.webkitAudioContext)(); } catch (e) { audioCtx = null; } }
if (audioCtx && audioCtx.state === 'suspended') audioCtx.resume();
return audioCtx;
}
function tone(freq, dur, type, vol, slideTo, delay) {
if (muted || !audioCtx) return;
try {
const t0 = audioCtx.currentTime + (delay || 0);
const osc = audioCtx.createOscillator(), gain = audioCtx.createGain();
osc.type = type;
osc.frequency.setValueAtTime(freq, t0);
if (slideTo) osc.frequency.exponentialRampToValueAtTime(slideTo, t0 + dur);
gain.gain.setValueAtTime(vol, t0);
gain.gain.exponentialRampToValueAtTime(0.0001, t0 + dur);
osc.connect(gain); gain.connect(audioCtx.destination);
osc.start(t0); osc.stop(t0 + dur);
} catch (e) { /* audio is optional */ }
}
const sfx = {
pick() { tone(520, 0.06, 'sine', 0.05); },
bad() { tone(160, 0.18, 'sawtooth', 0.04, 100); },
pour(n) { for (let i = 0; i < n * 2; i++) tone(260 + i * 55 + Math.random() * 40, 0.09, 'sine', 0.05, 380 + i * 60, i * 0.075); },
done() { tone(659, 0.12, 'triangle', 0.06); tone(988, 0.2, 'triangle', 0.06, null, 0.1); },
win() { [523, 659, 784, 1046].forEach((f, i) => tone(f, 0.22, 'triangle', 0.07, null, i * 0.12)); },
};
muteBtn.addEventListener('click', () => {
muted = !muted;
muteBtn.textContent = muted ? 'Sound off' : 'Sound on';
muteBtn.setAttribute('aria-pressed', String(muted));
});
/* ---------- Rules ---------- */
const topOf = t => t[t.length - 1];
function topRun(t) { let n = 0; for (let i = t.length - 1; i >= 0 && t[i] === topOf(t); i--) n++; return n; }
const isMono = t => t.length > 0 && t.every(c => c === t[0]);
const isDone = t => t.length === CAP && isMono(t);
// a pour must be legal and useful: onto an empty tube or a matching top, never out of a finished tube,
// and never a single-colour tube into an empty one (that only wastes a move)
function legal(ts, a, b) {
if (a === b) return false;
const s = ts[a], d = ts[b];
if (!s.length || isDone(s) || d.length >= CAP) return false;
if (d.length === 0) return !isMono(s);
return topOf(d) === topOf(s);
}
function hasMoves(ts) { for (let a = 0; a < ts.length; a++) for (let b = 0; b < ts.length; b++) if (legal(ts, a, b)) return true; return false; }
function applyPour(ts, a, b) { // moves the whole top run that fits, returns how many units moved
const n = Math.min(topRun(ts[a]), CAP - ts[b].length), c = topOf(ts[a]);
for (let i = 0; i < n; i++) { ts[a].pop(); ts[b].push(c); }
return n;
}
const allSorted = (ts, colors) => ts.filter(isDone).length === colors && ts.every(t => t.length === 0 || isDone(t));
/* ---------- Level generator: scramble BACKWARDS from the solved board, so a solution always exists ---------- */
// Every step is the exact inverse of a legal pour: take k units from the top of one tube and put them on a tube whose top is a
// different colour (or empty). Played forwards the steps, in reverse order, solve the board, and their number is the par.
function scramble(colors, empties, steps, R) {
const n = colors + empties;
const ts = Array.from({ length: n }, (_, i) => (i < colors ? Array(CAP).fill(i) : []));
let done = 0, guard = 0;
while (done < steps && guard++ < steps * 40) {
const rooms = []; // tubes that can receive units
ts.forEach((t, i) => { if (t.length < CAP) rooms.push(i); });
const s = rooms[Math.floor(R() * rooms.length)], S = ts[s];
const donors = []; // tubes whose top colour differs from the receiver's top
ts.forEach((t, i) => { if (i !== s && t.length && (!S.length || topOf(t) !== topOf(S))) donors.push(i); });
if (!donors.length) continue;
const d = donors[Math.floor(R() * donors.length)], D = ts[d];
const run = topRun(D);
const ks = [];
for (let k = 1; k <= Math.min(run, CAP - S.length); k++) if (k < run || k === D.length) ks.push(k);
if (!ks.length) continue;
const k = ks[Math.floor(R() * ks.length)];
for (let i = 0; i < k; i++) S.push(D.pop());
done++;
}
const mixed = ts.filter(t => t.length && !isMono(t)).length;
return { tubes: ts, par: done, ok: done === steps && !allSorted(ts, colors) && mixed >= Math.ceil(colors * 0.75) };
// (par here is the length of the backwards scramble: an upper bound, loadPuzzle() tightens it with the solver)
}
function generate(colors, empties, steps, seed) {
let last = null;
for (let attempt = 0; attempt < 40; attempt++) {
last = scramble(colors, empties, steps, mulberry32(seed + attempt * 7919));
if (last.ok) return last;
}
return last;
}
function levelSpec(i) { const [colors, empties, steps, hidden] = LEVELS[i]; return { name: 'Level ' + (i + 1), colors, empties, steps, hidden, seed: 500 + i * 7919 }; }
function dailySpec(n) { return { name: 'Daily Puzzle #' + n, colors: 8 + (n % 3), empties: 2, steps: 56 + (n % 4) * 6, hidden: n % 2 === 0, seed: n * 104729 + 31 }; }
/* ---------- Hint solver: greedy best-first search over tube states ---------- */
function heuristic(ts) {
let h = 0; const bottoms = {};
ts.forEach(t => { if (t.length) { bottoms[t[0]] = (bottoms[t[0]] || 0) + 1; h += t.filter(c => c !== t[0]).length; } });
Object.keys(bottoms).forEach(k => { h += bottoms[k] - 1; });
return h;
}
function stateKey(ts) { return ts.map(t => t.map(c => String.fromCharCode(97 + c)).join('')).sort().join('|'); }
function solve(start, colors, capNodes, capMs) { // returns a list of {from, to} or null
const t0 = Date.now(), heap = [], seen = new Set([stateKey(start)]);
const push = n => { heap.push(n); let i = heap.length - 1; while (i > 0) { const p = (i - 1) >> 1; if (heap[p].f <= heap[i].f) break; [heap[p], heap[i]] = [heap[i], heap[p]]; i = p; } };
const pop = () => { const top = heap[0], last = heap.pop(); if (heap.length) { heap[0] = last; let i = 0; for (;;) { let m = i; const l = 2 * i + 1, r = l + 1; if (l < heap.length && heap[l].f < heap[m].f) m = l; if (r < heap.length && heap[r].f < heap[m].f) m = r; if (m === i) break; [heap[m], heap[i]] = [heap[i], heap[m]]; i = m; } } return top; };
push({ ts: start, g: 0, f: heuristic(start) * 3, parent: null, move: null });
let nodes = 0;
while (heap.length && nodes < capNodes && Date.now() - t0 < capMs) {
const cur = pop(); nodes++;
if (allSorted(cur.ts, colors)) { const path = []; for (let n = cur; n.parent; n = n.parent) path.push(n.move); return path.reverse(); }
for (let a = 0; a < cur.ts.length; a++) for (let b = 0; b < cur.ts.length; b++) {
if (!legal(cur.ts, a, b)) continue;
const nx = cur.ts.map(t => t.slice());
applyPour(nx, a, b);
const k = stateKey(nx);
if (seen.has(k)) continue;
seen.add(k);
push({ ts: nx, g: cur.g + 1, f: heuristic(nx) * 3 + cur.g + 1, parent: cur, move: { from: a, to: b } });
}
}
return null;
}
/* ---------- Game state ---------- */
let spec = null, isDaily = false, dailyNum = 0, levelIndex = 0;
let tubes = [], revealed = [], par = 0, moves = 0, hintsLeft = MAX_HINTS, hintsUsed = 0;
let history = [], sel = -1, busy = false, state = 'menu', runId = 0;
const tubeEls = [], slotEls = [], numEls = [];
function buildBoard() {
tubesEl.innerHTML = '';
tubeEls.length = 0; slotEls.length = 0;
tubes.forEach((_, i) => {
const el = document.createElement('div');
el.className = 'cs-tube'; el.tabIndex = 0; el.setAttribute('role', 'button');
const slots = [];
for (let k = 0; k < CAP; k++) { const s = document.createElement('i'); s.className = 'cs-slot'; s.innerHTML = '<b></b>'; el.appendChild(s); slots.push(s); }
const num = document.createElement('span'); num.className = 'cs-num'; num.textContent = String(i + 1); el.appendChild(num);
el.addEventListener('pointerdown', e => { if (e.button === 0) { onTube(i); } });
el.addEventListener('keydown', e => {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); onTube(i); }
else if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') { e.preventDefault(); const j = (i + (e.key === 'ArrowRight' ? 1 : -1) + tubes.length) % tubes.length; tubeEls[j].focus(); }
});
tubesEl.appendChild(el); tubeEls.push(el); slotEls.push(slots);
});
}
function snapshot() { history.push({ tubes: tubes.map(t => t.slice()), revealed: revealed.map(t => t.slice()), moves }); }
function exposeTops() { tubes.forEach((t, i) => { if (t.length) revealed[i][t.length - 1] = true; if (isDone(t)) revealed[i] = revealed[i].map(() => true); }); }
function loadPuzzle(s) {
spec = s;
const g = generate(s.colors, s.empties, s.steps, s.seed);
tubes = g.tubes; par = g.par;
const found = solve(tubes.map(t => t.slice()), s.colors, 12000, 600); // a real solution is usually much shorter than the scramble
if (found && found.length) par = Math.min(par, found.length);
revealed = tubes.map(t => t.map(() => !s.hidden));
exposeTops();
moves = 0; hintsLeft = MAX_HINTS; hintsUsed = 0; history = []; sel = -1; busy = false; runId++;
buildBoard();
setMsg(s.hidden ? 'Hidden layers: a "?" is revealed when it reaches the top of its tube.' : 'Tap a tube, then tap where to pour.');
render(true);
}
function startLevel(i) { getAudio(); isDaily = false; levelIndex = i; loadPuzzle(levelSpec(i)); beginPlay(); $('cs-kicker').textContent = 'Level ' + (i + 1) + ' - ' + TIERS[Math.floor(i / 5)].name; $('cs-name').textContent = spec.colors + ' colours' + (spec.hidden ? ', hidden layers' : ''); }
function startDaily() { getAudio(); isDaily = true; dailyNum = todayNumber(); loadPuzzle(dailySpec(dailyNum)); beginPlay(); $('cs-kicker').textContent = 'Daily'; $('cs-name').textContent = spec.name; }
function beginPlay() {
if (document.activeElement && document.activeElement.blur) document.activeElement.blur();
state = 'play';
menuEl.classList.remove('show'); resultEl.classList.remove('show');
render(false);
}
function restart() { if (isDaily) startDaily(); else startLevel(levelIndex); }
function showMenu() {
runId++; busy = false; state = 'menu';
resultEl.classList.remove('show'); menuEl.classList.add('show');
renderMenu();
$('cs-kicker').textContent = 'Color Sort'; $('cs-name').textContent = 'Pick a level';
}
/* ---------- Rendering ---------- */
function setMsg(html, warn) { msgEl.innerHTML = html; msgEl.classList.toggle('warn', !!warn); }
function render(fresh) {
tubes.forEach((t, i) => {
const el = tubeEls[i];
el.classList.toggle('sel', i === sel);
el.classList.toggle('done', isDone(t));
const names = t.map((c, k) => (revealed[i][k] ? 'colour ' + (c + 1) : 'hidden')).join(', ');
el.setAttribute('aria-label', 'Tube ' + (i + 1) + ': ' + (names || 'empty'));
for (let k = 0; k < CAP; k++) {
const s = slotEls[i][k];
if (k < t.length) {
const vis = revealed[i][k];
s.classList.add('on');
s.style.background = vis ? COLORS[t[k]].c : '#475569';
s.firstChild.textContent = vis ? (save.symbols ? COLORS[t[k]].g : '') : '?';
} else s.classList.remove('on');
}
});
$('cs-moves').textContent = moves;
$('cs-par').textContent = par || '-';
$('cs-moves').classList.toggle('over', par > 0 && moves > Math.ceil(par * 1.25) + 1);
$('cs-progress').style.width = (spec ? tubes.filter(isDone).length / spec.colors * 100 : 0) + '%';
$('cs-hint-count').textContent = hintsLeft;
undoBtn.disabled = state !== 'play' || busy || history.length === 0;
hintBtn.disabled = state !== 'play' || busy || hintsLeft <= 0;
$('cs-restart').disabled = state !== 'play' || busy;
symBtn.textContent = save.symbols ? 'Symbols on' : 'Symbols off';
symBtn.setAttribute('aria-pressed', String(save.symbols));
}
function renderMenu() {
const today = todayNumber(), d = save.daily;
if (d.lastDay && d.lastDay < today - 1) d.streak = 0; // a missed day resets the streak
const wrap = $('cs-tiers');
wrap.innerHTML = '';
TIERS.forEach((t, ti) => {
const row = document.createElement('div');
row.className = 'cs-tier'; row.style.setProperty('--g', t.color);
row.innerHTML = '<div class="cs-tier-name">' + t.name + '<small>' + t.note + '</small></div>';
const tiles = document.createElement('div');
tiles.className = 'cs-tier-tiles';
for (let k = 0; k < 5; k++) {
const i = ti * 5 + k, unlocked = i === 0 || save.stars[i - 1] > 0; // solving a level unlocks the next one
const b = document.createElement('button');
b.type = 'button'; b.className = 'cs-tile' + (save.stars[i] === 3 ? ' perfect' : '');
b.disabled = !unlocked; b.title = unlocked ? 'Level ' + (i + 1) : 'Solve level ' + i + ' to unlock';
b.innerHTML = '<span class="cs-tile-num">' + (i + 1) + '</span><span class="cs-tile-stars">' + [0, 1, 2].map(n => '<span class="' + (n < save.stars[i] ? 'on' : '') + '">\u2605</span>').join('') + '</span>';
b.addEventListener('click', () => startLevel(i));
tiles.appendChild(b);
}
row.appendChild(tiles); wrap.appendChild(row);
});
$('cs-star-total').textContent = save.stars.reduce((s, v) => s + v, 0);
$('cs-streak').textContent = d.streak;
$('cs-daily-title').textContent = 'Daily Puzzle #' + today;
$('cs-daily-sub').textContent = d.day === today && d.stars > 0 ? 'Done today: ' + '\u2605'.repeat(d.stars) + '\u2606'.repeat(3 - d.stars) + ' in ' + d.moves + ' moves' : 'The same puzzle for everyone. Keep your streak!';
}
/* ---------- Playing ---------- */
function onTube(i) {
if (state !== 'play' || busy) return;
const t = tubes[i];
if (sel === -1) {
if (!t.length) return;
if (isDone(t)) { setMsg('That tube is already complete.'); return; }
sel = i; sfx.pick(); render(); return;
}
if (sel === i) { sel = -1; render(); return; }
if (legal(tubes, sel, i)) { pourAnimated(sel, i); return; }
const wasSel = sel;
sel = -1;
tubeEls[i].classList.remove('bad'); void tubeEls[i].offsetWidth; tubeEls[i].classList.add('bad');
sfx.bad();
setMsg(!tubes[wasSel].length ? '' : tubes[i].length >= CAP ? 'That tube is full.' : tubes[i].length && topOf(tubes[i]) !== topOf(tubes[wasSel]) ? 'Only the same colour can go on top.' : 'No need to move a sorted tube into an empty one.', true);
if (t.length && !isDone(t)) { sel = i; sfx.pick(); }
render();
}
function pourAnimated(a, b) {
busy = true;
const id = runId, A = tubeEls[a], B = tubeEls[b];
const n = Math.min(topRun(tubes[a]), CAP - tubes[b].length), color = COLORS[topOf(tubes[a])].c, destLen = tubes[b].length;
sel = -1; A.classList.remove('sel'); render();
const side = B.offsetLeft >= A.offsetLeft ? 1 : -1;
const dx = (B.offsetLeft + B.offsetWidth / 2 - side * 12) - (A.offsetLeft + A.offsetWidth / 2);
const dy = (B.offsetTop - 30) - A.offsetTop;
A.classList.add('pouring');
A.style.transformOrigin = '50% 0';
A.style.transform = 'translate(' + dx + 'px,' + dy + 'px) rotate(' + (side * 72) + 'deg)';
sfx.pour(n);
setTimeout(() => { // the stream flows, then the units change tubes
if (id !== runId) return;
const sY = B.offsetTop - 26, bottom = B.offsetTop + B.offsetHeight - 8, surface = bottom - destLen * 42;
const st = document.createElement('div');
st.className = 'cs-stream'; st.style.background = color;
st.style.left = (B.offsetLeft + B.offsetWidth / 2 - side * 12 - 3) + 'px'; st.style.top = sY + 'px'; st.style.height = Math.max(24, surface - sY) + 'px';
tubesEl.appendChild(st);
snapshot();
applyPour(tubes, a, b);
for (let i = 0; i < n; i++) revealed[b][destLen + i] = true;
revealed[a].length = tubes[a].length;
moves++;
exposeTops();
render();
for (let i = destLen; i < destLen + n; i++) slotEls[b][i].classList.add('fresh');
setTimeout(() => st.remove(), 340);
}, 330);
setTimeout(() => { if (id === runId) { A.style.transform = ''; } }, 700);
setTimeout(() => {
if (id !== runId) return;
A.classList.remove('pouring'); busy = false;
if (isDone(tubes[b])) { sfx.done(); setMsg('<b>Tube ' + (b + 1) + ' is sorted!</b>'); } else setMsg('');
render();
if (allSorted(tubes, spec.colors)) finish();
else if (!hasMoves(tubes)) setMsg('No moves left. Undo a few moves or restart.', true);
}, 980);
}
function undo() {
if (state !== 'play' || busy || !history.length) return;
const h = history.pop();
tubes = h.tubes; revealed = h.revealed; moves = h.moves; sel = -1;
setMsg('Undone. Undo is free and never costs a move.');
render();
}
function hint() {
if (state !== 'play' || busy || hintsLeft <= 0) return;
setMsg('Looking for a good move...');
const id = runId;
setTimeout(() => {
if (id !== runId) return;
const path = solve(tubes.map(t => t.slice()), spec.colors, 9000, 700);
if (!path || !path.length) { setMsg('No hint found from here. Try undoing a few moves.', true); return; }
hintsLeft--; hintsUsed++;
const m = path[0];
tubeEls[m.from].classList.add('hintFrom'); tubeEls[m.to].classList.add('hintTo');
setTimeout(() => { tubeEls[m.from].classList.remove('hintFrom'); tubeEls[m.to].classList.remove('hintTo'); }, 2600);
setMsg('<b>Hint:</b> pour tube ' + (m.from + 1) + ' into tube ' + (m.to + 1) + '.');
sfx.pick();
render();
}, 30);
}
function finish() {
if (state !== 'play') return;
state = 'over';
const s3 = Math.ceil(par * 1.25) + 1, s2 = Math.ceil(par * 1.7) + 3;
let stars = moves <= s3 ? 3 : moves <= s2 ? 2 : 1;
if (hintsUsed > 0) stars = Math.max(1, stars - 1);
let best = 0;
if (isDaily) {
const d = save.daily;
if (d.day !== dailyNum) { d.day = dailyNum; d.stars = 0; d.moves = 0; }
if (stars > d.stars || (stars === d.stars && (!d.moves || moves < d.moves))) { d.stars = stars; d.moves = moves; }
if (d.lastDay !== dailyNum) { d.streak = d.lastDay === dailyNum - 1 ? d.streak + 1 : 1; d.lastDay = dailyNum; }
} else {
save.stars[levelIndex] = Math.max(save.stars[levelIndex], stars);
save.best[levelIndex] = save.best[levelIndex] ? Math.min(save.best[levelIndex], moves) : moves;
best = save.best[levelIndex];
}
persist(); render();
sfx.win(); confetti();
$('cs-result-title').textContent = stars === 3 ? 'Perfectly sorted!' : 'Sorted!';
[...$('cs-result-stars').children].forEach((el, i) => el.classList.toggle('on', i < stars));
$('cs-result-sub').innerHTML = moves + ' moves (par ' + par + ')' + (hintsUsed ? ', ' + hintsUsed + ' hint' + (hintsUsed > 1 ? 's' : '') : '') +
'<br>3 stars at ' + s3 + ' moves or fewer, 2 stars at ' + s2 + (hintsUsed ? ', and a hint costs a star' : '') + (isDaily ? '<br>Streak ' + save.daily.streak : (best ? '<br>Best: ' + best + ' moves' : ''));
nextBtn.hidden = isDaily || levelIndex >= LEVEL_COUNT - 1;
shareBtn.hidden = !isDaily; shareBtn.textContent = 'Copy result';
const id = runId;
setTimeout(() => { if (id === runId && state === 'over') resultEl.classList.add('show'); }, 900);
}
function confetti() {
const colors = ['#f87171', '#fbbf24', '#4ade80', '#60a5fa', '#c084fc', '#f472b6'];
for (let i = 0; i < 46; i++) {
const p = document.createElement('div');
p.className = 'cs-confetti'; p.style.left = Math.random() * 100 + '%'; p.style.background = colors[i % colors.length];
p.style.setProperty('--dx', (Math.random() * 160 - 80) + 'px'); p.style.setProperty('--rot', (Math.random() * 720 - 360) + 'deg');
p.style.animationDuration = (1.6 + Math.random() * 1.4) + 's'; p.style.animationDelay = (Math.random() * 0.5) + 's';
panelEl.appendChild(p); setTimeout(() => p.remove(), 3800);
}
}
/* ---------- Controls ---------- */
undoBtn.addEventListener('click', () => { undo(); undoBtn.blur(); });
hintBtn.addEventListener('click', () => { hint(); hintBtn.blur(); });
$('cs-restart').addEventListener('click', restart);
symBtn.addEventListener('click', () => { save.symbols = !save.symbols; persist(); render(); symBtn.blur(); });
$('cs-levels-btn').addEventListener('click', showMenu);
$('cs-daily').addEventListener('click', startDaily);
$('cs-retry').addEventListener('click', restart);
$('cs-menu-back').addEventListener('click', showMenu);
nextBtn.addEventListener('click', () => startLevel(levelIndex + 1));
shareBtn.addEventListener('click', () => {
const d = save.daily;
const text = 'Color Sort Daily #' + dailyNum + ' ' + '\u2605'.repeat(d.stars) + '\u2606'.repeat(3 - d.stars) + ' ' + d.moves + ' moves, streak ' + d.streak;
const done = () => { shareBtn.textContent = 'Copied!'; };
try { navigator.clipboard.writeText(text).then(done, () => { shareBtn.textContent = text; }); } catch (e) { shareBtn.textContent = text; }
});
document.addEventListener('keydown', e => {
if (e.ctrlKey || e.metaKey || e.altKey) return;
const k = e.key;
if (state === 'play') {
if ((k === 'z' || k === 'Z') && !e.repeat) undo();
else if ((k === 'h' || k === 'H') && !e.repeat) hint();
else if ((k === 'r' || k === 'R') && !e.repeat) restart();
else if (/^[1-9]$/.test(k) && Number(k) <= tubes.length) onTube(Number(k) - 1);
}
if (k === 'Escape' && state !== 'menu') showMenu();
});
/* ---------- Fit the game into the space available ---------- */
const fitEl = $('cs-fit'), wrapEl = $('cs-wrap');
function fitToView() {
const availW = Math.max(240, window.innerWidth - 28), availH = Math.max(240, window.innerHeight - 24);
const narrow = availW < 640;
wrapEl.classList.toggle('cs-narrow', narrow);
document.body.style.overflow = narrow ? 'auto' : 'hidden';
document.body.style.alignItems = narrow ? 'flex-start' : 'center';
if (narrow) { wrapEl.style.transform = 'none'; wrapEl.style.width = Math.min(520, availW) + 'px'; fitEl.style.width = wrapEl.style.width; fitEl.style.height = wrapEl.offsetHeight + 'px'; return; }
wrapEl.style.transform = 'none'; wrapEl.style.width = '720px';
const natH = wrapEl.offsetHeight || 1, k = Math.max(0.4, Math.min(1, availW / 720, availH / natH));
wrapEl.style.transform = k < 1 ? 'scale(' + k + ')' : 'none';
fitEl.style.width = Math.round(720 * k) + 'px'; fitEl.style.height = Math.round(natH * k) + 'px';
}
window.addEventListener('resize', fitToView);
if (typeof ResizeObserver !== 'undefined') new ResizeObserver(fitToView).observe(wrapEl);
loadPuzzle(levelSpec(0));
showMenu();
fitToView();
/* wdp-share:start */
(function () {
var TITLE = 'Color Sort Water Puzzle', URL = 'https://webdevpuneet.com/ui-snippets/color-sort-puzzle/';
if (window.__wdpShare) return;
window.__wdpShare = true;
var blob = null, file = null, lastInfo = null;
/* ---- UI ---- */
var st = document.createElement('style');
st.textContent =
'.wdps-in,.wdps-btn,.wdps-box{touch-action:manipulation;-webkit-tap-highlight-color:transparent}.wdps-in{display:block;margin:12px auto 0;border:0;border-radius:12px;padding:11px 22px;font:800 15px/1 system-ui,sans-serif;background:#16a34a;color:#fff;cursor:pointer;box-shadow:0 3px 0 #11793a;pointer-events:auto;position:relative;z-index:5}' +
'.wdps-in:hover{background:#15803d}.wdps-in:active{transform:translateY(2px);box-shadow:0 1px 0 #11793a}' +
'.wdps-btn{position:fixed;right:12px;bottom:12px;z-index:2147483000;border:0;border-radius:99px;padding:9px 16px;font:700 13px/1 system-ui,sans-serif;background:#16a34a;color:#fff;cursor:pointer;box-shadow:0 3px 12px rgba(0,0,0,.3)}' +
'.wdps-btn.wdps-hot{animation:wdps-p 1.4s ease-in-out infinite}' +
'@keyframes wdps-p{0%,100%{box-shadow:0 0 0 0 rgba(22,163,74,.6),0 3px 12px rgba(0,0,0,.3)}50%{box-shadow:0 0 0 9px rgba(22,163,74,0),0 3px 12px rgba(0,0,0,.3)}}' +
'.wdps-btn:active{transform:translateY(1px)}' +
'.wdps-in[hidden],.wdps-btn[hidden],.wdps-box[hidden],.wdps-box [hidden]{display:none}' +
'.wdps-box{position:fixed;left:50%;bottom:12px;transform:translateX(-50%);z-index:2147483001;width:max-content;max-width:calc(100vw - 24px);box-sizing:border-box;background:#fff;color:#64748b;border:1px solid #d5e2ee;border-radius:12px;padding:8px 12px;font:700 12px/1.4 system-ui,sans-serif;box-shadow:0 8px 26px rgba(15,23,42,.25);display:flex;flex-wrap:wrap;gap:8px;align-items:center}' +
'.wdps-box button,.wdps-box a{border:1px solid #d5e2ee;background:#f8fafc;color:#1e293b;border-radius:99px;padding:5px 12px;font:700 12px system-ui,sans-serif;text-decoration:none;cursor:pointer}' +
'.wdps-box .wdps-x{border:0;background:none;font-size:19px;line-height:1;padding:0 4px;margin-left:auto;color:#475569}' +
'@media (max-width:700px){.wdps-box{width:calc(100vw - 24px);justify-content:center;padding:10px 12px}.wdps-box>span{width:100%;text-align:center}.wdps-box button,.wdps-box a{padding:9px 14px;font-size:13px}.wdps-box .wdps-x{position:absolute;top:4px;right:4px}.wdps-in{width:100%;max-width:320px}}';
document.head.appendChild(st);
var pill = document.createElement('button');
pill.type = 'button'; pill.className = 'wdps-btn'; pill.textContent = 'Share result';
var inl = document.createElement('button');
inl.type = 'button'; inl.className = 'wdps-in'; inl.textContent = 'Share result'; inl.hidden = true;
var box = document.createElement('div');
box.className = 'wdps-box'; box.hidden = true;
box.setAttribute('role', 'dialog'); box.setAttribute('aria-label', 'Share your result');
box.innerHTML =
'<span data-a="msg">Share your result:</span>' +
'<button type="button" data-a="copy">Copy image</button>' +
'<a data-a="dl" download="result.png">Download image</a>' +
'<button type="button" data-a="more" hidden>More…</button>' +
'<a data-a="wa" target="_blank" rel="noopener">WhatsApp</a>' +
'<a data-a="x" target="_blank" rel="noopener">X</a>' +
'<a data-a="fb" target="_blank" rel="noopener">Facebook</a>' +
'<button type="button" class="wdps-x" data-a="close" aria-label="Close share options">×</button>';
document.body.appendChild(box);
document.body.appendChild(pill);
function q(a) { return box.querySelector('[data-a="' + a + '"]'); }
function ours(el) { return el === inl || el === pill || !!el.closest('.wdps-box'); }
/* ---- reading the result from the screen ---- */
function shown(el) {
if (!el || !el.getClientRects().length) return false;
for (var n = el; n && n !== document.body; n = n.parentElement) {
var cs = getComputedStyle(n);
if (cs.display === 'none' || cs.visibility === 'hidden' || parseFloat(cs.opacity) < 0.1) return false;
if (n.hidden || n.getAttribute('aria-hidden') === 'true') return false;
}
return true;
}
function txt(el) { return (el.textContent || '').replace(/\s+/g, ' ').trim(); }
function sig(el) { return ((el.id || '') + ' ' + (typeof el.className === 'string' ? el.className : '')).toLowerCase(); }
var OUTCOME = /(you win|you won|win!|wins|victory|game over|time.?s up|cleared|complete|solved|defeat|you lose|you lost|lost|draw|crash|failed|success|congrat|score|result|finished|beat|all matched|well done|perfect|new best)/i;
var HEADSIG = /(title|headline|msg|message|banner|result|verdict|status)/;
var WIN = /(you win|you won|win!|wins|victory|cleared|complete|solved|success|congrat|beat|matched|perfect|new best|record)/i;
var LOSS = /(game over|time.?s up|defeat|you lose|you lost|lost|crash|failed|out of)/i;
var BOX = /(overlay|modal|banner|result|over|end|dialog|screen|popup|panel|card|finish|win)/;
function readResult() {
var head = '', sub = '', root = null, headEl = null;
var all = document.body.querySelectorAll('h1,h2,h3,h4,p,strong,div,span');
for (var i = 0; i < all.length && !head; i++) {
var el = all[i];
if (ours(el) || el.children.length > 2) continue;
if (!HEADSIG.test(sig(el))) continue;
/* a banner that holds its own title: use the title inside it instead */
var inner = false;
for (var c = 0; c < el.children.length; c++) if (HEADSIG.test(sig(el.children[c]))) inner = true;
if (inner) continue;
var t = txt(el);
if (t.length < 3 || t.length > 70 || !OUTCOME.test(t) || !shown(el)) continue;
head = t; headEl = el; root = el.parentElement;
}
if (root) {
var kids = root.querySelectorAll('p,span,div');
for (var j = 0; j < kids.length && !sub; j++) {
var k = kids[j];
if (ours(k) || k.children.length > 2 || !/(sub|text|summary|note|desc|detail)/.test(sig(k))) continue;
var tt = txt(k);
if (tt && tt !== head && tt.length >= 5 && tt.length <= 160 && shown(k)) sub = tt;
}
}
/* the result screen the button goes into: the nearest overlay / banner / result box around the headline */
var host = null;
for (var n = headEl, d = 0; n && n !== document.body && d < 4; n = n.parentElement, d++) {
if (d > 0 && BOX.test(sig(n)) && n.tagName !== 'BUTTON') { host = n; break; }
}
return { head: head, sub: sub, host: host };
}
var STATRE = /(score|moves|time|wpm|best|level|streak|accuracy|combo|coins|distance|height|points|stars|lives|wins|attempt|guess)/;
function readStats() {
var out = [], seen = {};
var all = document.body.querySelectorAll('span,strong,b,div,p,output');
for (var i = 0; i < all.length && out.length < 4; i++) {
var el = all[i];
if (ours(el) || el.children.length > 1) continue;
if (!STATRE.test(sig(el))) continue;
var t = txt(el);
if (!t || t.length > 28 || !/[0-9]/.test(t) || !shown(el)) continue;
if (!/[a-zA-Z]/.test(t) && el.parentElement) {
var parts = [], ch = el.parentElement.childNodes;
for (var p = 0; p < ch.length; p++) { var pv = (ch[p].textContent || '').replace(/\s+/g, ' ').trim(); if (pv) parts.push(pv); }
var pt = parts.join(' ');
if (pt.length <= 30 && /[a-zA-Z]/.test(pt)) t = pt;
}
var key = t.toLowerCase();
if (seen[key]) continue;
seen[key] = 1; out.push(t);
}
return out;
}
function bestCanvas() {
var cs = document.querySelectorAll('canvas'), best = null, area = 0;
for (var i = 0; i < cs.length; i++) {
var r = cs[i].getBoundingClientRect(), a = r.width * r.height;
if (a > area && a > 8000 && shown(cs[i])) { area = a; best = cs[i]; }
}
return best;
}
/* ---- show the button when a result is on screen ---- */
var lastHost = null, timer = 0;
function sync() {
timer = 0;
var r = readResult();
if (r.head && r.host) {
if (inl.parentElement !== r.host) r.host.appendChild(inl);
if (inl.hidden) inl.hidden = false;
if (!pill.hidden) pill.hidden = true;
} else {
if (!inl.hidden) inl.hidden = true;
if (pill.hidden) pill.hidden = false;
pill.classList.toggle('wdps-hot', !!r.head);
}
if (lastHost && !r.head && !box.hidden) box.hidden = true;
lastHost = r.head ? (r.host || document.body) : null;
}
function later() { if (!timer) timer = setTimeout(sync, 200); }
try {
new MutationObserver(function (list) {
for (var i = 0; i < list.length; i++) {
var t = list[i].target;
if (t.nodeType !== 1) t = t.parentElement;
if (t && !ours(t)) { later(); return; }
}
}).observe(document.body, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: ['class', 'style', 'hidden'] });
} catch (e) {}
setInterval(later, 1500);
later();
/* ---- the card (1080 x 1080, drawn in memory, light like Pocket Artillery's) ---- */
var outline = 0;
function put(g, t, x, y) { if (outline) { g.lineJoin = 'round'; g.lineWidth = outline; g.strokeStyle = 'rgba(8,12,28,.85)'; g.strokeText(t, x, y); } g.fillText(t, x, y); }
function wrap(g, text, x, y, maxW, lh, maxLines) {
var words = text.split(' '), line = '', n = 0;
for (var i = 0; i < words.length; i++) {
var tst = line ? line + ' ' + words[i] : words[i];
if (g.measureText(tst).width > maxW && line) {
put(g, line, x, y); y += lh; n++; line = words[i];
if (n >= maxLines - 1) { line = words.slice(i).join(' '); break; }
} else line = tst;
}
if (line) {
while (line.length > 3 && g.measureText(line).width > maxW) line = line.slice(0, -2);
put(g, line, x, y); y += lh;
}
return y;
}
/* Biggest-screenshot card: the game frame fills the whole image, the text sits on top of it as an overlay. */
function bgOf(el) {
for (var n = el; n; n = n.parentElement) {
var c = getComputedStyle(n).backgroundColor;
if (c && c !== 'transparent' && c.slice(-4) !== ', 0)') return c;
}
return '#0f172a';
}
function drawBig(info, cv) {
var W = 1080, r = cv.width / cv.height, H = Math.max(810, Math.min(1350, Math.round(W / r)));
var c = document.createElement('canvas'); c.width = W; c.height = H;
var g = c.getContext('2d');
g.fillStyle = bgOf(cv.parentElement || cv); g.fillRect(0, 0, W, H);
var w = W, h = w / r;
if (h > H) { h = H; w = h * r; }
g.imageSmoothingEnabled = true; g.imageSmoothingQuality = 'high';
try { g.drawImage(cv, (W - w) / 2, (H - h) / 2, w, h); } catch (e) { return null; }
/* dark fades under the text so it stays readable on any game colours */
var g1 = g.createLinearGradient(0, 0, 0, H * 0.55); g1.addColorStop(0, 'rgba(8,12,28,.74)'); g1.addColorStop(1, 'rgba(8,12,28,0)');
g.fillStyle = g1; g.fillRect(0, 0, W, H * 0.55);
var g2 = g.createLinearGradient(0, H - 200, 0, H); g2.addColorStop(0, 'rgba(8,12,28,0)'); g2.addColorStop(1, 'rgba(8,12,28,.85)');
g.fillStyle = g2; g.fillRect(0, H - 200, W, 200);
var head = info.head || 'Can you beat me?';
var kind = /draw|tie/i.test(head) ? 'draw' : LOSS.test(head) && !WIN.test(head) ? 'loss' : 'win';
g.textAlign = 'center';
var y = 36;
g.fillStyle = 'rgba(255,255,255,.93)'; g.font = '800 34px system-ui, sans-serif'; outline = 7;
y = wrap(g, TITLE.toUpperCase(), W / 2, y + 34, 1000, 42, 1);
g.fillStyle = kind === 'draw' ? '#e2e8f0' : kind === 'loss' ? '#9db8ff' : '#ffc233';
var big = head.length > 22 ? 84 : 112; g.font = '900 ' + big + 'px system-ui, sans-serif'; outline = 16;
y = wrap(g, head, W / 2, y + big * 0.85, 1000, big * 1.05, 2);
if (info.sub) { g.fillStyle = '#fff'; g.font = '700 40px system-ui, sans-serif'; outline = 9; y = wrap(g, info.sub, W / 2, y + 6, 980, 50, 2); }
outline = 0;
if (info.stats.length) {
g.font = '800 38px system-ui, sans-serif';
var gap = 16, pad = 26, ws = [], tot = -gap;
for (var i = 0; i < info.stats.length; i++) { ws.push(g.measureText(info.stats[i]).width + pad * 2); tot += ws[i] + gap; }
var sc = tot > 960 ? 960 / tot : 1, px = (W - tot * sc) / 2, py = y + 14;
for (var k = 0; k < info.stats.length; k++) {
var w0 = ws[k] * sc;
g.fillStyle = 'rgba(8,12,28,.72)'; g.strokeStyle = 'rgba(255,255,255,.55)'; g.lineWidth = 3;
g.beginPath();
if (g.roundRect) g.roundRect(px, py, w0, 68, 34); else g.rect(px, py, w0, 68);
g.fill(); g.stroke();
g.fillStyle = '#fff'; g.font = '800 ' + Math.round(38 * sc) + 'px system-ui, sans-serif';
g.fillText(info.stats[k], px + w0 / 2, py + 47);
px += w0 + gap * sc;
}
}
outline = 8;
g.fillStyle = '#fff'; g.font = '900 52px system-ui, sans-serif'; put(g, 'Can you beat it?', W / 2, H - 84);
g.fillStyle = 'rgba(255,255,255,.92)'; g.font = '700 34px system-ui, sans-serif'; outline = 6; put(g, 'Play free at webdevpuneet.com', W / 2, H - 34);
outline = 0;
return c;
}
function finish(c, cb) {
var dataUrl = '';
try { dataUrl = c.toDataURL('image/png'); } catch (e) {}
try { c.toBlob(function (b) { cb(b, dataUrl); }, 'image/png'); } catch (e) { cb(null, dataUrl); }
}
function makeCard(info, cb) {
var cv0 = bestCanvas();
if (cv0) { var bc = drawBig(info, cv0); if (bc) { finish(bc, cb); return; } }
var S = 1080, c = document.createElement('canvas');
c.width = S; c.height = S;
var g = c.getContext('2d');
var gr = g.createLinearGradient(0, 0, 0, S);
gr.addColorStop(0, '#eaf4ff'); gr.addColorStop(1, '#fff3ef');
g.fillStyle = gr; g.fillRect(0, 0, S, S);
var cv = bestCanvas();
var head = info.head || 'Can you beat me?';
var kind = /draw|tie/i.test(head) ? 'draw' : LOSS.test(head) && !WIN.test(head) ? 'loss' : 'win';
g.textAlign = 'center';
g.fillStyle = '#1e293b'; g.font = '800 60px system-ui, sans-serif';
var y = wrap(g, TITLE, S / 2, cv ? 120 : 200, 980, 68, 1);
g.fillStyle = kind === 'draw' ? '#475569' : kind === 'loss' ? '#3b63e0' : '#e8553a';
g.font = '800 ' + (head.length > 22 ? 76 : 100) + 'px system-ui, sans-serif';
y = wrap(g, head, S / 2, y + (head.length > 22 ? 70 : 90), 980, head.length > 22 ? 84 : 108, 2);
if (info.sub) { g.fillStyle = '#475569'; g.font = '700 38px system-ui, sans-serif'; y = wrap(g, info.sub, S / 2, y + 4, 940, 48, 2); }
if (info.stats.length) {
g.font = '800 36px system-ui, sans-serif';
var gap = 18, pad = 26, ws = [], tot = -gap;
for (var i = 0; i < info.stats.length; i++) { ws.push(g.measureText(info.stats[i]).width + pad * 2); tot += ws[i] + gap; }
var sc = tot > 1000 ? 1000 / tot : 1, px = (S - tot * sc) / 2, py = y + 14;
for (var k = 0; k < info.stats.length; k++) {
var w0 = ws[k] * sc;
g.fillStyle = '#ffffff'; g.strokeStyle = '#d5e2ee'; g.lineWidth = 3;
g.beginPath();
if (g.roundRect) g.roundRect(px, py, w0, 64, 32); else g.rect(px, py, w0, 64);
g.fill(); g.stroke();
g.fillStyle = '#1e293b'; g.font = '800 ' + Math.round(36 * sc) + 'px system-ui, sans-serif';
g.fillText(info.stats[k], px + w0 / 2, py + 44);
px += w0 + gap * sc;
}
y = py + 64 + 10;
}
if (cv) {
try {
var maxW = 960, maxH = Math.max(180, 930 - y - 30), r = cv.width / cv.height;
var w = maxW, h = w / r;
if (h > maxH) { h = maxH; w = h * r; }
var x = (S - w) / 2, yy = y + 20 + Math.max(0, (maxH - h) / 2);
g.save(); g.shadowColor = 'rgba(60,100,140,0.3)'; g.shadowBlur = 30; g.shadowOffsetY = 10;
g.fillStyle = '#fff'; g.fillRect(x - 8, yy - 8, w + 16, h + 16); g.restore();
g.drawImage(cv, x, yy, w, h);
} catch (e) {}
}
g.fillStyle = '#1e293b'; g.font = '800 46px system-ui, sans-serif';
g.fillText('Can you beat it?', S / 2, 1000);
g.fillStyle = '#64748b'; g.font = '700 34px system-ui, sans-serif';
g.fillText('Play free at webdevpuneet.com', S / 2, 1050);
var dataUrl = '';
try { dataUrl = c.toDataURL('image/png'); } catch (e) {}
try { c.toBlob(function (b) { cb(b, dataUrl); }, 'image/png'); } catch (e) { cb(null, dataUrl); }
}
function shareText(info) {
var bits = [];
if (info.head) bits.push(info.head);
if (info.stats.length) bits.push(info.stats.join(', '));
return TITLE + ': ' + (bits.length ? bits.join(' - ') + '. ' : '') + 'Can you beat it? ' + URL;
}
function msg(t) { q('msg').textContent = t; }
function openFallback() {
var t = encodeURIComponent(shareText(lastInfo));
q('wa').href = 'https://wa.me/?text=' + t;
q('x').href = 'https://twitter.com/intent/tweet?text=' + t;
q('fb').href = 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(URL) + '"e=' + t;
box.hidden = false;
}
function nativeShare() {
/* a user cancel does nothing; anything else (blocked in an iframe, no permission) shows Copy / Download / links */
try {
navigator.share({ files: [file], title: TITLE, text: shareText(lastInfo) }).catch(function (e) { if (!e || e.name !== 'AbortError') openFallback(); });
} catch (e) { openFallback(); }
}
function onShare() {
var info = readResult(); info.stats = readStats();
lastInfo = info;
msg('Share your result:');
makeCard(info, function (b, dataUrl) {
blob = b; file = null;
if (b) { try { file = new File([b], 'result.png', { type: 'image/png' }); } catch (e) {} }
if (dataUrl) q('dl').href = dataUrl;
q('dl').hidden = !dataUrl;
var canNative = !!(file && navigator.share && navigator.canShare && navigator.canShare({ files: [file] }));
var touch = !!(window.matchMedia && window.matchMedia('(pointer: coarse)').matches);
q('more').hidden = !canNative;
if (canNative && touch) nativeShare();
else openFallback();
});
}
/* Games often have document/window level touch and pointer handlers (some call preventDefault, which cancels the tap's
click on phones, others restart the game on touchstart). So: keep every pointer event on our controls away from the
game, and fire on touchend as well as click (guarded so one tap shares once). */
var busy = 0;
function trigger() { if (busy) return; busy = 1; setTimeout(function () { busy = 0; }, 700); onShare(); }
function guard(el, act) {
var evs = ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'touchstart', 'touchend', 'click'];
for (var i = 0; i < evs.length; i++) el.addEventListener(evs[i], function (e) { e.stopPropagation(); });
if (act) {
el.addEventListener('touchend', function (e) { if (e.cancelable) e.preventDefault(); act(); });
el.addEventListener('click', function () { act(); });
}
}
guard(pill, trigger);
guard(inl, trigger);
guard(box);
q('more').addEventListener('click', function () { if (file) nativeShare(); });
q('close').addEventListener('click', function () { box.hidden = true; });
document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && !box.hidden) box.hidden = true; });
q('copy').addEventListener('click', function () {
try {
navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]).then(
function () { msg('Image copied. Paste it into your chat or post.'); },
function () { msg('Could not copy here. Try Download image.'); }
);
} catch (e) { msg('Copying images is not supported here. Try Download image.'); }
});
})();
/* wdp-share:end */Extend with AI
Take This Snippet Further With AI
Paste this snippet's HTML, CSS and JS into an AI coding assistant like Claude and ask it to explain why scramble() works backwards instead of dealing colours at random, how the inverse-pour conditions guarantee a solution, and how solve() uses a heuristic and a heap to find hints. It is also a good base to extend: ask for tubes of different heights, a mode with limited undo, a "locked tube" obstacle, colours that mix, or an exact shortest-solution search (IDA* or BFS) to compute a true par. You can also ask it to review the pour animation timing and the busy flag for edge cases when you restart in the middle of a pour.
Prompt to recreate it
Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:
Build a water-sort (color sort) puzzle in plain HTML, CSS and JavaScript with no libraries, images or audio files.
Requirements:
- Glass tubes that hold four units each, drawn with CSS (gloss highlight, rim, rounded bottom). A pour is legal onto an empty tube or a tube with the same top colour and room, and moves the whole top run of that colour that fits. Refuse useless pours: out of a finished tube, or a single-colour tube into an empty tube. A tube full of one colour glows and locks. Win when every colour fills one tube.
- Generate each board by scrambling backwards from the solved state with inverse pours (take k units from a tube top and put them on a tube whose top is a different colour or empty), so a solution always exists and the number of steps is the par. Use a seeded random generator (mulberry32) so level i is the same for everyone. Reject boards that are not mixed enough.
- 20 levels in 4 tiers defined as data (colours 3 to 12, empty tubes, scramble depth, hidden layers). From level 11 the layers below the top unit show "?" until they become the top. Level unlocks after the previous one is solved.
- Stars from moves against par (3 stars within about 25 percent of par, 2 within about 70 percent, otherwise 1, minus one star if a hint was used). A Daily Puzzle seeded from the date with a day streak and a copy result button. Save stars, best moves, symbol setting and the streak to localStorage inside try/catch.
- Free undo using snapshots of the tubes and hidden flags (undo also removes the move from the counter), Restart, and 3 hints per level using a greedy best-first search with a binary heap, a canonical state key that ignores tube order and node and time caps. Highlight the source tube and the target tube.
- Pour animation: the source tube lifts and tilts toward the target, a stream of the right colour flows down, then the units change tubes and the new layers grow in. Lock input while animating and guard timers with a run id. Bubble sounds from the Web Audio API with a mute button, confetti on a win, a result screen with stars.
- Accessibility: an optional symbol on every colour, focusable tubes with aria labels, arrow keys and Enter to play, number keys to select tubes, and Z, H and R for undo, hint and restart.
- Scale the whole game to fit the available area (shrink with transform down to 40 percent) and stack the layout on narrow screens.Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.