Convert Hexapawn to common library

This commit is contained in:
Andrew Cooper
2022-03-18 07:05:27 +11:00
parent 455fea9609
commit 1a8ea5aabd
18 changed files with 527 additions and 603 deletions

View File

@@ -6,68 +6,67 @@ using System.Text;
using static Hexapawn.Pawn;
namespace Hexapawn
namespace Hexapawn;
internal class Board : IEnumerable<Pawn>, IEquatable<Board>
{
internal class Board : IEnumerable<Pawn>, IEquatable<Board>
private readonly Pawn[] _cells;
public Board()
{
private readonly Pawn[] _cells;
public Board()
_cells = new[]
{
_cells = new[]
Black, Black, Black,
None, None, None,
White, White, White
};
}
public Board(params Pawn[] cells)
{
_cells = cells;
}
public Pawn this[int index]
{
get => _cells[index - 1];
set => _cells[index - 1] = value;
}
public Board Reflected => new(Cell.AllCells.Select(c => this[c.Reflected]).ToArray());
public IEnumerator<Pawn> GetEnumerator() => _cells.OfType<Pawn>().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public override string ToString()
{
var builder = new StringBuilder().AppendLine();
for (int row = 0; row < 3; row++)
{
builder.Append(" ");
for (int col = 0; col < 3; col++)
{
Black, Black, Black,
None, None, None,
White, White, White
};
}
public Board(params Pawn[] cells)
{
_cells = cells;
}
public Pawn this[int index]
{
get => _cells[index - 1];
set => _cells[index - 1] = value;
}
public Board Reflected => new(Cell.AllCells.Select(c => this[c.Reflected]).ToArray());
public IEnumerator<Pawn> GetEnumerator() => _cells.OfType<Pawn>().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public override string ToString()
{
var builder = new StringBuilder().AppendLine();
for (int row = 0; row < 3; row++)
{
builder.Append(" ");
for (int col = 0; col < 3; col++)
{
builder.Append(_cells[row * 3 + col]);
}
builder.AppendLine();
builder.Append(_cells[row * 3 + col]);
}
return builder.ToString();
builder.AppendLine();
}
return builder.ToString();
}
public bool Equals(Board other) => other?.Zip(this).All(x => x.First == x.Second) ?? false;
public bool Equals(Board other) => other?.Zip(this).All(x => x.First == x.Second) ?? false;
public override bool Equals(object obj) => Equals(obj as Board);
public override bool Equals(object obj) => Equals(obj as Board);
public override int GetHashCode()
public override int GetHashCode()
{
var hash = 19;
for (int i = 0; i < 9; i++)
{
var hash = 19;
for (int i = 0; i < 9; i++)
{
hash = hash * 53 + _cells[i].GetHashCode();
}
return hash;
hash = hash * 53 + _cells[i].GetHashCode();
}
return hash;
}
}