| 12345678910111213141516171819202122232425262728293031323334 |
- using System;
- using System.Collections.Generic;
- namespace DevilLink2.Core
- {
- /// <summary>A snapshot of a legal move. Query again after changing the board.</summary>
- public sealed class BoardMove
- {
- public Tile First { get; }
- public Tile Second { get; }
- public IReadOnlyList<GridPosition> Path { get; }
- internal BoardMove(Tile first, Tile second, IReadOnlyList<GridPosition> path)
- { First = first; Second = second; Path = path; }
- }
- public sealed class BoardMoveFinder
- {
- private readonly LinkPathFinder paths = new LinkPathFinder();
- /// <summary>Scans in stable row/column order without mutating the board.</summary>
- public bool TryFindMove(Board board, out BoardMove move)
- {
- if (board == null) throw new ArgumentNullException(nameof(board));
- move = null;
- var tiles = new List<Tile>();
- 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;
- }
- }
- }
|