| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using BallSort.Engine.Exceptions; |
| | | 3 | | using BallSort.Engine.Extensions; |
| | | 4 | | using BallSort.Engine.Models; |
| | | 5 | | |
| | | 6 | | namespace BallSort.Engine.Game; |
| | | 7 | | |
| | | 8 | | public class Board |
| | | 9 | | { |
| | | 10 | | private Stack<Colour>[] _columns; |
| | | 11 | | |
| | 152 | 12 | | private readonly HashSet<Colour> _colours = []; |
| | | 13 | | |
| | 152 | 14 | | private readonly Stack<Move> _history = []; |
| | | 15 | | |
| | 9864558 | 16 | | public int Width { get; private init; } |
| | | 17 | | |
| | 1320294 | 18 | | public int Height { get; private init; } |
| | | 19 | | |
| | 2 | 20 | | public int Colours => _colours.Count; |
| | | 21 | | |
| | 28 | 22 | | private Board() |
| | | 23 | | { |
| | 28 | 24 | | } |
| | | 25 | | |
| | 124 | 26 | | public Board(Puzzle puzzle) |
| | | 27 | | { |
| | 124 | 28 | | _columns = new Stack<Colour>[puzzle.GridWidth]; |
| | | 29 | | |
| | 124 | 30 | | var index = 0; |
| | | 31 | | |
| | 124 | 32 | | Width = puzzle.GridWidth; |
| | | 33 | | |
| | 124 | 34 | | Height = puzzle.GridHeight; |
| | | 35 | | |
| | 1992 | 36 | | for (var column = 0; column < Width; column++) |
| | | 37 | | { |
| | 872 | 38 | | _columns[column] = new Stack<Colour>(Height); |
| | | 39 | | |
| | 8384 | 40 | | for (var row = 0; row < Height; row++) |
| | | 41 | | { |
| | 3320 | 42 | | var ball = (Colour) puzzle.Data.Layout[index]; |
| | | 43 | | |
| | 3320 | 44 | | if (ball != Colour.Empty) |
| | | 45 | | { |
| | 2526 | 46 | | _columns[column].Push(ball); |
| | | 47 | | |
| | 2526 | 48 | | _colours.Add(ball); |
| | | 49 | | } |
| | | 50 | | |
| | 3320 | 51 | | index++; |
| | | 52 | | } |
| | | 53 | | } |
| | 124 | 54 | | } |
| | | 55 | | |
| | | 56 | | public void Move(Move move) |
| | | 57 | | { |
| | 10578 | 58 | | var source = move.Source; |
| | | 59 | | |
| | 10578 | 60 | | var target = move.Target; |
| | | 61 | | |
| | 10578 | 62 | | Guard(source, "Source column {column} is out of bounds."); |
| | | 63 | | |
| | 10574 | 64 | | if (_columns[source].Count == 0) |
| | | 65 | | { |
| | 2 | 66 | | throw new InvalidMoveException($"Source column {source} is empty. Move id {move.Id}."); |
| | | 67 | | } |
| | | 68 | | |
| | 10572 | 69 | | var sourceBall = _columns[source].Peek(); |
| | | 70 | | |
| | 10572 | 71 | | Guard(target, $"Target column {move.Target} is out of bounds. Move id {move.Id}."); |
| | | 72 | | |
| | 10568 | 73 | | if (Height - _columns[target].Count == 0) |
| | | 74 | | { |
| | 2 | 75 | | throw new InvalidMoveException($"Target column {target} is full. Move id {move.Id}."); |
| | | 76 | | } |
| | | 77 | | |
| | 10566 | 78 | | var targetBall = Top(target); |
| | | 79 | | |
| | 10566 | 80 | | if (targetBall != Colour.Empty && sourceBall != targetBall) |
| | | 81 | | { |
| | 2 | 82 | | throw new InvalidMoveException($"Cannot move {sourceBall.ToHumanReadable()} ball from column {source} onto { |
| | | 83 | | } |
| | | 84 | | |
| | 10564 | 85 | | _columns[target].Push(_columns[source].Pop()); |
| | | 86 | | |
| | 10564 | 87 | | _history.Push(new Move(source, target)); |
| | 10564 | 88 | | } |
| | | 89 | | |
| | | 90 | | public void Move(int source, int target) |
| | | 91 | | { |
| | 18 | 92 | | Move(new Move(source, target)); |
| | 4 | 93 | | } |
| | | 94 | | |
| | | 95 | | public void UndoLastMove() |
| | | 96 | | { |
| | 8856 | 97 | | if (_history.Count > 0) |
| | | 98 | | { |
| | 8856 | 99 | | var lastMove = _history.Pop(); |
| | | 100 | | |
| | 8856 | 101 | | _columns[lastMove.Source].Push(_columns[lastMove.Target].Pop()); |
| | | 102 | | } |
| | 8856 | 103 | | } |
| | | 104 | | |
| | | 105 | | public Board Clone() |
| | | 106 | | { |
| | 28 | 107 | | var board = new Board |
| | 28 | 108 | | { |
| | 28 | 109 | | _columns = new Stack<Colour>[Width], |
| | 28 | 110 | | Width = Width, |
| | 28 | 111 | | Height = Height |
| | 28 | 112 | | }; |
| | | 113 | | |
| | 672 | 114 | | for (var column = 0; column < _columns.Length; column++) |
| | | 115 | | { |
| | 308 | 116 | | board._columns[column] = new Stack<Colour>(_columns[column].Reverse()); |
| | | 117 | | } |
| | | 118 | | |
| | 28 | 119 | | return board; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | public ReadOnlySpan<Colour> GetColumn(int column) |
| | | 123 | | { |
| | 152368 | 124 | | Guard(column); |
| | | 125 | | |
| | 152366 | 126 | | var data = new Colour[Height]; |
| | | 127 | | |
| | 152366 | 128 | | var i = _columns[column].Count - 1; |
| | | 129 | | |
| | 1483984 | 130 | | foreach (var item in _columns[column]) |
| | | 131 | | { |
| | 589626 | 132 | | data[i] = item; |
| | | 133 | | |
| | 589626 | 134 | | i--; |
| | | 135 | | } |
| | | 136 | | |
| | 152366 | 137 | | return data; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | public bool IsComplete(int column) |
| | | 141 | | { |
| | 120344 | 142 | | Guard(column); |
| | | 143 | | |
| | 120344 | 144 | | if (_columns[column].Count != Height) |
| | | 145 | | { |
| | 55546 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | 64798 | 149 | | var colour = _columns[column].Peek(); |
| | | 150 | | |
| | 553104 | 151 | | foreach (var item in _columns[column]) |
| | | 152 | | { |
| | 239014 | 153 | | if (item != colour) |
| | | 154 | | { |
| | 54520 | 155 | | return false; |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | 10278 | 159 | | return true; |
| | 54520 | 160 | | } |
| | | 161 | | |
| | | 162 | | public bool IsEmpty(int column) |
| | | 163 | | { |
| | 811266 | 164 | | Guard(column); |
| | | 165 | | |
| | 811266 | 166 | | return _columns[column].Count == 0; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | public int BallCount(int column) |
| | | 170 | | { |
| | 129464 | 171 | | Guard(column); |
| | | 172 | | |
| | 129464 | 173 | | return _columns[column].Count; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public Colour Top(int column) |
| | | 177 | | { |
| | 3547966 | 178 | | Guard(column); |
| | | 179 | | |
| | 3547966 | 180 | | if (_columns[column].Count == 0) |
| | | 181 | | { |
| | 8802 | 182 | | return Colour.Empty; |
| | | 183 | | } |
| | | 184 | | |
| | 3539164 | 185 | | return _columns[column].Peek(); |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | public int TopRunLength(int column) |
| | | 189 | | { |
| | 71262 | 190 | | Guard(column); |
| | | 191 | | |
| | 71262 | 192 | | var stack = _columns[column]; |
| | | 193 | | |
| | 71262 | 194 | | if (! stack.TryPeek(out var colour)) |
| | | 195 | | { |
| | 2 | 196 | | return 0; |
| | | 197 | | } |
| | | 198 | | |
| | 71260 | 199 | | var length = 1; |
| | | 200 | | |
| | 71260 | 201 | | var enumerator = stack.GetEnumerator(); |
| | | 202 | | |
| | 71260 | 203 | | enumerator.MoveNext(); |
| | | 204 | | |
| | 148934 | 205 | | while (enumerator.MoveNext()) |
| | | 206 | | { |
| | 134868 | 207 | | if (enumerator.Current != colour) |
| | | 208 | | { |
| | | 209 | | break; |
| | | 210 | | } |
| | | 211 | | |
| | 77674 | 212 | | length++; |
| | | 213 | | } |
| | | 214 | | |
| | 71260 | 215 | | return length; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | public bool IsFull(int column) |
| | | 219 | | { |
| | 63412 | 220 | | return Capacity(column) == 0; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | public bool IsPure(int column) |
| | | 224 | | { |
| | 89514 | 225 | | Guard(column); |
| | | 226 | | |
| | 89514 | 227 | | var colour = _columns[column].Peek(); |
| | | 228 | | |
| | 671464 | 229 | | foreach (var item in _columns[column]) |
| | | 230 | | { |
| | 280136 | 231 | | if (item != colour) |
| | | 232 | | { |
| | 67836 | 233 | | return false; |
| | | 234 | | } |
| | | 235 | | } |
| | | 236 | | |
| | 21678 | 237 | | return true; |
| | 67836 | 238 | | } |
| | | 239 | | |
| | | 240 | | public int Capacity(int column) |
| | | 241 | | { |
| | 116424 | 242 | | Guard(column); |
| | | 243 | | |
| | 116424 | 244 | | return Height - _columns[column].Count; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | public bool IsSolved() |
| | | 248 | | { |
| | 23404 | 249 | | for (var x = 0; x < Width; x++) |
| | | 250 | | { |
| | 11678 | 251 | | if (IsEmpty(x)) |
| | | 252 | | { |
| | | 253 | | continue; |
| | | 254 | | } |
| | | 255 | | |
| | 11572 | 256 | | if (! IsComplete(x)) |
| | | 257 | | { |
| | 10536 | 258 | | return false; |
| | | 259 | | } |
| | | 260 | | } |
| | | 261 | | |
| | 24 | 262 | | return true; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 266 | | private void Guard(int column, string messageTemplate = null) |
| | | 267 | | { |
| | 5059758 | 268 | | if ((uint) column >= (uint) Width) |
| | | 269 | | { |
| | 10 | 270 | | if (messageTemplate == null) |
| | | 271 | | { |
| | 2 | 272 | | throw new OutOfBoundsException($"Column {column} is out of bounds."); |
| | | 273 | | } |
| | | 274 | | |
| | 8 | 275 | | throw new OutOfBoundsException(messageTemplate.Replace("{column}", column.ToString())); |
| | | 276 | | } |
| | 5059748 | 277 | | } |
| | | 278 | | } |