Board.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DevilLink2.Core
  4. {
  5. /// <summary>A board owns its cell array. Null cells are empty; no gravity or refill.</summary>
  6. public sealed class Board
  7. {
  8. private readonly Tile[,] cells;
  9. public int Columns { get; }
  10. public int Rows { get; }
  11. public int RemainingTileCount { get; private set; }
  12. // Sparse layouts are supported for saved fixtures and algorithm callers.
  13. public Board(int columns, int rows, IEnumerable<Tile> tiles)
  14. {
  15. if (columns <= 0) throw new ArgumentOutOfRangeException(nameof(columns));
  16. if (rows <= 0) throw new ArgumentOutOfRangeException(nameof(rows));
  17. if ((long)columns * rows > int.MaxValue) throw new ArgumentOutOfRangeException(nameof(rows));
  18. if (tiles == null) throw new ArgumentNullException(nameof(tiles));
  19. Columns = columns; Rows = rows;
  20. cells = new Tile[rows, columns];
  21. var ids = new HashSet<int>();
  22. foreach (var tile in tiles)
  23. {
  24. if (tile == null) throw new ArgumentException("Null tile in layout.", nameof(tiles));
  25. if (tile.Owner != null) throw new ArgumentException("Tile already belongs to a board.", nameof(tiles));
  26. ValidatePosition(tile.Row, tile.Column);
  27. if (cells[tile.Row, tile.Column] != null || !ids.Add(tile.Id))
  28. throw new ArgumentException("Duplicate tile position or ID.", nameof(tiles));
  29. cells[tile.Row, tile.Column] = tile;
  30. RemainingTileCount++;
  31. }
  32. foreach (var tile in cells) if (tile != null) tile.Owner = this;
  33. }
  34. public bool Contains(int row, int column) => row >= 0 && row < Rows && column >= 0 && column < Columns;
  35. public Tile GetTile(int row, int column) { ValidatePosition(row, column); return cells[row, column]; }
  36. public bool IsEmpty(int row, int column) => GetTile(row, column) == null;
  37. public bool RemoveTile(int row, int column)
  38. {
  39. ValidatePosition(row, column);
  40. if (cells[row, column] == null) return false;
  41. cells[row, column] = null;
  42. RemainingTileCount--;
  43. return true;
  44. }
  45. // Reject a stale/foreign instance even if its ID and coordinates are equal.
  46. public bool RemoveTile(Tile tile)
  47. {
  48. if (tile == null) throw new ArgumentNullException(nameof(tile));
  49. ValidatePosition(tile.Row, tile.Column);
  50. return ReferenceEquals(cells[tile.Row, tile.Column], tile) && RemoveTile(tile.Row, tile.Column);
  51. }
  52. // A permutation of this board's remaining tile instances, in occupied-cell row order.
  53. // Validate completely before changing cells or coordinates.
  54. internal void Rearrange(IReadOnlyList<Tile> orderedTiles)
  55. {
  56. if (orderedTiles == null) throw new ArgumentNullException(nameof(orderedTiles));
  57. if (orderedTiles.Count != RemainingTileCount) throw new ArgumentException("Tile count changed.", nameof(orderedTiles));
  58. var expected = new HashSet<Tile>();
  59. var positions = new List<GridPosition>();
  60. for (int r = 0; r < Rows; r++) for (int c = 0; c < Columns; c++)
  61. if (cells[r, c] != null) { expected.Add(cells[r, c]); positions.Add(new GridPosition(r, c)); }
  62. foreach (var tile in orderedTiles)
  63. if (tile == null || !expected.Remove(tile)) throw new ArgumentException("Not a permutation of this board.", nameof(orderedTiles));
  64. for (int i = 0; i < positions.Count; i++)
  65. {
  66. var position = positions[i]; var tile = orderedTiles[i];
  67. cells[position.Row, position.Column] = tile;
  68. tile.SetPosition(position.Row, position.Column);
  69. }
  70. }
  71. private void ValidatePosition(int row, int column)
  72. {
  73. if (row < 0 || row >= Rows) throw new ArgumentOutOfRangeException(nameof(row));
  74. if (column < 0 || column >= Columns) throw new ArgumentOutOfRangeException(nameof(column));
  75. }
  76. }
  77. }