| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- 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<int> Bounds = new List<int>();
- 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<ArgumentNullException>(() => BoardGenerator.Shuffle<int>(null, random));
- Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => 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<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => board.GetTile(row, column));
- Assert.Throws<ArgumentOutOfRangeException>(() => board.IsEmpty(row, column));
- Assert.Throws<ArgumentOutOfRangeException>(() => board.RemoveTile(row, column));
- Assert.That(board.RemainingTileCount, Is.EqualTo(4));
- }
- [Test]
- public void InvalidLayoutsAndTilesAreRejected()
- {
- Assert.Throws<ArgumentException>(() => new Board(2, 1, new[] { new Tile(1, TileSymbol.imp, 0, 0), new Tile(1, TileSymbol.ring, 0, 1) }));
- Assert.Throws<ArgumentException>(() => new Board(2, 1, new[] { new Tile(1, TileSymbol.imp, 0, 0), new Tile(2, TileSymbol.ring, 0, 0) }));
- Assert.Throws<ArgumentOutOfRangeException>(() => new Board(2, 1, new[] { new Tile(1, TileSymbol.imp, 1, 0) }));
- Assert.Throws<ArgumentNullException>(() => new Board(2, 1, null));
- Assert.Throws<ArgumentException>(() => new Board(2, 1, new Tile[] { null }));
- Assert.Throws<ArgumentOutOfRangeException>(() => new Tile(0, TileSymbol.imp, 0, 0));
- Assert.Throws<ArgumentOutOfRangeException>(() => 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<Tile>(); 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<Tile>();
- 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<Tile> 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<Tile>(); 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<GridPosition> 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);
- }
- }
- }
- }
|