using System;
using System.Collections.Generic;
namespace DevilLink2.Core
{
/// A board owns its cell array. Null cells are empty; no gravity or refill.
public sealed class Board
{
private readonly Tile[,] cells;
public int Columns { get; }
public int Rows { get; }
public int RemainingTileCount { get; private set; }
// Sparse layouts are supported for saved fixtures and algorithm callers.
public Board(int columns, int rows, IEnumerable tiles)
{
if (columns <= 0) throw new ArgumentOutOfRangeException(nameof(columns));
if (rows <= 0) throw new ArgumentOutOfRangeException(nameof(rows));
if ((long)columns * rows > int.MaxValue) throw new ArgumentOutOfRangeException(nameof(rows));
if (tiles == null) throw new ArgumentNullException(nameof(tiles));
Columns = columns; Rows = rows;
cells = new Tile[rows, columns];
var ids = new HashSet();
foreach (var tile in tiles)
{
if (tile == null) throw new ArgumentException("Null tile in layout.", nameof(tiles));
if (tile.Owner != null) throw new ArgumentException("Tile already belongs to a board.", nameof(tiles));
ValidatePosition(tile.Row, tile.Column);
if (cells[tile.Row, tile.Column] != null || !ids.Add(tile.Id))
throw new ArgumentException("Duplicate tile position or ID.", nameof(tiles));
cells[tile.Row, tile.Column] = tile;
RemainingTileCount++;
}
foreach (var tile in cells) if (tile != null) tile.Owner = this;
}
public bool Contains(int row, int column) => row >= 0 && row < Rows && column >= 0 && column < Columns;
public Tile GetTile(int row, int column) { ValidatePosition(row, column); return cells[row, column]; }
public bool IsEmpty(int row, int column) => GetTile(row, column) == null;
public bool RemoveTile(int row, int column)
{
ValidatePosition(row, column);
if (cells[row, column] == null) return false;
cells[row, column] = null;
RemainingTileCount--;
return true;
}
// Reject a stale/foreign instance even if its ID and coordinates are equal.
public bool RemoveTile(Tile tile)
{
if (tile == null) throw new ArgumentNullException(nameof(tile));
ValidatePosition(tile.Row, tile.Column);
return ReferenceEquals(cells[tile.Row, tile.Column], tile) && RemoveTile(tile.Row, tile.Column);
}
// A permutation of this board's remaining tile instances, in occupied-cell row order.
// Validate completely before changing cells or coordinates.
internal void Rearrange(IReadOnlyList orderedTiles)
{
if (orderedTiles == null) throw new ArgumentNullException(nameof(orderedTiles));
if (orderedTiles.Count != RemainingTileCount) throw new ArgumentException("Tile count changed.", nameof(orderedTiles));
var expected = new HashSet();
var positions = new List();
for (int r = 0; r < Rows; r++) for (int c = 0; c < Columns; c++)
if (cells[r, c] != null) { expected.Add(cells[r, c]); positions.Add(new GridPosition(r, c)); }
foreach (var tile in orderedTiles)
if (tile == null || !expected.Remove(tile)) throw new ArgumentException("Not a permutation of this board.", nameof(orderedTiles));
for (int i = 0; i < positions.Count; i++)
{
var position = positions[i]; var tile = orderedTiles[i];
cells[position.Row, position.Column] = tile;
tile.SetPosition(position.Row, position.Column);
}
}
private void ValidatePosition(int row, int column)
{
if (row < 0 || row >= Rows) throw new ArgumentOutOfRangeException(nameof(row));
if (column < 0 || column >= Columns) throw new ArgumentOutOfRangeException(nameof(column));
}
}
}