using System; using System.Collections.Generic; using System.Linq; using DevilLink2.Core; using NUnit.Framework; namespace DevilLink2.Tests { public sealed class CoreAlgorithmTests { private readonly LinkPathFinder finder = new LinkPathFinder(); [TestCase(8, 4)] [TestCase(8, 6)] [TestCase(14, 10)] public void GenerationFillsCellsWithUniqueIdsAndExpectedEvenSymbolCounts(int columns, int rows) { var board = BoardGenerator.Generate(columns, rows, 2026); var tiles = Tiles(board).ToArray(); Assert.That(board.Columns, Is.EqualTo(columns)); Assert.That(board.Rows, Is.EqualTo(rows)); Assert.That(board.RemainingTileCount, Is.EqualTo(columns * rows)); Assert.That(tiles.Select(t => t.Id).Distinct().Count(), Is.EqualTo(tiles.Length)); for (int i = 0; i < tiles.Length; i++) { Assert.That(tiles[i].Row, Is.EqualTo(i / columns)); Assert.That(tiles[i].Column, Is.EqualTo(i % columns)); } foreach (TileSymbol symbol in Enum.GetValues(typeof(TileSymbol))) { int expectedPairs = Enumerable.Range(0, tiles.Length / 2).Count(i => i % 12 == (int)symbol); Assert.That(tiles.Count(t => t.Symbol == symbol), Is.EqualTo(expectedPairs * 2)); } } [TestCase(8, 4)] [TestCase(8, 6)] [TestCase(14, 10)] public void SameSeedReproducesBoard(int columns, int rows) { Assert.That(Tiles(BoardGenerator.Generate(columns, rows, -7)).Select(t => t.Symbol), Is.EqualTo(Tiles(BoardGenerator.Generate(columns, rows, -7)).Select(t => t.Symbol))); } [Test] public void SymbolOrderMatchesSource() { Assert.That(Enum.GetNames(typeof(TileSymbol)), Is.EqualTo(new[] { "imp", "trident", "ring", "potion", "candle", "star", "piggy", "A", "Q", "J", "T10", "N9" })); } private sealed class ZeroRandom : Random { public readonly List Bounds = new List(); public override int Next(int maxValue) { Bounds.Add(maxValue); return 0; } } [Test] public void FisherYatesUsesDescendingBoundsAndPreservesPermutation() { var values = new[] { 0, 1, 2, 3 }; var random = new ZeroRandom(); BoardGenerator.Shuffle(values, random); Assert.That(random.Bounds, Is.EqualTo(new[] { 4, 3, 2 })); Assert.That(values, Is.EqualTo(new[] { 1, 2, 3, 0 })); BoardGenerator.Shuffle(new int[0], random); BoardGenerator.Shuffle(new[] { 5 }, random); Assert.That(random.Bounds.Count, Is.EqualTo(3)); Assert.Throws(() => BoardGenerator.Shuffle(null, random)); Assert.Throws(() => BoardGenerator.Shuffle(values, null)); } [TestCase(0, 4)] [TestCase(4, 0)] [TestCase(-1, 4)] [TestCase(3, 3)] [TestCase(int.MaxValue, 2)] public void InvalidGenerationIsRejected(int columns, int rows) { Assert.Catch(() => BoardGenerator.Generate(columns, rows, 0)); } [TestCase("AA", 0)] [TestCase("A.A", 0)] [TestCase("#####/#A..#/###.#/###A#/#####", 1)] [TestCase("#####/#A.##/##.##/##.A#/#####", 2)] [TestCase("A#A", 2)] [TestCase("A/#/A", 2)] public void LegalPathsAreCompressedAndRespectObstacles(string layout, int turns) { var board = Layout(layout); var ends = Tiles(board).Where(t => t.Symbol == TileSymbol.imp).ToArray(); int count = board.RemainingTileCount; Assert.That(finder.TryFindPath(board, ends[0].Position, ends[1].Position, out var path), Is.True); Assert.That(path.Count - 2, Is.EqualTo(turns)); ValidatePath(board, ends[0].Position, ends[1].Position, path); Assert.That(board.RemainingTileCount, Is.EqualTo(count)); } [TestCase("#####/#A.##/##..#/###A#/#####")] // Three forced turns; endpoints cannot reach the border. [TestCase("#####/#A#A#/#####")] // Blocked straight route and no escape. public void ThreeTurnsOrBlockingWallsFail(string layout) { var board = Layout(layout); var ends = Tiles(board).Where(t => t.Symbol == TileSymbol.imp).ToArray(); Assert.That(finder.TryFindPath(board, ends[0].Position, ends[1].Position, out var path), Is.False); Assert.That(path, Is.Empty); } [Test] public void InvalidEndpointsFailIncludingOutsideTheSingleBorder() { var board = Layout("A.#A"); Assert.That(finder.TryFindPath(board, new GridPosition(0, 0), new GridPosition(0, 2), out _), Is.False); Assert.That(finder.TryFindPath(board, new GridPosition(0, 0), new GridPosition(0, 0), out _), Is.False); Assert.That(finder.TryFindPath(board, new GridPosition(0, 0), new GridPosition(0, 1), out _), Is.False); foreach (var p in new[] { new GridPosition(-2, 0), new GridPosition(2, 0), new GridPosition(0, -2), new GridPosition(0, 5), new GridPosition(-1, 0), new GridPosition(0, 4) }) Assert.That(finder.TryFindPath(board, new GridPosition(0, 0), p, out _), Is.False); Assert.Throws(() => finder.TryFindPath(null, new GridPosition(0, 0), new GridPosition(0, 1), out _)); } [Test] public void RemovalIsSafeAndDoesNotRefillOrRemoveForeignTiles() { var board = Layout("AA"); var tile = board.GetTile(0, 0); Assert.That(board.RemoveTile(new Tile(tile.Id, tile.Symbol, 0, 0)), Is.False); Assert.That(board.RemoveTile(tile), Is.True); Assert.That(board.IsEmpty(0, 0), Is.True); Assert.That(board.RemoveTile(tile), Is.False); Assert.That(board.RemoveTile(0, 0), Is.False); Assert.That(board.RemainingTileCount, Is.EqualTo(1)); Assert.That(board.GetTile(0, 1), Is.Not.Null); Assert.That(finder.TryFindPath(board, tile.Position, new GridPosition(0, 1), out _), Is.False); Assert.That(board.RemoveTile(0, 1), Is.True); Assert.That(board.RemainingTileCount, Is.Zero); } [TestCase(-1, 0)] [TestCase(0, -1)] [TestCase(2, 0)] [TestCase(0, 2)] public void BoardAccessAndRemovalRejectOutOfBounds(int row, int column) { var board = Layout("AA/AA"); Assert.Throws(() => board.GetTile(row, column)); Assert.Throws(() => board.IsEmpty(row, column)); Assert.Throws(() => board.RemoveTile(row, column)); Assert.That(board.RemainingTileCount, Is.EqualTo(4)); } [Test] public void InvalidLayoutsAndTilesAreRejected() { Assert.Throws(() => new Board(2, 1, new[] { new Tile(1, TileSymbol.imp, 0, 0), new Tile(1, TileSymbol.ring, 0, 1) })); Assert.Throws(() => new Board(2, 1, new[] { new Tile(1, TileSymbol.imp, 0, 0), new Tile(2, TileSymbol.ring, 0, 0) })); Assert.Throws(() => new Board(2, 1, new[] { new Tile(1, TileSymbol.imp, 1, 0) })); Assert.Throws(() => new Board(2, 1, null)); Assert.Throws(() => new Board(2, 1, new Tile[] { null })); Assert.Throws(() => new Tile(0, TileSymbol.imp, 0, 0)); Assert.Throws(() => new Tile(1, (TileSymbol)99, 0, 0)); } [Test] public void ExhaustiveSmallBoardsAgreeWithIndependentCornerEnumeration() { // Every pair of endpoints and every obstacle mask on a 3x2 board: 240 layouts. for (int a = 0; a < 6; a++) for (int b = a + 1; b < 6; b++) for (int mask = 0; mask < 16; mask++) { var tiles = new List(); int bit = 0; for (int i = 0; i < 6; i++) { if (i == a || i == b) tiles.Add(new Tile(i + 1, TileSymbol.imp, i / 3, i % 3)); else if ((mask & (1 << bit++)) != 0) tiles.Add(new Tile(i + 1, TileSymbol.ring, i / 3, i % 3)); } var board = new Board(3, 2, tiles); var start = new GridPosition(a / 3, a % 3); var end = new GridPosition(b / 3, b % 3); bool actual = finder.TryFindPath(board, start, end, out var path); Assert.That(actual, Is.EqualTo(CornerOracle(board, start, end)), $"a={a}, b={b}, mask={mask}"); if (actual) ValidatePath(board, start, end, path); } } [Test] public void RandomInteriorLayoutsAgreeWithCornerOracleInBothDirections() { var random = new Random(87031); for (int sample = 0; sample < 600; sample++) { int a = random.Next(25), b; do { b = random.Next(25); } while (a == b); var tiles = new List(); for (int i = 0; i < 25; i++) { if (i == a || i == b) tiles.Add(new Tile(i + 1, TileSymbol.imp, i / 5, i % 5)); else if (random.Next(100) < 65) tiles.Add(new Tile(i + 1, TileSymbol.ring, i / 5, i % 5)); } var board = new Board(5, 5, tiles); var start = new GridPosition(a / 5, a % 5); var end = new GridPosition(b / 5, b % 5); bool expected = CornerOracle(board, start, end); Assert.That(finder.TryFindPath(board, start, end, out var path), Is.EqualTo(expected), $"sample={sample}"); if (expected) ValidatePath(board, start, end, path); Assert.That(finder.TryFindPath(board, end, start, out var reverse), Is.EqualTo(expected)); if (expected) ValidatePath(board, end, start, reverse); Assert.That(board.RemainingTileCount, Is.EqualTo(tiles.Count)); } } private static IEnumerable Tiles(Board board) { for (int r = 0; r < board.Rows; r++) for (int c = 0; c < board.Columns; c++) if (!board.IsEmpty(r, c)) yield return board.GetTile(r, c); } private static Board Layout(string layout) { var rows = layout.Split('/'); var tiles = new List(); int id = 0; for (int r = 0; r < rows.Length; r++) for (int c = 0; c < rows[0].Length; c++) if (rows[r][c] != '.') tiles.Add(new Tile(++id, rows[r][c] == 'A' ? TileSymbol.imp : TileSymbol.ring, r, c)); return new Board(rows[0].Length, rows.Length, tiles); } private static bool Segment(Board board, GridPosition a, GridPosition b, GridPosition start, GridPosition end) { if (a.Row != b.Row && a.Column != b.Column) return false; int dr = Math.Sign(b.Row - a.Row), dc = Math.Sign(b.Column - a.Column); var p = a; while (true) { if (p.Row < -1 || p.Row > board.Rows || p.Column < -1 || p.Column > board.Columns) return false; if (p != start && p != end && board.Contains(p.Row, p.Column) && !board.IsEmpty(p.Row, p.Column)) return false; if (p == b) return true; p = new GridPosition(p.Row + dr, p.Column + dc); } } private static bool CornerOracle(Board board, GridPosition a, GridPosition b) { // Enumerate straight paths and all possible horizontal/vertical middle segments. if (Segment(board, a, b, a, b)) return true; for (int r = -1; r <= board.Rows; r++) { var x = new GridPosition(r, a.Column); var y = new GridPosition(r, b.Column); if (Segment(board, a, x, a, b) && Segment(board, x, y, a, b) && Segment(board, y, b, a, b)) return true; } for (int c = -1; c <= board.Columns; c++) { var x = new GridPosition(a.Row, c); var y = new GridPosition(b.Row, c); if (Segment(board, a, x, a, b) && Segment(board, x, y, a, b) && Segment(board, y, b, a, b)) return true; } return false; } private static void ValidatePath(Board board, GridPosition start, GridPosition end, IReadOnlyList path) { Assert.That(path.Count, Is.InRange(2, 4)); Assert.That(path[0], Is.EqualTo(start)); Assert.That(path[path.Count - 1], Is.EqualTo(end)); for (int i = 1; i < path.Count; i++) { Assert.That(path[i], Is.Not.EqualTo(path[i - 1])); Assert.That(Segment(board, path[i - 1], path[i], start, end), Is.True); if (i < path.Count - 1) Assert.That(path[i - 1].Row == path[i].Row && path[i].Row == path[i + 1].Row || path[i - 1].Column == path[i].Column && path[i].Column == path[i + 1].Column, Is.False); } } } }