// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Builds a board from a seed, solvable by construction. //! //! Arrows are placed one at a time, each with a clear line from its head to //! the edge past every arrow placed before it. Removing them in the reverse //! of that order then always works: when an arrow's turn comes, the only //! arrows left are the ones placed before it, and none of them is in its //! way. An arrow placed later may lie across an earlier one's line, and that //! is the whole of the puzzle. const std = @import("std"); const Allocator = std.mem.Allocator; const Board = @import("Board.zig"); const Random = @import("Random.zig"); const Dot = Board.Dot; const Heading = Board.Heading; pub const Params = struct { width: u16, height: u16, /// How much of the board to cover with arrows, in percent. A crowded /// board may stop short of it. fill: u8, /// The shortest and longest an arrow may be, in dots; at least two. min_length: u16, max_length: u16, /// The chance, in percent, of an arrow's body turning at each dot. turn: u8, }; /// How many placements in a row may fail before the board is taken as full. const max_failures = 200; pub fn generate(gpa: Allocator, params: Params, seed: u64) (Board.Error || Allocator.Error)!Board { std.debug.assert(params.min_length >= 2 and params.min_length <= params.max_length); if (params.width == 0 or params.height == 0 or params.width > Board.max_size or params.height > Board.max_size) return error.BadSize; var random: Random = .init(seed); const width = params.width; const height = params.height; const area = @as(usize, width) * height; const target = area * @min(params.fill, 100) / 100; // Whether each dot is covered by an arrow placed so far, and whether it // is on the line ahead of the arrow being placed. const covered = try gpa.alloc(bool, area); defer gpa.free(covered); @memset(covered, false); const ahead = try gpa.alloc(bool, area); defer gpa.free(ahead); var dots: std.ArrayList(Dot) = .empty; defer dots.deinit(gpa); var ends: std.ArrayList(usize) = .empty; defer ends.deinit(gpa); var arrow: std.ArrayList(Dot) = .empty; defer arrow.deinit(gpa); var failures: usize = 0; while (dots.items.len < target and failures < max_failures) { arrow.clearRetainingCapacity(); if (try place(gpa, &random, params, covered, ahead, &arrow)) { failures = 0; // `arrow` runs head first; the board wants tail first. std.mem.reverse(Dot, arrow.items); for (arrow.items) |dot| covered[@as(usize, dot.y) * width + dot.x] = true; try dots.appendSlice(gpa, arrow.items); try ends.append(gpa, dots.items.len); } else { failures += 1; } } // Shuffle the arrows, so that their numbering says nothing about the // order they come off in. const count = ends.items.len; const paths = try gpa.alloc([]const Dot, count); defer gpa.free(paths); for (paths, 0..) |*p, i| { const start = if (i == 0) 0 else ends.items[i - 1]; p.* = dots.items[start..ends.items[i]]; } var i = count; while (i > 1) { i -= 1; const j = random.below(i + 1); std.mem.swap([]const Dot, &paths[i], &paths[j]); } return Board.init(gpa, width, height, paths); } /// Tries to place one arrow, head first, into `arrow`. fn place( gpa: Allocator, random: *Random, params: Params, covered: []const bool, ahead: []bool, arrow: *std.ArrayList(Dot), ) Allocator.Error!bool { const width = params.width; const height = params.height; const index = struct { fn of(w: u16, dot: Dot) usize { return @as(usize, dot.y) * w + dot.x; } }.of; const head: Dot = .{ .x = @intCast(random.below(width)), .y = @intCast(random.below(height)), }; if (covered[index(width, head)]) return false; const heading: Heading = @fromBackingInt(@intCast(random.below(4))); // The line ahead must be clear of every arrow placed so far, and this // arrow's own body must stay off it too. @memset(ahead, false); var ray: Board.Ray = .{ .at = head, .heading = heading, .width = width, .height = height }; while (ray.next()) |dot| { if (covered[index(width, dot)]) return false; ahead[index(width, dot)] = true; } const length = random.between(params.min_length, params.max_length); try arrow.append(gpa, head); // The body grows backwards from the head. The first step is fixed, since // it is what gives the arrow its heading. var going = heading.opposite(); while (arrow.items.len < length) { const at = arrow.items[arrow.items.len - 1]; // Straight on, or a quarter turn either way, in an order that // prefers straight on unless this is a turn; never back. The first // step is always straight on. const left = going.clockwise().opposite(); const right = going.clockwise(); const turns: [2]Heading = if (random.chance(50)) .{ left, right } else .{ right, left }; const options: []const Heading = if (arrow.items.len == 1) &.{going} else if (random.chance(params.turn)) &.{ turns[0], turns[1], going } else &.{ going, turns[0], turns[1] }; const step: ?Dot = for (options) |h| { var r: Board.Ray = .{ .at = at, .heading = h, .width = width, .height = height }; const next = r.next() orelse continue; const n = index(width, next); if (covered[n] or ahead[n]) continue; for (arrow.items) |taken| { if (taken.eql(next)) break; } else { going = h; break next; } } else null; try arrow.append(gpa, step orelse break); } return arrow.items.len >= params.min_length; } const testing = std.testing; const solve = @import("solve.zig").solve; const small: Params = .{ .width = 8, .height = 8, .fill = 60, .min_length = 2, .max_length = 5, .turn = 30 }; test "a generated board is solvable" { for (0..200) |seed| { var board = try generate(testing.allocator, small, seed); defer board.deinit(testing.allocator); try testing.expect(board.arrowCount() > 0); const order = (try solve(testing.allocator, &board)).?; testing.allocator.free(order); } } test "the same seed builds the same board" { var a = try generate(testing.allocator, small, 37); defer a.deinit(testing.allocator); var b = try generate(testing.allocator, small, 37); defer b.deinit(testing.allocator); try testing.expectEqualSlices(Dot, a.dots, b.dots); try testing.expectEqualSlices(u32, a.starts, b.starts); } test "arrows keep to the lengths asked for" { const params: Params = .{ .width = 12, .height = 12, .fill = 70, .min_length = 3, .max_length = 6, .turn = 50 }; var board = try generate(testing.allocator, params, 5); defer board.deinit(testing.allocator); for (0..board.arrowCount()) |i| { const len = board.path(@intCast(i)).len; try testing.expect(len >= 3 and len <= 6); } }