🎮ArcadeLab

Lantern Junction

by LunarHawk81
226 lines10.0 KB
▶ Play
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="A railway-switch logic game. Set five junctions, then send each courier to its lantern without changing the switches between departures.">
<title>Lantern Junction</title>
<style>
  :root { color-scheme: light; background: #edf4f3; color: #173331; font-family: system-ui, sans-serif; }
  * { box-sizing: border-box; }
  body { margin: 0; }
  main { max-width: 49rem; margin: 0 auto; padding: clamp(.75rem, 4vw, 2rem); }
  h1 { margin: 0; font-size: clamp(2.2rem, 7vw, 3.65rem); line-height: 1; letter-spacing: -.06em; }
  h2 { margin: 0 0 .4rem; font-size: 1.05rem; }
  p { margin: .7rem 0; line-height: 1.45; }
  .game { background: #fff; border: 1px solid #c7ddd8; border-radius: 1rem; padding: clamp(.6rem, 3vw, 1.15rem); }
  .rounds, .actions { display: flex; flex-wrap: wrap; gap: .4rem; }
  button { font: inherit; border: 1px solid #8aaba4; background: #f6faf9; color: inherit; border-radius: .45rem; padding: .5rem; cursor: pointer; min-height: 44px; }
  button[aria-pressed="true"], #send { background: #175552; border-color: #175552; color: #fff; }
  button:disabled { cursor: default; opacity: .58; }
  button:focus-visible, a:focus-visible { outline: 3px solid #a05219; outline-offset: 3px; }
  .map { margin: .65rem 0; background: #edf5f3; border-radius: .6rem; }
  svg { display: block; width: 100%; height: auto; max-height: 21rem; }
  .rail { stroke: #a7bdb8; stroke-width: 2; fill: none; }
  .rail.selected { stroke: #2e726c; stroke-width: 4; }
  .rail.travelled { stroke: #9a4e1c; stroke-width: 7; }
  .node circle { fill: #fff; stroke: #22534e; stroke-width: 2; }
  .node text { fill: #173331; font-weight: 800; font-size: 17px; text-anchor: middle; dominant-baseline: central; }
  .node.start circle { fill: #d7ede7; }
  .node.amber circle { fill: #f2d587; }
  .node.red circle { fill: #f6c2b1; }
  .node.blue circle { fill: #c2dcf8; }
  .node text.caption { font-size: 15px; font-weight: 650; }
  .switches { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .35rem; }
  .switches button { padding: .35rem .15rem; }
  .queue { margin: .75rem 0; padding: .65rem .75rem; background: #eef6f3; border-radius: .45rem; list-style: none; }
  .queue li + li { margin-top: .3rem; }
  .queue .done { color: #236049; }
  .queue .missed { color: #8b3113; }
  .status { min-height: 2.7em; }
  .note { color: #435d59; font-size: .93rem; }
  a { color: #155d59; text-underline-offset: .15em; }
  @media (max-width: 25rem) { .switches { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
</style>
</head>
<body>
<main>
  <header>
    <h1>Lantern Junction</h1>
    <p>Set the switches before the first courier leaves. Each courier throws every switch it passes, changing the route for whoever comes next.</p>
  </header>
  <section class="game" aria-label="Railway logic game">
    <nav class="rounds" aria-label="Choose a round">
      <button type="button" data-round="0">Morning</button>
      <button type="button" data-round="1">Midday</button>
      <button type="button" data-round="2">Night</button>
    </nav>
    <div class="map">
      <svg id="track" viewBox="0 0 560 306" role="img" aria-label="Railway map from two starting depots through five switches to three lanterns"></svg>
    </div>
    <h2>Switch settings</h2>
    <p class="note">Tap a switch to change its destination. Dark teal tracks show the current route. Brown tracks show where the last courier went.</p>
    <div class="switches" id="switches"></div>
    <ol class="queue" id="queue" aria-label="Departure order"></ol>
    <p class="status" id="status" role="status" aria-live="polite"></p>
    <div class="actions">
      <button type="button" id="send">Send next courier</button>
      <button type="button" id="reset">Reset switches</button>
    </div>
  </section>
  <p class="note">Before dispatching, trace the second courier’s route after the first one flips its switches. The same habit helps in <a href="https://snake2048.io/" target="_blank" rel="noopener">the Mergecoil puzzle</a>, where a move changes the space you have for the following turn.</p>
</main>
<script>
  const nodes = {
    West: [53, 91], East: [53, 215], J: [157, 91], K: [157, 215],
    L: [321, 48], M: [321, 153], N: [321, 258],
    Amber: [505, 48], Red: [505, 153], Blue: [505, 258]
  };
  const routes = {
    J: ["L", "M"], K: ["M", "N"], L: ["Amber", "Blue"],
    M: ["Amber", "Red"], N: ["Red", "Blue"]
  };
  const depots = { West: "J", East: "K" };
  const rounds = [
    [["West", "Blue"], ["East", "Amber"]],
    [["West", "Red"], ["West", "Amber"]],
    [["East", "Blue"], ["West", "Red"], ["East", "Amber"]]
  ];
  const map = document.querySelector("#track");
  const switchPanel = document.querySelector("#switches");
  const queue = document.querySelector("#queue");
  const status = document.querySelector("#status");
  const send = document.querySelector("#send");
  const roundButtons = [...document.querySelectorAll("[data-round]")];
  const edges = new Map();
  let currentRound = 0;
  let settings;
  let results;
  let lastPath;
  let failed;

  function svg(tag, attributes, content = "") {
    const element = document.createElementNS("http://www.w3.org/2000/svg", tag);
    for (const [name, value] of Object.entries(attributes)) element.setAttribute(name, value);
    element.textContent = content;
    return element;
  }

  function drawTrack(from, to) {
    const [x1, y1] = nodes[from];
    const [x2, y2] = nodes[to];
    const midpoint = (x1 + x2) / 2;
    const edge = svg("path", { class: "rail", d: `M ${x1} ${y1} C ${midpoint} ${y1} ${midpoint} ${y2} ${x2} ${y2}` });
    map.append(edge);
    edges.set(`${from}-${to}`, edge);
  }

  function drawNode(name, [x, y]) {
    const kind = name in depots ? "start" : name in routes ? "switch" : name.toLowerCase();
    const group = svg("g", { class: `node ${kind}`, transform: `translate(${x} ${y})` });
    const caption = kind === "switch" ? "" : name;
    group.append(
      svg("circle", { r: 18 }),
      svg("text", { x: 0, y: 0 }, caption ? name[0] : name),
      svg("text", { x: 0, y: 33, class: "caption" }, caption)
    );
    map.append(group);
  }

  function calculateTrip(origin, startSettings) {
    const nextSettings = { ...startSettings };
    const path = [origin, depots[origin]];
    let current = depots[origin];
    while (current in routes) {
      const outgoing = routes[current][nextSettings[current]];
      nextSettings[current] = 1 - nextSettings[current];
      path.push(outgoing);
      current = outgoing;
    }
    return { path, nextSettings, destination: current };
  }

  function render() {
    const progress = rounds[currentRound];
    const active = new Set(Object.entries(depots).map(([from, to]) => `${from}-${to}`));
    for (const [name, exits] of Object.entries(routes)) active.add(`${name}-${exits[settings[name]]}`);
    const travelled = new Set(lastPath.slice(1).map((name, index) => `${lastPath[index]}-${name}`));
    for (const [key, edge] of edges) {
      edge.classList.toggle("selected", active.has(key));
      edge.classList.toggle("travelled", travelled.has(key));
    }
    for (const [name, button] of switchButtons) {
      const destination = routes[name][settings[name]];
      button.textContent = `${name} → ${destination}`;
      button.setAttribute("aria-label", `Switch ${name}, currently leads to ${destination}`);
      button.disabled = results.length > 0;
    }
    queue.replaceChildren(...progress.map(([origin, target], index) => {
      const item = document.createElement("li");
      const result = results[index];
      item.textContent = `Courier ${index + 1} · ${origin} to ${target}`;
      if (result) {
        item.textContent += result.destination === target ? " · arrived" : ` · reached ${result.destination}`;
        item.className = result.destination === target ? "done" : "missed";
      }
      return item;
    }));
    roundButtons.forEach((button, index) => button.setAttribute("aria-pressed", String(index === currentRound)));
    send.disabled = failed || results.length === progress.length;
  }

  function reset() {
    settings = Object.fromEntries(Object.keys(routes).map(name => [name, 0]));
    results = [];
    lastPath = [];
    failed = false;
    status.textContent = "Set the switches, then send courier 1.";
    render();
  }

  function dispatch() {
    const progress = rounds[currentRound];
    if (failed || results.length === progress.length) return;
    const [origin, target] = progress[results.length];
    const result = calculateTrip(origin, settings);
    results.push(result);
    settings = result.nextSettings;
    lastPath = result.path;
    failed = result.destination !== target;
    if (failed) {
      status.textContent = `Courier ${results.length} reached ${result.destination} instead of ${target}. Reset and try a different plan.`;
    } else if (results.length === progress.length) {
      status.textContent = `All ${results.length} couriers reached their lanterns. Route complete!`;
    } else {
      status.textContent = `Courier ${results.length} arrived. Its switches have flipped. Send the next courier.`;
    }
    render();
  }

  for (const [from, to] of Object.entries(depots)) drawTrack(from, to);
  for (const [from, destinations] of Object.entries(routes)) {
    for (const to of destinations) drawTrack(from, to);
  }
  for (const [name, position] of Object.entries(nodes)) drawNode(name, position);

  const switchButtons = Object.keys(routes).map(name => {
    const button = document.createElement("button");
    button.type = "button";
    button.addEventListener("click", () => {
      settings[name] = 1 - settings[name];
      render();
    });
    switchPanel.append(button);
    return [name, button];
  });
  roundButtons.forEach((button, index) => button.addEventListener("click", () => {
    currentRound = index;
    reset();
  }));
  send.addEventListener("click", dispatch);
  document.querySelector("#reset").addEventListener("click", reset);
  reset();
</script>
</body>
</html>

Game Source: Lantern Junction

Creator: LunarHawk81

Libraries: none

Complexity: complex (226 lines, 10.0 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: lantern-junction-lunarhawk81" to link back to the original. Then publish at arcadelab.ai/publish.