before remove validation board

This commit is contained in:
2021-02-25 19:55:43 -06:00
parent f644795cd3
commit 640db4f4a2
8 changed files with 172 additions and 58 deletions

View File

@@ -1,20 +1,21 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.BoardState
{
public class Array2D<T> : IEnumerable
public class Array2D<T> : IEnumerable<T>
{
/// <returns>False to stop iterating.</returns>
public delegate void ForEachDelegate(T element, int x, int y);
private readonly T[] array;
private readonly int width;
private readonly int height;
public Array2D(int width, int height)
{
this.width = width;
this.height = height;
array = new T[width * height];
}
@@ -24,6 +25,48 @@ namespace Gameboard.ShogiUI.BoardState
set => array[y * width + x] = value;
}
public void ForEach(ForEachDelegate callback)
{
for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
{
callback(this[x, y], x, y);
}
}
}
public void ForEachNotNull(ForEachDelegate callback)
{
for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
{
if (this[x, y] != null)
callback(this[x, y], x, y);
}
}
}
// TODO: Figure out a better return type, or make this class specific to ShogiBoard.
public BoardVector IndexOf(Predicate<T> predicate)
{
for (var x = 0; x < width; x++)
for (var y = 0; y < height; y++)
{
if (this[x, y] != null && predicate(this[x, y]))
{
return new BoardVector(x, y);
}
}
return null;
}
public IEnumerator<T> GetEnumerator()
{
foreach (var item in array) yield return item;
}
IEnumerator IEnumerable.GetEnumerator() => array.GetEnumerator();
}
}