Skip to content

Commit

Permalink
Removing the null idea from the grid and grid generator and adding in…
Browse files Browse the repository at this point in the history
… the idea of a default empty value that the generator should use when determining what to fill in. Also adding a readme.
  • Loading branch information
tspayne87 committed Oct 29, 2022
1 parent 32cc163 commit 9570696
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 18 deletions.
15 changes: 14 additions & 1 deletion MonoGame.Grid/Extensions/Models/GridGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ internal class GridGenerator<TValue> : IGridGenerator<TValue>
/// </summary>
private Func<TValue>? _randomGetter;

/// <summary>
/// The empty value we want to use when determining if the value should be filled
/// </summary>
private TValue? _emptyValue;

/// <summary>
/// Builds the generator constructor to fill a grid in
/// </summary>
Expand All @@ -30,6 +35,7 @@ public GridGenerator(IGrid<TValue> grid)
_grid = grid;
_shapesToAvoid = new List<IBaseShape<TValue>>();
_randomGetter = null;
_emptyValue = default;
}

/// <inheritdoc />
Expand All @@ -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);

Expand All @@ -68,6 +74,13 @@ public IGridGenerator<TValue> OverwriteNoneNulls()
return this;
}

/// <inheritdoc />
public IGridGenerator<TValue> EmptyValue(TValue value)
{
_emptyValue = value;
return this;
}

/// <inheritdoc />
public IGridGenerator<TValue> WithRandomValue(Func<TValue> randomGetter)
{
Expand Down
8 changes: 8 additions & 0 deletions MonoGame.Grid/Extensions/Models/IGridGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public interface IGridGenerator<TValue>
/// <returns>Will return the generator for chaining</returns>
public IGridGenerator<TValue> OverwriteNoneNulls();

/// <summary>
/// Sets the empty value that should be used when determing if we need to fill this
/// value in the grid or not
/// </summary>
/// <param name="value">The value we want to use as the empty value</param>
/// <returns>Will return the generator for chaining</returns>
public IGridGenerator<TValue> EmptyValue(TValue value);

/// <summary>
/// Will generate the grid with values
/// </summary>
Expand Down
22 changes: 11 additions & 11 deletions MonoGame.Grid/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Grid<TValue> : IGrid<TValue>
/// <summary>
/// Internal grid meant to store the items for the grid
/// </summary>
private TValue?[] _grid;
private TValue[] _grid;

/// <inheritdoc />
public int Width { get; private set; }
Expand All @@ -25,7 +25,7 @@ public class Grid<TValue> : IGrid<TValue>
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;
Expand All @@ -38,11 +38,11 @@ public Grid()
{
Width = 0;
Height = 0;
_grid = new TValue?[0];
_grid = new TValue[0];
}

/// <inheritdoc />
public TValue? this[int x, int y]
public TValue this[int x, int y]
{
get
{
Expand All @@ -60,27 +60,27 @@ public Grid()
}

/// <inheritdoc />
public TValue? this[int i]
public TValue this[int i]
{
get => _grid[i];
set => _grid[i] = value;
}

/// <inheritdoc />
public TValue? this[Point point]
public TValue this[Point point]
{
get => this[point.X, point.Y];
set => this[point.X, point.Y] = value;
}

/// <inheritdoc />
public ReadOnlySpan<TValue?> AsSpan()
public ReadOnlySpan<TValue> AsSpan()
{
return new ReadOnlySpan<TValue?>(_grid);
return new ReadOnlySpan<TValue>(_grid);
}

/// <inheritdoc />
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;
Expand All @@ -94,7 +94,7 @@ public void Add(params TValue?[] items)
/// <inheritdoc />
public void Clear()
{
_grid = new TValue?[Width * Height];
_grid = new TValue[Width * Height];
}

/// <inheritdoc />
Expand All @@ -104,7 +104,7 @@ public bool Contains(Point point)
}

/// <inheritdoc />
public IEnumerator<TValue?> GetEnumerator()
public IEnumerator<TValue> GetEnumerator()
{
return _grid.AsEnumerable().GetEnumerator();
}
Expand Down
12 changes: 6 additions & 6 deletions MonoGame.Grid/IGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace MonoGame.Grid
/// Grid interface meant to create a grid type object
/// </summary>
/// <typeparam name="TValue">The type of object stored on the grid</typeparam>
public interface IGrid<TValue> : IEnumerable<TValue?>
public interface IGrid<TValue> : IEnumerable<TValue>
{
/// <summary>
/// Internal grid meant to store the items for the grid
Expand All @@ -33,7 +33,7 @@ public interface IGrid<TValue> : IEnumerable<TValue?>
/// 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
/// </exception>
public TValue? this[int x, int y] { get; set; }
public TValue this[int x, int y] { get; set; }

/// <summary>
/// Overloaded operator to get a item at the specific index on the grid based on a 1d array
Expand All @@ -44,7 +44,7 @@ public interface IGrid<TValue> : IEnumerable<TValue?>
/// 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
/// </exception>
public TValue? this[int i] { get; set; }
public TValue this[int i] { get; set; }

/// <summary>
/// Overloaded operator to get and set data at a specific point on the grid
Expand All @@ -55,13 +55,13 @@ public interface IGrid<TValue> : IEnumerable<TValue?>
/// 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
/// </exception>
public TValue? this[Point point] { get; set; }
public TValue this[Point point] { get; set; }

/// <summary>
/// Gets a read only span of the grid, in a single based array where x = i % width and y = i / width
/// </summary>
/// <returns>Returns a span of the grid</returns>
public ReadOnlySpan<TValue?> AsSpan();
public ReadOnlySpan<TValue> AsSpan();

/// <summary>
/// Adds a set of items to the grid one row at a time
Expand All @@ -71,7 +71,7 @@ public interface IGrid<TValue> : IEnumerable<TValue?>
/// 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
/// </remarks>
public void Add(params TValue?[] items);
public void Add(params TValue[] items);

/// <summary>
/// Clears the grid and assigns all values with null
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<string>()
{
{ "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"
```

##

0 comments on commit 9570696

Please sign in to comment.