UTests pass with Pathfinder2D

This commit is contained in:
2021-02-28 07:52:28 -06:00
parent 715b328ef5
commit f55716d2ec
24 changed files with 687 additions and 294 deletions

View File

@@ -1,11 +1,12 @@
using System;
using PathFinding;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState
{
public class Array2D<T> : IEnumerable<T>
public class Array2D<T> : IPlanarCollection<T>, IEnumerable<T>
{
/// <returns>False to stop iterating.</returns>
public delegate void ForEachDelegate(T element, int x, int y);
@@ -31,6 +32,13 @@ namespace Gameboard.ShogiUI.BoardState
set => array[(int)y * width + (int)x] = value;
}
public int GetLength(int dimension) => dimension switch
{
0 => width,
1 => height,
_ => throw new IndexOutOfRangeException()
};
public void ForEach(ForEachDelegate callback)
{
for (var x = 0; x < width; x++)

View File

@@ -1,37 +0,0 @@
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState
{
/// <summary>
/// Provides normalized Vector2s relative to player.
/// "Up" for player 1 is "Down" for player 2; that sort of thing.
/// </summary>
public class Direction
{
private static readonly Vector2 PositiveX = new Vector2(1, 0);
private static readonly Vector2 NegativeX = new Vector2(-1, 0);
private static readonly Vector2 PositiveY = new Vector2(0, 1);
private static readonly Vector2 NegativeY = new Vector2(0, -1);
private static readonly Vector2 PositiveYX = new Vector2(1, 1);
private static readonly Vector2 NegativeYX = new Vector2(-1, -1);
private static readonly Vector2 NegativeYPositiveX = new Vector2(1, -1);
private static readonly Vector2 PositiveYNegativeX = new Vector2(-1, 1);
private readonly WhichPlayer whichPlayer;
public Direction(WhichPlayer whichPlayer)
{
this.whichPlayer = whichPlayer;
}
public Vector2 Up => whichPlayer == WhichPlayer.Player1 ? PositiveY : NegativeY;
public Vector2 Down => whichPlayer == WhichPlayer.Player1 ? NegativeY : PositiveY;
public Vector2 Left => whichPlayer == WhichPlayer.Player1 ? NegativeX : PositiveX;
public Vector2 Right => whichPlayer == WhichPlayer.Player1 ? PositiveX : NegativeX;
public Vector2 UpLeft => whichPlayer == WhichPlayer.Player1 ? PositiveYNegativeX : NegativeYPositiveX;
public Vector2 UpRight => whichPlayer == WhichPlayer.Player1 ? PositiveYX : NegativeYX;
public Vector2 DownLeft => whichPlayer == WhichPlayer.Player1 ? NegativeYX : PositiveYX;
public Vector2 DownRight => whichPlayer == WhichPlayer.Player1 ? NegativeYPositiveX : PositiveYNegativeX;
public Vector2 KnightLeft => whichPlayer == WhichPlayer.Player1 ? new Vector2(-1, 2) : new Vector2(1, -2);
public Vector2 KnightRight => whichPlayer == WhichPlayer.Player1 ? new Vector2(1, 2) : new Vector2(-1, -2);
}
}

View File

@@ -0,0 +1,18 @@
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState
{
public static class Direction
{
public static readonly Vector2 Up = new Vector2(0, 1);
public static readonly Vector2 Down = new Vector2(0, -1);
public static readonly Vector2 Left = new Vector2(-1, 0);
public static readonly Vector2 Right = new Vector2(1, 0);
public static readonly Vector2 UpLeft = new Vector2(1, 1);
public static readonly Vector2 UpRight = new Vector2(-1, 1);
public static readonly Vector2 DownLeft = new Vector2(-1, -1);
public static readonly Vector2 DownRight = new Vector2(1, -1);
public static readonly Vector2 KnightLeft = new Vector2(-1, 2);
public static readonly Vector2 KnightRight = new Vector2(1, 2);
}
}

View File

@@ -4,4 +4,8 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PathFinding\PathFinding.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,7 +1,9 @@
using System.Numerics;
using System.Diagnostics;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState
{
[DebuggerDisplay("{From} - {To}")]
public class Move
{
public WhichPiece? PieceFromCaptured { get; set; }

View File

@@ -1,9 +1,11 @@
using System.Diagnostics;
using PathFinding;
using System.Collections.Generic;
using System.Diagnostics;
namespace Gameboard.ShogiUI.BoardState
{
[DebuggerDisplay("{WhichPiece} {Owner}")]
public class Piece
public abstract class Piece : IPlanarElement
{
public WhichPiece WhichPiece { get; }
public WhichPlayer Owner { get; private set; }
@@ -48,7 +50,7 @@ namespace Gameboard.ShogiUI.BoardState
: WhichPlayer.Player1;
}
public void Promote() => IsPromoted = true;
public void Promote() => IsPromoted = CanPromote;
public void Demote() => IsPromoted = false;
@@ -57,5 +59,11 @@ namespace Gameboard.ShogiUI.BoardState
ToggleOwnership();
Demote();
}
public abstract ICollection<Path> GetPaths();
public abstract Piece DeepClone();
public bool IsUpsideDown => Owner == WhichPlayer.Player2;
}
}

View File

@@ -0,0 +1,47 @@
using PathFinding;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState.Pieces
{
public class Bishop : Piece
{
private static readonly List<Path> MoveSet = new List<Path>(4)
{
new Path(Direction.UpLeft, Distance.MultiStep),
new Path(Direction.UpRight, Distance.MultiStep),
new Path(Direction.DownLeft, Distance.MultiStep),
new Path(Direction.DownRight, Distance.MultiStep)
};
private static readonly List<Path> PromotedMoveSet = new List<Path>(8)
{
new Path(Direction.Up),
new Path(Direction.Left),
new Path(Direction.Right),
new Path(Direction.Down),
new Path(Direction.UpLeft, Distance.MultiStep),
new Path(Direction.UpRight, Distance.MultiStep),
new Path(Direction.DownLeft, Distance.MultiStep),
new Path(Direction.DownRight, Distance.MultiStep)
};
public Bishop(WhichPlayer owner) : base(WhichPiece.Bishop, owner)
{
// TODO: If this strat works out, we can do away with the Direction class entirely.
PromotedMoveSet.AddRange(MoveSet);
}
public override Piece DeepClone()
{
var clone = new Bishop(Owner);
if (IsPromoted) clone.Promote();
return clone;
}
public override ICollection<Path> GetPaths()
{
var moveSet = IsPromoted ? PromotedMoveSet : MoveSet;
return Owner == WhichPlayer.Player1 ? moveSet : moveSet.Select(_ => new Path(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
}
}

View File

@@ -0,0 +1,34 @@
using PathFinding;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState.Pieces
{
public class GoldenGeneral : Piece
{
public static readonly List<Path> MoveSet = new List<Path>(6)
{
new Path(Direction.Up),
new Path(Direction.UpLeft),
new Path(Direction.UpRight),
new Path(Direction.Left),
new Path(Direction.Right),
new Path(Direction.Down)
};
public GoldenGeneral(WhichPlayer owner) : base(WhichPiece.GoldenGeneral, owner)
{
}
public override Piece DeepClone()
{
var clone = new GoldenGeneral(Owner);
if (IsPromoted) clone.Promote();
return clone;
}
public override ICollection<Path> GetPaths() => Owner == WhichPlayer.Player1
? MoveSet
: MoveSet.Select(_ => new Path(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
}

View File

@@ -0,0 +1,35 @@
using PathFinding;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState.Pieces
{
public class King : Piece
{
private static readonly List<Path> MoveSet = new List<Path>(8)
{
new Path(Direction.Up),
new Path(Direction.Left),
new Path(Direction.Right),
new Path(Direction.Down),
new Path(Direction.UpLeft),
new Path(Direction.UpRight),
new Path(Direction.DownLeft),
new Path(Direction.DownRight)
};
public King(WhichPlayer owner) : base(WhichPiece.King, owner)
{
}
public override Piece DeepClone()
{
var clone = new King(Owner);
if (IsPromoted) clone.Promote();
return clone;
}
public override ICollection<Path> GetPaths() => Owner == WhichPlayer.Player1
? MoveSet
: MoveSet.Select(_ => new Path(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
}

View File

@@ -0,0 +1,33 @@
using PathFinding;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState.Pieces
{
public class Knight : Piece
{
private static readonly List<Path> MoveSet = new List<Path>(2)
{
new Path(Direction.KnightLeft),
new Path(Direction.KnightRight)
};
public Knight(WhichPlayer owner) : base(WhichPiece.Knight, owner)
{
}
public override Piece DeepClone()
{
var clone = new Knight(Owner);
if (IsPromoted) clone.Promote();
return clone;
}
public override ICollection<Path> GetPaths()
{
var moveSet = IsPromoted ? GoldenGeneral.MoveSet : MoveSet;
return Owner == WhichPlayer.Player1 ? moveSet : moveSet.Select(_ => new Path(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
}
}

View File

@@ -0,0 +1,32 @@
using PathFinding;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState.Pieces
{
public class Lance : Piece
{
private static readonly List<Path> MoveSet = new List<Path>(1)
{
new Path(Direction.Up, Distance.MultiStep),
};
public Lance(WhichPlayer owner) : base(WhichPiece.Lance, owner)
{
}
public override Piece DeepClone()
{
var clone = new Lance(Owner);
if (IsPromoted) clone.Promote();
return clone;
}
public override ICollection<Path> GetPaths()
{
var moveSet = IsPromoted ? GoldenGeneral.MoveSet : MoveSet;
return Owner == WhichPlayer.Player1 ? moveSet : moveSet.Select(_ => new Path(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
}
}

View File

@@ -0,0 +1,32 @@
using PathFinding;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState.Pieces
{
public class Pawn : Piece
{
private static readonly List<Path> MoveSet = new List<Path>(1)
{
new Path(Direction.Up)
};
public Pawn(WhichPlayer owner) : base(WhichPiece.Pawn, owner)
{
}
public override Piece DeepClone()
{
var clone = new Pawn(Owner);
if (IsPromoted) clone.Promote();
return clone;
}
public override ICollection<Path> GetPaths()
{
var moveSet = IsPromoted ? GoldenGeneral.MoveSet : MoveSet;
return Owner == WhichPlayer.Player1 ? moveSet : moveSet.Select(_ => new Path(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
}
}

View File

@@ -0,0 +1,44 @@
using PathFinding;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState.Pieces
{
public class Rook : Piece
{
private static readonly List<Path> MoveSet = new List<Path>(4)
{
new Path(Direction.Up, Distance.MultiStep),
new Path(Direction.Left, Distance.MultiStep),
new Path(Direction.Right, Distance.MultiStep),
new Path(Direction.Down, Distance.MultiStep)
};
private static readonly List<Path> PromotedMoveSet = new List<Path>(8)
{
new Path(Direction.Up, Distance.MultiStep),
new Path(Direction.Left, Distance.MultiStep),
new Path(Direction.Right, Distance.MultiStep),
new Path(Direction.Down, Distance.MultiStep),
new Path(Direction.UpLeft),
new Path(Direction.UpRight),
new Path(Direction.DownLeft),
new Path(Direction.DownRight)
};
public Rook(WhichPlayer owner) : base(WhichPiece.Rook, owner)
{
}
public override Piece DeepClone()
{
var clone = new Rook(Owner);
if (IsPromoted) clone.Promote();
return clone;
}
public override ICollection<Path> GetPaths()
{
var moveSet = IsPromoted ? PromotedMoveSet : MoveSet;
return Owner == WhichPlayer.Player1 ? moveSet : moveSet.Select(_ => new Path(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
}
}

View File

@@ -0,0 +1,35 @@
using PathFinding;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState.Pieces
{
public class SilverGeneral : Piece
{
private static readonly List<Path> MoveSet = new List<Path>(4)
{
new Path(Direction.Up),
new Path(Direction.UpLeft),
new Path(Direction.UpRight),
new Path(Direction.DownLeft),
new Path(Direction.DownRight)
};
public SilverGeneral(WhichPlayer owner) : base(WhichPiece.SilverGeneral, owner)
{
}
public override Piece DeepClone()
{
var clone = new SilverGeneral(Owner);
if (IsPromoted) clone.Promote();
return clone;
}
public override ICollection<Path> GetPaths()
{
var moveSet = IsPromoted ? GoldenGeneral.MoveSet : MoveSet;
return Owner == WhichPlayer.Player1 ? moveSet : moveSet.Select(_ => new Path(Vector2.Negate(_.Direction), _.Distance)).ToList();
}
}
}

View File

@@ -1,4 +1,6 @@
using System;
using Gameboard.ShogiUI.BoardState.Pieces;
using PathFinding;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
@@ -16,6 +18,7 @@ namespace Gameboard.ShogiUI.BoardState
private ShogiBoard validationBoard;
private Vector2 player1King;
private Vector2 player2King;
private PathFinder2D<Piece> pathFinder;
public IReadOnlyDictionary<WhichPlayer, List<Piece>> Hands { get; }
public Array2D<Piece> Board { get; }
public List<Move> MoveHistory { get; }
@@ -31,17 +34,51 @@ namespace Gameboard.ShogiUI.BoardState
{ WhichPlayer.Player1, new List<Piece>()},
{ WhichPlayer.Player2, new List<Piece>()},
};
pathFinder = new PathFinder2D<Piece>(Board);
InitializeBoardState();
player1King = new Vector2(4, 0);
player2King = new Vector2(4, 8);
}
public ShogiBoard(IList<Move> moves) : this()
{
for (var i = 0; i < moves.Count; i++)
{
if (!Move(moves[i]))
{
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
}
}
}
private ShogiBoard(ShogiBoard toCopy)
{
Board = new Array2D<Piece>(9, 9);
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
Board[x, y] = toCopy.Board[x, y]?.DeepClone();
pathFinder = new PathFinder2D<Piece>(Board);
MoveHistory = new List<Move>(toCopy.MoveHistory);
Hands = new Dictionary<WhichPlayer, List<Piece>>
{
{ WhichPlayer.Player1, new List<Piece>(toCopy.Hands[WhichPlayer.Player1]) },
{ WhichPlayer.Player2, new List<Piece>(toCopy.Hands[WhichPlayer.Player2]) }
};
player1King = toCopy.player1King;
player2King = toCopy.player2King;
}
public bool Move(Move move)
{
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
var moveSuccess = TryMove(move);
if (!moveSuccess) return false;
if (!moveSuccess)
{
return false;
}
// Evaluate check
InCheck = EvaluateCheck(otherPlayer) ? otherPlayer : null;
@@ -59,8 +96,9 @@ namespace Gameboard.ShogiUI.BoardState
// Try making the move in a "throw away" board.
if (validationBoard == null)
{
validationBoard = ConstructWithMoves(MoveHistory);
validationBoard = new ShogiBoard(this);
}
var isValid = move.PieceFromCaptured.HasValue
? validationBoard.PlaceFromHand(move)
: validationBoard.PlaceFromBoard(move);
@@ -92,15 +130,15 @@ namespace Gameboard.ShogiUI.BoardState
if (piece.Owner == InCheck) // Owned by the player in check...
{
var positionsToCheck = new List<Vector2>(10);
IterateMoveSet(from, (innerPiece, position) =>
{
if (innerPiece?.Owner != InCheck) positionsToCheck.Add(position); // Find possible moves...
});
//IterateMoveSet(from, (innerPiece, position) =>
//{
// if (innerPiece?.Owner != InCheck) positionsToCheck.Add(position); // Find possible moves...
//});
// And evaluate if any move gets the player out of check.
foreach (var position in positionsToCheck)
{
if (validationBoard == null) validationBoard = ConstructWithMoves(MoveHistory);
if (validationBoard == null) validationBoard = new ShogiBoard(this);
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position });
if (moveSuccess)
{
@@ -148,7 +186,7 @@ namespace Gameboard.ShogiUI.BoardState
var fromPiece = Board[move.From.X, move.From.Y];
if (fromPiece == null) return false; // Invalid move
if (fromPiece.Owner != WhoseTurn) return false; // Invalid move; cannot move other players pieces.
if (ValidateMoveAgainstMoveSet(move.From, move.To) == false) return false; // Invalid move; move not part of move-set.
if (IsPathable(move.From, move.To, fromPiece) == false) return false; // Invalid move; move not part of move-set.
var captured = Board[move.To.X, move.To.Y];
if (captured != null)
@@ -188,6 +226,17 @@ namespace Gameboard.ShogiUI.BoardState
MoveHistory.Add(move);
return true;
}
private bool IsPathable(Vector2 from, Vector2 to, Piece piece)
{
var isObstructed = false;
var isPathable = pathFinder.PathTo(piece, from, to, (other, position) =>
{
if (other.Owner == piece.Owner) isObstructed = true;
});
return !isObstructed && isPathable;
}
public void PrintStateAsAscii()
{
var builder = new StringBuilder();
@@ -228,219 +277,34 @@ namespace Gameboard.ShogiUI.BoardState
/// </summary>
private bool EvaluateCheck(WhichPlayer whichPlayer)
{
var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
var destination = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
var inCheck = false;
// Iterate every board piece...
Board.ForEachNotNull((piece, x, y) =>
{
var v = new Vector2(x, y);
var origin = new Vector2(x, y);
// ...that belongs to the opponent within range...
if (piece.Owner != whichPlayer && (piece.IsRanged || Vector2.Distance(kingPosition, v) < 3))
if (piece.Owner != whichPlayer && pathFinder.IsPathable(origin, destination, piece))
{
IterateMoveSet(new Vector2(x, y), (threatenedPiece, position) =>
pathFinder.PathTo(piece, origin, destination, (threatenedPiece, position) =>
{
// ...and threatens the player's king.
inCheck |=
threatenedPiece?.WhichPiece == WhichPiece.King
&& threatenedPiece?.Owner == whichPlayer;
threatenedPiece.WhichPiece == WhichPiece.King
&& threatenedPiece.Owner == whichPlayer;
});
}
});
return inCheck;
}
private bool ValidateMoveAgainstMoveSet(Vector2 from, Vector2 to)
{
var isValid = false;
IterateMoveSet(from, (piece, position) =>
{
if (piece?.Owner != WhoseTurn && position == to)
{
isValid = true;
}
});
return isValid;
}
/// <summary>
/// Iterate through the possible moves of a piece at a given position.
/// </summary>
private void IterateMoveSet(Vector2 from, MoveSetCallback callback)
{
// TODO: Make these are of the move To, so only possible moves towards the move To are iterated.
// Maybe separate functions? Sometimes I need to iterate the whole move-set, sometimes I need to iterate only the move-set towards the move To.
var piece = Board[from.X, from.Y];
switch (piece?.WhichPiece)
{
case WhichPiece.King:
IterateKingMoveSet(from, callback);
break;
case WhichPiece.GoldenGeneral:
IterateGoldenGeneralMoveSet(from, callback);
break;
case WhichPiece.SilverGeneral:
IterateSilverGeneralMoveSet(from, callback);
break;
case WhichPiece.Bishop:
IterateBishopMoveSet(from, callback);
break;
case WhichPiece.Rook:
IterateRookMoveSet(from, callback);
break;
case WhichPiece.Knight:
IterateKnightMoveSet(from, callback);
break;
case WhichPiece.Lance:
IterateLanceMoveSet(from, callback);
break;
case WhichPiece.Pawn:
IteratePawnMoveSet(from, callback);
break;
}
}
private void IterateKingMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
BoardStep(from, direction.Up, callback);
BoardStep(from, direction.UpLeft, callback);
BoardStep(from, direction.UpRight, callback);
BoardStep(from, direction.Down, callback);
BoardStep(from, direction.DownLeft, callback);
BoardStep(from, direction.DownRight, callback);
BoardStep(from, direction.Left, callback);
BoardStep(from, direction.Right, callback);
}
private void IterateGoldenGeneralMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
BoardStep(from, direction.Up, callback);
BoardStep(from, direction.UpLeft, callback);
BoardStep(from, direction.UpRight, callback);
BoardStep(from, direction.Down, callback);
BoardStep(from, direction.Left, callback);
BoardStep(from, direction.Right, callback);
}
private void IterateSilverGeneralMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
if (piece.IsPromoted)
{
IterateGoldenGeneralMoveSet(from, callback);
}
else
{
BoardStep(from, direction.Up, callback);
BoardStep(from, direction.UpLeft, callback);
BoardStep(from, direction.UpRight, callback);
BoardStep(from, direction.DownLeft, callback);
BoardStep(from, direction.DownRight, callback);
}
}
private void IterateBishopMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
BoardWalk(from, direction.UpLeft, callback);
BoardWalk(from, direction.UpRight, callback);
BoardWalk(from, direction.DownLeft, callback);
BoardWalk(from, direction.DownRight, callback);
if (piece.IsPromoted)
{
BoardStep(from, direction.Up, callback);
BoardStep(from, direction.Left, callback);
BoardStep(from, direction.Right, callback);
BoardStep(from, direction.Down, callback);
}
}
private void IterateRookMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
BoardWalk(from, direction.Up, callback);
BoardWalk(from, direction.Left, callback);
BoardWalk(from, direction.Right, callback);
BoardWalk(from, direction.Down, callback);
if (piece.IsPromoted)
{
BoardStep(from, direction.UpLeft, callback);
BoardStep(from, direction.UpRight, callback);
BoardStep(from, direction.DownLeft, callback);
BoardStep(from, direction.DownRight, callback);
}
}
private void IterateKnightMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
if (piece.IsPromoted)
{
IterateGoldenGeneralMoveSet(from, callback);
}
else
{
var direction = new Direction(piece.Owner);
BoardStep(from, direction.KnightLeft, callback);
BoardStep(from, direction.KnightRight, callback);
}
}
private void IterateLanceMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
if (piece.IsPromoted)
{
IterateGoldenGeneralMoveSet(from, callback);
}
else
{
var direction = new Direction(piece.Owner);
BoardWalk(from, direction.Up, callback);
}
}
private void IteratePawnMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
if (piece?.WhichPiece == WhichPiece.Pawn)
{
if (piece.IsPromoted)
{
IterateGoldenGeneralMoveSet(from, callback);
}
else
{
var direction = new Direction(piece.Owner);
BoardStep(from, direction.Up, callback);
}
}
}
/// <summary>
/// Useful for iterating the board for pieces that move many spaces.
/// </summary>
/// <param name="callback">A function that returns true if walking should continue.</param>
private void BoardWalk(Vector2 from, Vector2 direction, MoveSetCallback callback)
{
var foundAnotherPiece = false;
var to = Vector2.Add(from, direction);
while (to.X >= 0 && to.X < 9 && to.Y >= 0 && to.Y < 9 && !foundAnotherPiece)
{
var piece = Board[to.X, to.Y];
callback(piece, to);
to = Vector2.Add(to, direction);
foundAnotherPiece = piece != null;
}
}
/// <summary>
/// Useful for iterating the board for pieces that move only one space.
/// </summary>
private void BoardStep(Vector2 from, Vector2 direction, MoveSetCallback callback)
{
var to = Vector2.Add(from, direction);
if (to.X >= 0 && to.X < 9 && to.Y >= 0 && to.Y < 9)
{
callback(Board[to.X, to.Y], to);
}
}
#endregion
#region Initialize
@@ -453,7 +317,7 @@ namespace Gameboard.ShogiUI.BoardState
private void ResetFrontRow(WhichPlayer player)
{
int y = player == WhichPlayer.Player1 ? 2 : 6;
for (int x = 0; x < 9; x++) Board[x, y] = new Piece(WhichPiece.Pawn, player);
for (int x = 0; x < 9; x++) Board[x, y] = new Pawn(player);
}
private void ResetMiddleRow(WhichPlayer player)
{
@@ -464,28 +328,28 @@ namespace Gameboard.ShogiUI.BoardState
Board[8, y] = null;
if (player == WhichPlayer.Player1)
{
Board[1, y] = new Piece(WhichPiece.Bishop, player);
Board[7, y] = new Piece(WhichPiece.Rook, player);
Board[1, y] = new Bishop(player);
Board[7, y] = new Rook(player);
}
else
{
Board[1, y] = new Piece(WhichPiece.Rook, player);
Board[7, y] = new Piece(WhichPiece.Bishop, player);
Board[1, y] = new Rook(player);
Board[7, y] = new Bishop(player);
}
}
private void ResetRearRow(WhichPlayer player)
{
int y = player == WhichPlayer.Player1 ? 0 : 8;
Board[0, y] = new Piece(WhichPiece.Lance, player);
Board[1, y] = new Piece(WhichPiece.Knight, player);
Board[2, y] = new Piece(WhichPiece.SilverGeneral, player);
Board[3, y] = new Piece(WhichPiece.GoldenGeneral, player);
Board[4, y] = new Piece(WhichPiece.King, player);
Board[5, y] = new Piece(WhichPiece.GoldenGeneral, player);
Board[6, y] = new Piece(WhichPiece.SilverGeneral, player);
Board[7, y] = new Piece(WhichPiece.Knight, player);
Board[8, y] = new Piece(WhichPiece.Lance, player);
Board[0, y] = new Lance(player);
Board[1, y] = new Knight(player);
Board[2, y] = new SilverGeneral(player);
Board[3, y] = new GoldenGeneral(player);
Board[4, y] = new King(player);
Board[5, y] = new GoldenGeneral(player);
Board[6, y] = new SilverGeneral(player);
Board[7, y] = new Knight(player);
Board[8, y] = new Lance(player);
}
private void InitializeBoardState()
{
@@ -499,17 +363,17 @@ namespace Gameboard.ShogiUI.BoardState
}
#endregion
public static ShogiBoard ConstructWithMoves(IList<Move> moves)
{
var s = new ShogiBoard();
for (var i = 0; i < moves.Count; i++)
{
if (!s.Move(moves[i]))
{
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
}
}
return s;
}
//public static ShogiBoard ConstructWithMoves(IList<Move> moves)
//{
// var s = new ShogiBoard();
// for (var i = 0; i < moves.Count; i++)
// {
// if (!s.Move(moves[i]))
// {
// throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
// }
// }
// return s;
//}
}
}