| | 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 | |
|
| 96 | 12 | | private readonly HashSet<Colour> _colours = []; |
| | 13 | |
|
| | 14 | | private Move? _lastMove; |
| | 15 | |
|
| 250944 | 16 | | public int Width { get; private init; } |
| | 17 | |
|
| 102290 | 18 | | public int Height { get; private init; } |
| | 19 | |
|
| 2 | 20 | | public int Colours => _colours.Count; |
| | 21 | |
|
| 10 | 22 | | private Board() |
| | 23 | | { |
| 10 | 24 | | } |
| | 25 | |
|
| 86 | 26 | | public Board(Puzzle puzzle) |
| | 27 | | { |
| 86 | 28 | | _columns = new Stack<Colour>[puzzle.GridWidth]; |
| | 29 | |
|
| 86 | 30 | | var index = 0; |
| | 31 | |
|
| 86 | 32 | | Width = puzzle.GridWidth; |
| | 33 | |
|
| 86 | 34 | | Height = puzzle.GridHeight; |
| | 35 | |
|
| 940 | 36 | | for (var column = 0; column < Width; column++) |
| | 37 | | { |
| 384 | 38 | | _columns[column] = new Stack<Colour>(Height); |
| | 39 | |
|
| 3248 | 40 | | for (var row = 0; row < Height; row++) |
| | 41 | | { |
| 1240 | 42 | | var ball = (Colour) puzzle.Data.Layout[index]; |
| | 43 | |
|
| 1240 | 44 | | if (ball != Colour.Empty) |
| | 45 | | { |
| 758 | 46 | | _columns[column].Push(ball); |
| | 47 | |
|
| 758 | 48 | | _colours.Add(ball); |
| | 49 | | } |
| | 50 | |
|
| 1240 | 51 | | index++; |
| | 52 | | } |
| | 53 | | } |
| 86 | 54 | | } |
| | 55 | |
|
| | 56 | | public void Move(Move move, bool force = false) |
| | 57 | | { |
| 804 | 58 | | var source = move.Source; |
| | 59 | |
|
| 804 | 60 | | var target = move.Target; |
| | 61 | |
|
| 804 | 62 | | Guard(source, "Source column {column} is out of bounds."); |
| | 63 | |
|
| 800 | 64 | | if (_columns[source].Count == 0) |
| | 65 | | { |
| 2 | 66 | | throw new InvalidMoveException($"Source column {source} is empty. Move id {move.Id}."); |
| | 67 | | } |
| | 68 | |
|
| 798 | 69 | | var sourceBall = _columns[source].Peek(); |
| | 70 | |
|
| 798 | 71 | | Guard(target, $"Target column {move.Target} is out of bounds. Move id {move.Id}."); |
| | 72 | |
|
| 794 | 73 | | if (Height - _columns[target].Count == 0) |
| | 74 | | { |
| 2 | 75 | | throw new InvalidMoveException($"Target column {target} is full. Move id {move.Id}."); |
| | 76 | | } |
| | 77 | |
|
| 792 | 78 | | var targetBall = Top(target); |
| | 79 | |
|
| 792 | 80 | | if (! force && targetBall != Colour.Empty && sourceBall != targetBall) |
| | 81 | | { |
| 2 | 82 | | throw new InvalidMoveException($"Cannot move {sourceBall.ToHumanReadable()} ball from column {source} onto { |
| | 83 | | } |
| | 84 | |
|
| 790 | 85 | | _columns[target].Push(_columns[source].Pop()); |
| | 86 | |
|
| 790 | 87 | | _lastMove = new Move(source, target); |
| | 88 | |
|
| 790 | 89 | | } |
| | 90 | |
|
| | 91 | | public void Move(int source, int target, bool force = false) |
| | 92 | | { |
| 228 | 93 | | Move(new Move(source, target), force); |
| 214 | 94 | | } |
| | 95 | |
|
| | 96 | | public void UndoLastMove() |
| | 97 | | { |
| 210 | 98 | | if (_lastMove.HasValue) |
| | 99 | | { |
| 210 | 100 | | Move(_lastMove.Value.Target, _lastMove.Value.Source, true); |
| | 101 | | } |
| 210 | 102 | | } |
| | 103 | |
|
| | 104 | | public Board Clone() |
| | 105 | | { |
| 10 | 106 | | var board = new Board |
| 10 | 107 | | { |
| 10 | 108 | | _columns = new Stack<Colour>[Width], |
| 10 | 109 | | Width = Width, |
| 10 | 110 | | Height = Height |
| 10 | 111 | | }; |
| | 112 | |
|
| 156 | 113 | | for (var column = 0; column < _columns.Length; column++) |
| | 114 | | { |
| 68 | 115 | | board._columns[column] = new Stack<Colour>(_columns[column].Reverse()); |
| | 116 | | } |
| | 117 | |
|
| 10 | 118 | | return board; |
| | 119 | | } |
| | 120 | |
|
| | 121 | | public Colour[] GetColumn(int column) |
| | 122 | | { |
| 5586 | 123 | | Guard(column); |
| | 124 | |
|
| 5584 | 125 | | var data = new Colour[Height]; |
| | 126 | |
|
| 5584 | 127 | | var i = _columns[column].Count - 1; |
| | 128 | |
|
| 46300 | 129 | | foreach (var item in _columns[column]) |
| | 130 | | { |
| 17566 | 131 | | data[i] = item; |
| | 132 | |
|
| 17566 | 133 | | i--; |
| | 134 | | } |
| | 135 | |
|
| 5584 | 136 | | return data; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | public bool IsComplete(int column) |
| | 140 | | { |
| 11990 | 141 | | Guard(column); |
| | 142 | |
|
| 11990 | 143 | | if (_columns[column].Count != Height) |
| | 144 | | { |
| 9542 | 145 | | return false; |
| | 146 | | } |
| | 147 | |
|
| 2448 | 148 | | var colour = _columns[column].Peek(); |
| | 149 | |
|
| 22242 | 150 | | foreach (var item in _columns[column]) |
| | 151 | | { |
| 9044 | 152 | | if (item != colour) |
| | 153 | | { |
| 742 | 154 | | return false; |
| | 155 | | } |
| | 156 | | } |
| | 157 | |
|
| 1706 | 158 | | return true; |
| 742 | 159 | | } |
| | 160 | |
|
| | 161 | | public bool IsEmpty(int column) |
| | 162 | | { |
| 52472 | 163 | | Guard(column); |
| | 164 | |
|
| 52472 | 165 | | return _columns[column].Count == 0; |
| | 166 | | } |
| | 167 | |
|
| | 168 | | public int BallCount(int column) |
| | 169 | | { |
| 2164 | 170 | | Guard(column); |
| | 171 | |
|
| 2164 | 172 | | return _columns[column].Count; |
| | 173 | | } |
| | 174 | |
|
| | 175 | | public Colour Top(int column) |
| | 176 | | { |
| 28864 | 177 | | Guard(column); |
| | 178 | |
|
| 28864 | 179 | | if (_columns[column].Count == 0) |
| | 180 | | { |
| 854 | 181 | | return Colour.Empty; |
| | 182 | | } |
| | 183 | |
|
| 28010 | 184 | | return _columns[column].Peek(); |
| | 185 | | } |
| | 186 | |
|
| | 187 | | public int TopRunLength(int column) |
| | 188 | | { |
| 1512 | 189 | | Guard(column); |
| | 190 | |
|
| 1512 | 191 | | var stack = _columns[column]; |
| | 192 | |
|
| 1512 | 193 | | if (! stack.TryPeek(out var colour)) |
| | 194 | | { |
| 2 | 195 | | return 0; |
| | 196 | | } |
| | 197 | |
|
| 1510 | 198 | | var length = 1; |
| | 199 | |
|
| 1510 | 200 | | var enumerator = stack.GetEnumerator(); |
| | 201 | |
|
| 1510 | 202 | | enumerator.MoveNext(); |
| | 203 | |
|
| 2020 | 204 | | while (enumerator.MoveNext()) |
| | 205 | | { |
| 834 | 206 | | if (enumerator.Current != colour) |
| | 207 | | { |
| | 208 | | break; |
| | 209 | | } |
| | 210 | |
|
| 510 | 211 | | length++; |
| | 212 | | } |
| | 213 | |
|
| 1510 | 214 | | return length; |
| | 215 | | } |
| | 216 | |
|
| | 217 | | public bool IsFull(int column) |
| | 218 | | { |
| 35200 | 219 | | return Capacity(column) == 0; |
| | 220 | | } |
| | 221 | |
|
| | 222 | | public int Capacity(int column) |
| | 223 | | { |
| 51704 | 224 | | Guard(column); |
| | 225 | |
|
| 51704 | 226 | | return Height - _columns[column].Count; |
| | 227 | | } |
| | 228 | |
|
| | 229 | | public bool IsSolved() |
| | 230 | | { |
| 2436 | 231 | | for (var x = 0; x < Width; x++) |
| | 232 | | { |
| 1212 | 233 | | if (IsEmpty(x)) |
| | 234 | | { |
| | 235 | | continue; |
| | 236 | | } |
| | 237 | |
|
| 1126 | 238 | | if (! IsComplete(x)) |
| | 239 | | { |
| 570 | 240 | | return false; |
| | 241 | | } |
| | 242 | | } |
| | 243 | |
|
| 6 | 244 | | return true; |
| | 245 | | } |
| | 246 | |
|
| | 247 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 248 | | private void Guard(int column, string messageTemplate = null) |
| | 249 | | { |
| 155894 | 250 | | if ((uint) column >= (uint) Width) |
| | 251 | | { |
| 10 | 252 | | if (messageTemplate == null) |
| | 253 | | { |
| 2 | 254 | | throw new OutOfBoundsException($"Column {column} is out of bounds."); |
| | 255 | | } |
| | 256 | |
|
| 8 | 257 | | throw new OutOfBoundsException(messageTemplate.Replace("{column}", column.ToString())); |
| | 258 | | } |
| 155884 | 259 | | } |
| | 260 | | } |