// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! A board of dots and the arrows on it, and what a tap on one of them does. //! //! The board knows nothing of lives or levels; see `Game` for those. It owns //! its memory, is built once from a list of paths, and changes afterwards //! only by arrows leaving it, or by `reset` putting them all back. const std = @import("std"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; const Board = @This(); /// The largest width or height a board may have, which keeps every dot's /// index and every arrow's index in 16 bits. pub const max_size = 256; /// One cell of the board, counted from the top left. pub const Dot = struct { x: u16, y: u16, pub fn eql(a: Dot, b: Dot) bool { return a.x == b.x and a.y == b.y; } }; /// The direction an arrow points. `up` is towards row 0. pub const Heading = enum { up, right, down, left, pub fn opposite(h: Heading) Heading { return switch (h) { .up => .down, .right => .left, .down => .up, .left => .right, }; } /// The heading a quarter turn clockwise. pub fn clockwise(h: Heading) Heading { return switch (h) { .up => .right, .right => .down, .down => .left, .left => .up, }; } /// The heading of a single step from `from` to the adjacent `to`, or /// null if the two are not adjacent. pub fn between(from: Dot, to: Dot) ?Heading { if (from.x == to.x) { if (to.y +% 1 == from.y) return .up; if (from.y +% 1 == to.y) return .down; } else if (from.y == to.y) { if (from.x +% 1 == to.x) return .right; if (to.x +% 1 == from.x) return .left; } return null; } }; /// An arrow, by its position in the list the board was built from. pub const ArrowIndex = u16; const none: ArrowIndex = std.math.maxInt(ArrowIndex); /// What tapping an arrow does. pub const Outcome = union(enum) { /// The line from the head to the edge was clear and the arrow is gone. exit, /// Something was in the way, and the arrow has not moved. block: Block, }; pub const Block = struct { /// The first arrow in the way. blocker: ArrowIndex, /// How many empty dots lie between the head and the blocker. gap: u16, }; pub const Error = error{ /// The width or height is zero or more than `max_size`. BadSize, /// A dot lies outside the board. DotOutOfBounds, /// An arrow has fewer than two dots, and so no heading. ArrowTooShort, /// Two consecutive dots of an arrow are not adjacent. ArrowNotConnected, /// A dot belongs to more than one arrow, or twice to the same one. ArrowsOverlap, /// An arrow's own body lies on the line from its head to the edge, so /// it could never leave. ArrowFacesItself, }; width: u16, height: u16, /// Every arrow's dots, tail first, one arrow after another. dots: []const Dot, /// Where each arrow's dots start in `dots`, with one more at the end. starts: []const u32, /// The arrow covering each dot, row by row, whether or not it has left. cover: []const ArrowIndex, /// Whether each arrow is still on the board. present: []bool, remaining: u16, /// Builds a board of `width` by `height` dots with an arrow along each of /// `paths`, each given tail first. pub fn init( gpa: Allocator, width: u16, height: u16, paths: []const []const Dot, ) (Error || Allocator.Error)!Board { if (width == 0 or height == 0 or width > max_size or height > max_size) return error.BadSize; const area = @as(usize, width) * height; // More dots than the board has means some dot is covered twice. Since // every arrow is at least two dots, this also keeps the count of arrows // below `none`. var total: usize = 0; for (paths) |p| { if (p.len < 2) return error.ArrowTooShort; total += p.len; } if (total > area) return error.ArrowsOverlap; const dots = try gpa.alloc(Dot, total); errdefer gpa.free(dots); const starts = try gpa.alloc(u32, paths.len + 1); errdefer gpa.free(starts); const cover = try gpa.alloc(ArrowIndex, area); errdefer gpa.free(cover); const present = try gpa.alloc(bool, paths.len); errdefer gpa.free(present); @memset(cover, none); @memset(present, true); var at: u32 = 0; for (paths, 0..) |p, i| { starts[i] = at; for (p, 0..) |dot, j| { if (dot.x >= width or dot.y >= height) return error.DotOutOfBounds; if (j > 0 and Heading.between(p[j - 1], dot) == null) return error.ArrowNotConnected; const c = &cover[@as(usize, dot.y) * width + dot.x]; if (c.* != none) return error.ArrowsOverlap; c.* = @intCast(i); dots[at] = dot; at += 1; } } starts[paths.len] = at; const board: Board = .{ .width = width, .height = height, .dots = dots, .starts = starts, .cover = cover, .present = present, .remaining = @intCast(paths.len), }; for (0..paths.len) |i| { var ray = board.rayFrom(@intCast(i)); while (ray.next()) |dot| { if (board.cover[board.indexOf(dot)] == i) return error.ArrowFacesItself; } } return board; } pub fn deinit(board: *Board, gpa: Allocator) void { gpa.free(board.dots); gpa.free(board.starts); gpa.free(board.cover); gpa.free(board.present); board.* = undefined; } /// A copy with its own memory, arrows present or not as they are here. pub fn clone(board: *const Board, gpa: Allocator) Allocator.Error!Board { const dots = try gpa.dupe(Dot, board.dots); errdefer gpa.free(dots); const starts = try gpa.dupe(u32, board.starts); errdefer gpa.free(starts); const cover = try gpa.dupe(ArrowIndex, board.cover); errdefer gpa.free(cover); const present = try gpa.dupe(bool, board.present); return .{ .width = board.width, .height = board.height, .dots = dots, .starts = starts, .cover = cover, .present = present, .remaining = board.remaining, }; } /// Puts every arrow back on the board. pub fn reset(board: *Board) void { @memset(board.present, true); board.remaining = @intCast(board.present.len); } pub fn arrowCount(board: *const Board) ArrowIndex { return @intCast(board.present.len); } /// An arrow's dots, tail first. pub fn path(board: *const Board, arrow: ArrowIndex) []const Dot { return board.dots[board.starts[arrow]..board.starts[arrow + 1]]; } pub fn head(board: *const Board, arrow: ArrowIndex) Dot { const p = board.path(arrow); return p[p.len - 1]; } pub fn heading(board: *const Board, arrow: ArrowIndex) Heading { const p = board.path(arrow); return Heading.between(p[p.len - 2], p[p.len - 1]).?; } pub fn isPresent(board: *const Board, arrow: ArrowIndex) bool { return board.present[arrow]; } /// The arrow on the board at `dot`, if there is one. pub fn arrowAt(board: *const Board, dot: Dot) ?ArrowIndex { if (dot.x >= board.width or dot.y >= board.height) return null; const a = board.cover[board.indexOf(dot)]; if (a == none or !board.present[a]) return null; return a; } /// What tapping `arrow` would do, without doing it. pub fn check(board: *const Board, arrow: ArrowIndex) Outcome { assert(board.present[arrow]); var ray = board.rayFrom(arrow); var gap: u16 = 0; while (ray.next()) |dot| : (gap += 1) { if (board.arrowAt(dot)) |blocker| return .{ .block = .{ .blocker = blocker, .gap = gap } }; } return .exit; } /// Taps `arrow`, which must be on the board: it leaves if it can. pub fn tap(board: *Board, arrow: ArrowIndex) Outcome { const outcome = board.check(arrow); if (outcome == .exit) { board.present[arrow] = false; board.remaining -= 1; } return outcome; } /// The dots from just past an arrow's head to the edge of the board, in /// order. pub const Ray = struct { at: Dot, heading: Heading, width: u16, height: u16, pub fn next(ray: *Ray) ?Dot { switch (ray.heading) { .up => { if (ray.at.y == 0) return null; ray.at.y -= 1; }, .down => { if (ray.at.y + 1 >= ray.height) return null; ray.at.y += 1; }, .left => { if (ray.at.x == 0) return null; ray.at.x -= 1; }, .right => { if (ray.at.x + 1 >= ray.width) return null; ray.at.x += 1; }, } return ray.at; } }; pub fn rayFrom(board: *const Board, arrow: ArrowIndex) Ray { return board.rayAlong(board.head(arrow), board.heading(arrow)); } pub fn rayAlong(board: *const Board, from: Dot, toward: Heading) Ray { return .{ .at = from, .heading = toward, .width = board.width, .height = board.height }; } fn indexOf(board: *const Board, dot: Dot) usize { return @as(usize, dot.y) * board.width + dot.x; } const testing = std.testing; fn d(x: u16, y: u16) Dot { return .{ .x = x, .y = y }; } test "an arrow with a clear line to the edge leaves" { var board: Board = try .init(testing.allocator, 3, 3, &.{ &.{ d(0, 1), d(1, 1) }, }); defer board.deinit(testing.allocator); try testing.expectEqual(Heading.right, board.heading(0)); try testing.expectEqual(Outcome.exit, board.tap(0)); try testing.expect(!board.isPresent(0)); try testing.expectEqual(0, board.remaining); try testing.expectEqual(null, board.arrowAt(d(1, 1))); } test "an arrow in the way blocks, and nothing moves" { // 0 points right at 1, three dots along, with one empty dot between. var board: Board = try .init(testing.allocator, 5, 3, &.{ &.{ d(0, 1), d(1, 1) }, &.{ d(3, 0), d(3, 1), d(3, 2) }, }); defer board.deinit(testing.allocator); try testing.expectEqual(Outcome{ .block = .{ .blocker = 1, .gap = 1 } }, board.tap(0)); try testing.expect(board.isPresent(0)); try testing.expectEqual(2, board.remaining); // Once the blocker has gone, the way is clear. try testing.expectEqual(Outcome.exit, board.tap(1)); try testing.expectEqual(Outcome.exit, board.tap(0)); try testing.expectEqual(0, board.remaining); } test "only the line ahead of the head matters" { // 1 sits beside 0's body and behind its head, not in front of it. var board: Board = try .init(testing.allocator, 4, 2, &.{ &.{ d(1, 0), d(2, 0) }, &.{ d(0, 0), d(0, 1) }, }); defer board.deinit(testing.allocator); try testing.expectEqual(Outcome.exit, board.check(0)); } test "an arrow at the edge facing out leaves at once" { var board: Board = try .init(testing.allocator, 2, 2, &.{ &.{ d(0, 1), d(0, 0) }, }); defer board.deinit(testing.allocator); try testing.expectEqual(Heading.up, board.heading(0)); try testing.expectEqual(Outcome.exit, board.tap(0)); } test "reset puts every arrow back" { var board: Board = try .init(testing.allocator, 3, 1, &.{ &.{ d(0, 0), d(1, 0) }, }); defer board.deinit(testing.allocator); _ = board.tap(0); board.reset(); try testing.expect(board.isPresent(0)); try testing.expectEqual(1, board.remaining); try testing.expectEqual(0, board.arrowAt(d(1, 0))); } test "malformed boards are refused" { const gpa = testing.allocator; try testing.expectError(error.BadSize, Board.init(gpa, 0, 3, &.{})); try testing.expectError(error.BadSize, Board.init(gpa, max_size + 1, 3, &.{})); try testing.expectError(error.ArrowTooShort, Board.init(gpa, 3, 3, &.{&.{d(0, 0)}})); try testing.expectError(error.DotOutOfBounds, Board.init(gpa, 3, 3, &.{&.{ d(2, 0), d(3, 0) }})); try testing.expectError(error.ArrowNotConnected, Board.init(gpa, 3, 3, &.{&.{ d(0, 0), d(1, 1) }})); try testing.expectError(error.ArrowNotConnected, Board.init(gpa, 3, 3, &.{&.{ d(0, 0), d(0, 0) }})); try testing.expectError(error.ArrowsOverlap, Board.init(gpa, 3, 3, &.{ &.{ d(0, 0), d(1, 0) }, &.{ d(1, 0), d(1, 1) }, })); try testing.expectError(error.ArrowsOverlap, Board.init(gpa, 3, 3, &.{ &.{ d(0, 0), d(1, 0), d(1, 1), d(0, 1), d(0, 0) }, })); } test "an arrow pointing back at its own body is refused" { // A U whose head, at (1,1), points right at its own tail at (2,1). try testing.expectError(error.ArrowFacesItself, Board.init(testing.allocator, 3, 2, &.{ &.{ d(2, 1), d(2, 0), d(1, 0), d(0, 0), d(0, 1), d(1, 1) }, })); }