| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.Collections.Generic;
- namespace DevilLink2.Core
- {
- /// <summary>A board owns its cell array. Null cells are empty; no gravity or refill.</summary>
- 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<Tile> 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<int>();
- 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<Tile> 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<Tile>();
- var positions = new List<GridPosition>();
- 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));
- }
- }
- }
|