From 9570696591b6b452226f36a09746c07aa1313448 Mon Sep 17 00:00:00 2001 From: Terry Payne Date: Sat, 29 Oct 2022 15:39:59 -0500 Subject: [PATCH] Removing the null idea from the grid and grid generator and adding in the idea of a default empty value that the generator should use when determining what to fill in. Also adding a readme. --- .../Extensions/Models/GridGenerator.cs | 15 +++++++- .../Extensions/Models/IGridGenerator.cs | 8 +++++ MonoGame.Grid/Grid.cs | 22 ++++++------ MonoGame.Grid/IGrid.cs | 12 +++---- README.md | 34 +++++++++++++++++++ 5 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 README.md diff --git a/MonoGame.Grid/Extensions/Models/GridGenerator.cs b/MonoGame.Grid/Extensions/Models/GridGenerator.cs index f7c0e45..7487cf2 100644 --- a/MonoGame.Grid/Extensions/Models/GridGenerator.cs +++ b/MonoGame.Grid/Extensions/Models/GridGenerator.cs @@ -21,6 +21,11 @@ internal class GridGenerator : IGridGenerator /// private Func? _randomGetter; + /// + /// The empty value we want to use when determining if the value should be filled + /// + private TValue? _emptyValue; + /// /// Builds the generator constructor to fill a grid in /// @@ -30,6 +35,7 @@ public GridGenerator(IGrid grid) _grid = grid; _shapesToAvoid = new List>(); _randomGetter = null; + _emptyValue = default; } /// @@ -41,7 +47,7 @@ public void Generate() var span = _grid.AsSpan(); for (var i = span.Length - 1; i >=0; --i) { - if (span[i] == null) + if ((span[i] == null && _emptyValue == null) || span[i]?.Equals(_emptyValue) == true) { var point = i.ToPoint(_grid.Width); @@ -68,6 +74,13 @@ public IGridGenerator OverwriteNoneNulls() return this; } + /// + public IGridGenerator EmptyValue(TValue value) + { + _emptyValue = value; + return this; + } + /// public IGridGenerator WithRandomValue(Func randomGetter) { diff --git a/MonoGame.Grid/Extensions/Models/IGridGenerator.cs b/MonoGame.Grid/Extensions/Models/IGridGenerator.cs index 04b4492..6341667 100644 --- a/MonoGame.Grid/Extensions/Models/IGridGenerator.cs +++ b/MonoGame.Grid/Extensions/Models/IGridGenerator.cs @@ -27,6 +27,14 @@ public interface IGridGenerator /// Will return the generator for chaining public IGridGenerator OverwriteNoneNulls(); + /// + /// Sets the empty value that should be used when determing if we need to fill this + /// value in the grid or not + /// + /// The value we want to use as the empty value + /// Will return the generator for chaining + public IGridGenerator EmptyValue(TValue value); + /// /// Will generate the grid with values /// diff --git a/MonoGame.Grid/Grid.cs b/MonoGame.Grid/Grid.cs index a408cd2..17bf832 100644 --- a/MonoGame.Grid/Grid.cs +++ b/MonoGame.Grid/Grid.cs @@ -9,7 +9,7 @@ public class Grid : IGrid /// /// Internal grid meant to store the items for the grid /// - private TValue?[] _grid; + private TValue[] _grid; /// public int Width { get; private set; } @@ -25,7 +25,7 @@ public class Grid : IGrid public Grid(int width, int height) { if (width <= 0 || height <= 0) throw new IndexOutOfRangeException(); - _grid = new TValue?[width * height]; + _grid = new TValue[width * height]; Width = width; Height = height; @@ -38,11 +38,11 @@ public Grid() { Width = 0; Height = 0; - _grid = new TValue?[0]; + _grid = new TValue[0]; } /// - public TValue? this[int x, int y] + public TValue this[int x, int y] { get { @@ -60,27 +60,27 @@ public Grid() } /// - public TValue? this[int i] + public TValue this[int i] { get => _grid[i]; set => _grid[i] = value; } /// - public TValue? this[Point point] + public TValue this[Point point] { get => this[point.X, point.Y]; set => this[point.X, point.Y] = value; } /// - public ReadOnlySpan AsSpan() + public ReadOnlySpan AsSpan() { - return new ReadOnlySpan(_grid); + return new ReadOnlySpan(_grid); } /// - public void Add(params TValue?[] items) + public void Add(params TValue[] items) { if (_grid.Length == 0) // We only worry about the first item added to calculate the width Width = items.Length; @@ -94,7 +94,7 @@ public void Add(params TValue?[] items) /// public void Clear() { - _grid = new TValue?[Width * Height]; + _grid = new TValue[Width * Height]; } /// @@ -104,7 +104,7 @@ public bool Contains(Point point) } /// - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { return _grid.AsEnumerable().GetEnumerator(); } diff --git a/MonoGame.Grid/IGrid.cs b/MonoGame.Grid/IGrid.cs index 21d043d..66c051a 100644 --- a/MonoGame.Grid/IGrid.cs +++ b/MonoGame.Grid/IGrid.cs @@ -11,7 +11,7 @@ namespace MonoGame.Grid /// Grid interface meant to create a grid type object /// /// The type of object stored on the grid - public interface IGrid : IEnumerable + public interface IGrid : IEnumerable { /// /// Internal grid meant to store the items for the grid @@ -33,7 +33,7 @@ public interface IGrid : IEnumerable /// Will return an out of range exception if either x is between 0 and width of /// grid and x is between 0 and height of grid /// - public TValue? this[int x, int y] { get; set; } + public TValue this[int x, int y] { get; set; } /// /// Overloaded operator to get a item at the specific index on the grid based on a 1d array @@ -44,7 +44,7 @@ public interface IGrid : IEnumerable /// Will return an out of range exception if either x is between 0 and width of /// grid and x is between 0 and height of grid /// - public TValue? this[int i] { get; set; } + public TValue this[int i] { get; set; } /// /// Overloaded operator to get and set data at a specific point on the grid @@ -55,13 +55,13 @@ public interface IGrid : IEnumerable /// Will return an out of range exception if either x is between 0 and width of /// grid and x is between 0 and height of grid /// - public TValue? this[Point point] { get; set; } + public TValue this[Point point] { get; set; } /// /// Gets a read only span of the grid, in a single based array where x = i % width and y = i / width /// /// Returns a span of the grid - public ReadOnlySpan AsSpan(); + public ReadOnlySpan AsSpan(); /// /// Adds a set of items to the grid one row at a time @@ -71,7 +71,7 @@ public interface IGrid : IEnumerable /// This will mainly be used during the construction of the object /// so that it is easier to see the grid based on putting it in code /// - public void Add(params TValue?[] items); + public void Add(params TValue[] items); /// /// Clears the grid and assigns all values with null diff --git a/README.md b/README.md new file mode 100644 index 0000000..d0a8335 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# Monogame Grid +A grid structure that is meant to be used with monogame projects to help store information about a grid object +in a grid. It is also meant to add common functions that will be used on a grid whether it be a turned based game +to a match three game. + +## Installation +#### Package Manager +```bash + Install-Package MonoGame.Grid +``` +#### .NET CLI +```bash + dotnet add package MonoGame.Grid +``` +#### Paket CLI +```bash + paket add MonoGame.Grid +``` + +## Usage +```csharp + var grid = new Grid() + { + { "O", "X", "X" }, + { "O", "O", "X" }, + { "O", "X", "O" } + }; + + var item1 = grid[1, 2]; // -- "X" + var item2 = grid[3]; // Is equal to 1, 0 which will return "O"; + var item3 = grid[new Point(1, 2)] // Will use the monogame point structure and will use the x and y like above which will return "X" +``` + +## \ No newline at end of file