Files
logic-puzzle/lib/domain/postcards.dart
T
2026-11-14 08:55:48 +01:00

178 lines
13 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'postcard_patterns.dart';
import 'puzzle.dart';
/// A separate collection; it never changes a book's saved position.
class IslandPostcard {
const IslandPostcard(this.id, this.title, this.invitation, this.message);
final String id, title, invitation, message;
String get illustrationAsset => 'assets/postcards/illustrations/$id.png';
static const puzzlesPerOuting = 3;
String puzzleKey(int slot) => 'postcard_v2_${id}_$slot';
String legacyPuzzleKey(int slot) => 'postcard_v1_${id}_$slot';
Puzzle puzzle(int slot) {
RangeError.checkValidIndex(slot, const [0, 1, 2]);
final index = islandPostcards.indexOf(this);
final (picture, clues) = postcardPatterns[index * puzzlesPerOuting + slot];
final board = Puzzle(7, picture.split('').map(int.parse).toList());
// Overlapping area clues add another way to reason about the same picture.
// Keep the picture, givens and puzzle key stable for saved games.
final mirror = (index + slot).isOdd;
int left(int column) => mirror ? 3 - column : column;
board.regions.addAll([
Region('A', 0, left(0), 4, 4),
Region('B', 1, left(3), 4, 4),
Region('C', 3, left(1), 4, 4),
]);
for (final clue in clues) {
board.given[clue] = true;
}
// Introduce the pair on the last pattern of later outings. Pick two
// editable, opposite cells far apart so both lanterns invite a deduction.
if (index >= 2 && slot == 2) {
LanternPair? pair;
var greatestDistance = -1;
for (var a = 0; a < 49; a++) {
for (var b = a + 1; b < 49; b++) {
if (board.given[a] ||
board.given[b] ||
board.solution[a] == board.solution[b] ||
a ~/ 7 == b ~/ 7 ||
a % 7 == b % 7) {
continue;
}
final distance = (a ~/ 7 - b ~/ 7).abs() + (a % 7 - b % 7).abs();
if (distance > greatestDistance) {
pair = LanternPair(a, b);
greatestDistance = distance;
}
}
}
if (pair != null) board.lanternPairs.add(pair);
}
// The missing total is exactly one more than the previous row's total.
if (index >= 4 && slot == 1) {
for (var row = 1; row < 7; row++) {
if (board.rows[row] == board.rows[row - 1] + 1) {
board.hiddenRow = row;
break;
}
}
}
if (index >= 3 && slot == 0) {
for (var row = 0; row < 7 && board.mirrorPairs.isEmpty; row++) {
for (var col = 0; col < 3; col++) {
final a = row * 7 + col, b = row * 7 + 6 - col;
if (!board.given[a] &&
!board.given[b] &&
board.solution[a] == board.solution[b]) {
board.mirrorPairs.add(LanternPair(a, b));
break;
}
}
}
}
if (index >= 5 && slot == 1 && board.hiddenRow == null) {
for (var row = 0; row < 7 && board.footprintTrails.isEmpty; row++) {
for (var col = 0; col < 5; col++) {
final trail = [row * 7 + col, row * 7 + col + 1, row * 7 + col + 2];
if (trail.every((i) => !board.given[i]) &&
board.solution[trail[1]] == 1 &&
board.solution[trail[0]] + board.solution[trail[2]] == 1) {
board.footprintTrails.add(trail);
break;
}
}
}
}
return board;
}
}
const islandPostcards = <IslandPostcard>[
IslandPostcard(
'washing-line',
'The washing line',
'The morning after the light returned, Mara declared the island ready for ordinary adventures. First stop: the washing line.',
'Mara pinned a postcard between two socks. On its front she had drawn the lighthouse, slightly crooked.\n\n“Artistic freedom,” she said.\n\nA gust took the sheet beside it and wrapped us both in white. For a moment the world was warm cotton, sunlight and Gran laughing somewhere near my elbow.\n\nWhen we escaped, the postcard was still there. On the back she had written: Nothing remarkable happened today. We were very pleased.\n\nI added a small drawing of the sheet. It looked like a ghost. Gran said that was artistic freedom too.',
),
IslandPostcard(
'bakery-door',
'Before the bakery opens',
'There is a queue outside the bakery, although the only people in it are Mara, you, and a gull with unreasonable expectations.',
'The baker opened the door with flour on one eyebrow.\n\n“Usual?”\n\n“Something adventurous,” said Gran.\n\nHe gave her the usual bun upside down. She considered it carefully and approved.\n\nWe ate on the sea wall, catching the crumbs in our palms. The gull watched with the grave disappointment of someone left out of a will. Gran saved a corner for later, wrapping it in the paper bag.\n\n“For the walk home?” I asked.\n\n“For the person I will be in ten minutes,” she said. “I expect she’ll be hungry.”',
),
IslandPostcard(
'tidal-pool',
'A sea in miniature',
'Beyond the harbour steps, the falling tide has left a pool no wider than Mara’s kitchen table. Something small is moving beneath the weed.',
'We crouched without speaking. A shrimp flickered beneath the water, almost invisible until its shadow moved.\n\nI wanted to point it out. Gran had already seen it. She touched my wrist, gently, and we waited.\n\nA crab emerged carrying a strand of weed like an inconvenient scarf. It climbed a pebble, reconsidered, and climbed down.\n\n“Busy morning,” Gran whispered.\n\nWe left everything where we found it. On our postcard I drew the pool larger than the island, because for those few minutes it had been.',
),
IslandPostcard(
'mended-pocket',
'The mended pocket',
'A familiar coat hangs beside the cottage door. Today Mara is repairing the pocket that has been dropping treasures all week.',
'Out came a button, two smooth stones and a shopping list that said only tea.\n\n“That explains yesterday,” Gran said.\n\nShe guided the needle through the lining while I held the cloth flat. Her stitches were small but not perfectly straight. Mine were neither. We decided the pocket would prefer character to elegance.\n\nWhen it was finished, she put one stone back.\n\n“What’s that for?”\n\n“Nothing yet.”\n\nI had thought a pocket was somewhere to keep things you needed. Gran seemed to think it could also be somewhere to keep a possibility.',
),
IslandPostcard(
'borrowed-ladder',
'A ladder between neighbours',
'The harbour noticeboard has lost its top screw. A ladder appears outside the cottage with a note tied to the middle rung.',
'The note said: Bring it back when the sky is fixed.\n\nGran read it twice. “Ambitious.”\n\nWe carried the ladder together, stopping whenever someone wanted to tell us where it had been before. It had rescued a kite, painted three kitchens and helped hang the Christmas lights in June, which apparently had been a complicated year.\n\nThe noticeboard took five minutes. Returning the ladder took an hour and two cups of tea.\n\nOn our postcard I drew all its rungs. Gran wrote the neighbours’ names in the spaces.',
),
IslandPostcard(
'rain-in-shed',
'Rain on the shed roof',
'The clouds arrive halfway through your walk. Mara knows a shortcut to shelter, provided you do not mind sharing it with flowerpots.',
'The shed smelled of earth and old string. Rain struck the roof in uneven handfuls, then settled into a steady rush.\n\nGran turned a flowerpot over for me to sit on. She took the one with a crack.\n\nWe watched a bright bead of water gather beneath the doorframe. It held on longer than seemed possible.\n\n“I thought we were going somewhere,” I said.\n\n“We are.”\n\n“Where?”\n\nShe listened to the roof. “At the moment, here.”\n\nBy the time the rain stopped, I had forgotten to be disappointed.',
),
IslandPostcard(
'ferry-envelope',
'An envelope from the ferry',
'The afternoon ferry brings a parcel, a bicycle and a letter addressed to “the house with the stubborn roses.” Everyone knows where that is.',
'The letter was from someone who had stayed on the island years ago. She remembered the roses. She remembered a cup with a chipped blue handle. She wanted to know whether the light still turned.\n\nGran read that line quietly. Then she fetched paper.\n\n“Yes,” she wrote.\n\nShe paused, looking towards the window, and passed the pen to me.\n\nI told the stranger about the harbour, the repaired lantern and the sound of Gran’s kettle. It took a whole page to explain that single yes.\n\nWe enclosed a rose petal, pressed flat.',
),
IslandPostcard(
'bell-rope',
'The spare bell rope',
'A new rope waits beside the harbour bell. The old one has become too frayed for another winter, but Mara is reluctant to throw it away.',
'We cut the sound lengths into shorter pieces. One would hold the shed door open. Another would tie the roses back. Gran kept the smallest.\n\n“That one won’t reach anything,” I said.\n\nShe made a loop through the hole in our postcard box. Now it had a handle.\n\nAt noon the harbour keeper tested the new rope. The bell sounded exactly as it always had.\n\nI looked at the old fibres resting in Gran’s palm. Their work had changed, that was all.\n\nOn the walk home, I carried the box.',
),
IslandPostcard(
'hill-bench',
'The bench above the roofs',
'There is a bench halfway up the hill. Its plaque has weathered blank, but Mara says you can still tell what it was for.',
'From the bench, the roofs fitted together like pieces of a puzzle. I could see the bakery chimney and the blue shed where we had waited out the rain.\n\n“Who put this here?” I asked.\n\n“I don’t remember.”\n\n“Doesn’t that bother you?”\n\nGran moved her bag to make room for a man coming slowly up the path. He sat with a grateful sigh. For a while, all three of us watched the ferry.\n\nLater, on the back of the postcard, Gran wrote: Whoever you were, thank you.\n\nWe left a space beneath it.',
),
IslandPostcard(
'blue-cup',
'The chipped blue cup',
'Mara has invited a neighbour for tea. The good cups are on the high shelf. She reaches for the chipped blue one instead.',
'“That’s my cup,” the neighbour said, pleased.\n\nI had thought it belonged to Gran.\n\n“It does,” she told me afterwards. “But it knows how he takes his tea.”\n\nHe stayed until the kitchen windows turned gold. They talked about a boat I had never seen and someone who used to sing very badly while mending it. Neither could remember the song. They tried anyway.\n\nAfter he left, I washed the blue cup carefully.\n\nThere was room on our postcard for the cup, the window, and three very wobbly musical notes.',
),
IslandPostcard(
'kite-tail',
'A tail for the kite',
'A child on the common has made a kite that keeps diving into the grass. Mara stops to inspect it with great seriousness.',
'“Too impatient,” she announced.\n\nThe child looked offended.\n\n“The kite,” Gran added.\n\nWe made a tail from scraps of cloth. I held the frame while the child tied each knot. Gran supplied advice, most of which concerned not standing on the string.\n\nThe first flight failed. The second nearly worked. On the third, the kite lifted above us and stayed.\n\nNobody said anything wise. We just shouted.\n\nI drew it in the corner of the postcard, very small, and left almost everything else blue.',
),
IslandPostcard(
'evening-windows',
'Windows at dusk',
'You take the long way home as the island lights its windows. Mara asks you to notice how many different colours warmth can be.',
'The bakery window was honey-yellow. The boat shed glowed pale green through its dusty glass. In one upstairs room, a reading lamp made a circle no bigger than an open book.\n\nI tried to count them all and lost track.\n\nGran pointed to our cottage. Its window was dark.\n\n“Forgot to leave a lamp on,” she said.\n\nSo we hurried the last little stretch. I turned the switch while she put the kettle on. Then I went outside again to look.\n\nOur light belonged among the others. I had never noticed how much I liked that.',
),
IslandPostcard(
'empty-card',
'The card we nearly missed',
'Only two blank postcards remain in the box. You suggest saving them for something special. Mara puts one in her coat pocket.',
'Nothing particularly special happened. We bought milk. We waited while a delivery van reversed. Gran stopped to free a leaf caught in a spider’s web without breaking the threads.\n\nBack at the cottage, the card was still blank.\n\n“Perhaps tomorrow,” I said.\n\nGran took a pencil and drew the leaf. I added the web, carefully, leaving the place where it had been caught empty.\n\nWe studied our work.\n\n“I’m glad we brought it,” she said.\n\nThe milk had gone warm while we were drawing. We drank it anyway.',
),
IslandPostcard(
'room-for-more',
'Room for more',
'The postcard box is nearly full. Tonight you carry it up to the lighthouse, where the island looks small enough to hold in both hands.',
'We laid the cards along the windowsill: the washing line, the pool, the borrowed ladder. Fourteen small places inside one familiar place.\n\nBelow us, the ferry moved through the evening water. Somewhere behind those roofs, the baker was setting dough for morning.\n\n“What shall we do tomorrow?” I asked.\n\nGran fitted the cards back into their box. There was still room along one side.\n\n“We’ll take a pencil,” she said.\n\nThe light passed over the window and went on across the sea. We stayed a little longer, with no particular reason to hurry home.',
),
];