using System; using System.Collections.Generic; namespace DevilLink2.Core { /// A snapshot of a legal move. Query again after changing the board. public sealed class BoardMove { public Tile First { get; } public Tile Second { get; } public IReadOnlyList Path { get; } internal BoardMove(Tile first, Tile second, IReadOnlyList path) { First = first; Second = second; Path = path; } } public sealed class BoardMoveFinder { private readonly LinkPathFinder paths = new LinkPathFinder(); /// Scans in stable row/column order without mutating the board. public bool TryFindMove(Board board, out BoardMove move) { if (board == null) throw new ArgumentNullException(nameof(board)); move = null; var tiles = new List(); for (int r = 0; r < board.Rows; r++) for (int c = 0; c < board.Columns; c++) if (!board.IsEmpty(r, c)) tiles.Add(board.GetTile(r, c)); for (int i = 0; i < tiles.Count; i++) for (int j = i + 1; j < tiles.Count; j++) if (tiles[i].Symbol == tiles[j].Symbol && paths.TryFindPath(board, tiles[i].Position, tiles[j].Position, out var path)) { move = new BoardMove(tiles[i], tiles[j], path); return true; } return false; } } }