Switch to Vector2

This commit is contained in:
2021-02-26 17:11:08 -06:00
parent 640db4f4a2
commit 715b328ef5
9 changed files with 154 additions and 219 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState
{
@@ -24,6 +25,11 @@ namespace Gameboard.ShogiUI.BoardState
get => array[y * width + x];
set => array[y * width + x] = value;
}
public T this[float x, float y]
{
get => array[(int)y * width + (int)x];
set => array[(int)y * width + (int)x] = value;
}
public void ForEach(ForEachDelegate callback)
{
@@ -48,15 +54,14 @@ namespace Gameboard.ShogiUI.BoardState
}
}
// TODO: Figure out a better return type, or make this class specific to ShogiBoard.
public BoardVector IndexOf(Predicate<T> predicate)
public Vector2? 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 new Vector2(x, y);
}
}
return null;

View File

@@ -1,21 +1,21 @@
using System.Diagnostics;
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState
{
/// <summary>
/// Provides normalized BoardVectors relative to player.
/// 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 BoardVector PositiveX = new BoardVector(1, 0);
private static readonly BoardVector NegativeX = new BoardVector(-1, 0);
private static readonly BoardVector PositiveY = new BoardVector(0, 1);
private static readonly BoardVector NegativeY = new BoardVector(0, -1);
private static readonly BoardVector PositiveYX = new BoardVector(1, 1);
private static readonly BoardVector NegativeYX = new BoardVector(-1, -1);
private static readonly BoardVector NegativeYPositiveX = new BoardVector(1, -1);
private static readonly BoardVector PositiveYNegativeX = new BoardVector(-1, 1);
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)
@@ -23,40 +23,15 @@ namespace Gameboard.ShogiUI.BoardState
this.whichPlayer = whichPlayer;
}
public BoardVector Up => whichPlayer == WhichPlayer.Player1 ? PositiveY : NegativeY;
public BoardVector Down => whichPlayer == WhichPlayer.Player1 ? NegativeY : PositiveY;
public BoardVector Left => whichPlayer == WhichPlayer.Player1 ? NegativeX : PositiveX;
public BoardVector Right => whichPlayer == WhichPlayer.Player1 ? PositiveX : NegativeX;
public BoardVector UpLeft => whichPlayer == WhichPlayer.Player1 ? PositiveYNegativeX : NegativeYPositiveX;
public BoardVector UpRight => whichPlayer == WhichPlayer.Player1 ? PositiveYX : NegativeYX;
public BoardVector DownLeft => whichPlayer == WhichPlayer.Player1 ? NegativeYX : PositiveYX;
public BoardVector DownRight => whichPlayer == WhichPlayer.Player1 ? NegativeYPositiveX : PositiveYNegativeX;
public BoardVector KnightLeft => whichPlayer == WhichPlayer.Player1 ? new BoardVector(-1, 2) : new BoardVector(1, -2);
public BoardVector KnightRight => whichPlayer == WhichPlayer.Player1 ? new BoardVector(1, 2) : new BoardVector(-1, -2);
}
[DebuggerDisplay("[{X}, {Y}]")]
public class BoardVector
{
public int X { get; set; }
public int Y { get; set; }
public bool IsValidBoardPosition => X > -1 && X < 9 && Y > -1 && Y < 9;
public bool IsHand => X < 0 && Y < 0; // TODO: Find a better way to distinguish positions vs hand.
public BoardVector(int x, int y)
{
X = x;
Y = y;
}
public BoardVector Add(BoardVector other) => new BoardVector(X + other.X, Y + other.Y);
public override bool Equals(object obj) => (obj is BoardVector other) && other.X == X && other.Y == Y;
public override int GetHashCode()
{
// [0,3] should hash different than [3,0]
return X.GetHashCode() * 3 + Y.GetHashCode() * 5;
}
public static bool operator ==(BoardVector a, BoardVector b) => a.Equals(b);
public static bool operator !=(BoardVector a, BoardVector b) => !a.Equals(b);
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

@@ -1,10 +1,12 @@
namespace Gameboard.ShogiUI.BoardState
using System.Numerics;
namespace Gameboard.ShogiUI.BoardState
{
public class Move
{
public WhichPiece? PieceFromCaptured { get; set; }
public BoardVector From { get; set; }
public BoardVector To { get; set; }
public Vector2 From { get; set; }
public Vector2 To { get; set; }
public bool IsPromotion { get; set; }
}
}

View File

@@ -12,7 +12,7 @@ namespace Gameboard.ShogiUI.BoardState
/// </summary>
public class ShogiBoard
{
private delegate void MoveSetCallback(Piece piece, BoardVector position);
private delegate void MoveSetCallback(Piece piece, Vector2 position);
private ShogiBoard validationBoard;
private Vector2 player1King;
private Vector2 player2King;
@@ -35,59 +35,31 @@ namespace Gameboard.ShogiUI.BoardState
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 (!TryMove(moves[i]))
{
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
}
}
}
public bool Move(Move move)
{
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
var moveSuccess = TryMove(move);
if (!moveSuccess) return false;
// Evaluate check
InCheck = EvaluateCheck(otherPlayer) ? otherPlayer : null;
if (InCheck.HasValue)
{
//IsCheckmate = EvaluateCheckmate();
}
return true;
}
/// <summary>
/// Attempts a given move. Returns false if the move is illegal.
/// </summary>
//public bool TryMove2(Move move)
//{
// // Try making the move in a "throw away" board.
// if (validationBoard == null)
// {
// validationBoard = new ShogiBoard(MoveHistory);
// }
// var isValid = move.PieceFromCaptured.HasValue
// ? validationBoard.PlaceFromHand(move)
// : validationBoard.PlaceFromBoard(move);
// if (!isValid)
// {
// // Invalidate the "throw away" board.
// validationBoard = null;
// return false;
// }
// // Assert that this move does not put the moving player in check.
// if (validationBoard.EvaluateCheck(WhoseTurn)) return false;
// var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
// // The move is valid and legal; update board state.
// if (move.PieceFromCaptured.HasValue) PlaceFromHand(move);
// else PlaceFromBoard(move);
// // Evaluate check
// InCheck = EvaluateCheck(otherPlayer) ? otherPlayer : null;
// if (InCheck.HasValue)
// {
// //IsCheckmate = EvaluateCheckmate();
// }
// return true;
//}
public bool TryMove(Move move, bool shouldEvaluateCheck = true)
private bool TryMove(Move move)
{
// Try making the move in a "throw away" board.
if (validationBoard == null)
{
validationBoard = new ShogiBoard(MoveHistory);
validationBoard = ConstructWithMoves(MoveHistory);
}
var isValid = move.PieceFromCaptured.HasValue
? validationBoard.PlaceFromHand(move)
@@ -101,24 +73,11 @@ namespace Gameboard.ShogiUI.BoardState
// Assert that this move does not put the moving player in check.
if (validationBoard.EvaluateCheck(WhoseTurn)) return false;
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
// The move is valid and legal; update board state.
if (move.PieceFromCaptured.HasValue) PlaceFromHand(move);
else PlaceFromBoard(move);
// Evaluate check
if (shouldEvaluateCheck)
{
InCheck = EvaluateCheck(otherPlayer) ? otherPlayer : null;
if (InCheck.HasValue)
{
IsCheckmate = EvaluateCheckmate();
}
}
return true;
}
private bool EvaluateCheckmate()
{
if (!InCheck.HasValue) return false;
@@ -129,10 +88,10 @@ namespace Gameboard.ShogiUI.BoardState
{
if (!isCheckmate) return; // Short circuit
var from = new BoardVector(x, y);
var from = new Vector2(x, y);
if (piece.Owner == InCheck) // Owned by the player in check...
{
var positionsToCheck = new List<BoardVector>(10);
var positionsToCheck = new List<Vector2>(10);
IterateMoveSet(from, (innerPiece, position) =>
{
if (innerPiece?.Owner != InCheck) positionsToCheck.Add(position); // Find possible moves...
@@ -141,7 +100,8 @@ namespace Gameboard.ShogiUI.BoardState
// And evaluate if any move gets the player out of check.
foreach (var position in positionsToCheck)
{
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position }, false);
if (validationBoard == null) validationBoard = ConstructWithMoves(MoveHistory);
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position });
if (moveSuccess)
{
isCheckmate &= validationBoard.EvaluateCheck(InCheck.Value);
@@ -277,8 +237,7 @@ namespace Gameboard.ShogiUI.BoardState
// ...that belongs to the opponent within range...
if (piece.Owner != whichPlayer && (piece.IsRanged || Vector2.Distance(kingPosition, v) < 3))
{
Console.WriteLine($"Evaluating {piece.WhichPiece}");
IterateMoveSet(new BoardVector(x, y), (threatenedPiece, position) =>
IterateMoveSet(new Vector2(x, y), (threatenedPiece, position) =>
{
// ...and threatens the player's king.
inCheck |=
@@ -289,8 +248,7 @@ namespace Gameboard.ShogiUI.BoardState
});
return inCheck;
}
private bool ValidateMoveAgainstMoveSet(BoardVector from, BoardVector to)
private bool ValidateMoveAgainstMoveSet(Vector2 from, Vector2 to)
{
var isValid = false;
IterateMoveSet(from, (piece, position) =>
@@ -306,7 +264,7 @@ namespace Gameboard.ShogiUI.BoardState
/// <summary>
/// Iterate through the possible moves of a piece at a given position.
/// </summary>
private void IterateMoveSet(BoardVector from, MoveSetCallback callback)
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.
@@ -339,7 +297,7 @@ namespace Gameboard.ShogiUI.BoardState
break;
}
}
private void IterateKingMoveSet(BoardVector from, MoveSetCallback callback)
private void IterateKingMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
@@ -352,7 +310,7 @@ namespace Gameboard.ShogiUI.BoardState
BoardStep(from, direction.Left, callback);
BoardStep(from, direction.Right, callback);
}
private void IterateGoldenGeneralMoveSet(BoardVector from, MoveSetCallback callback)
private void IterateGoldenGeneralMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
@@ -363,7 +321,7 @@ namespace Gameboard.ShogiUI.BoardState
BoardStep(from, direction.Left, callback);
BoardStep(from, direction.Right, callback);
}
private void IterateSilverGeneralMoveSet(BoardVector from, MoveSetCallback callback)
private void IterateSilverGeneralMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
@@ -380,7 +338,7 @@ namespace Gameboard.ShogiUI.BoardState
BoardStep(from, direction.DownRight, callback);
}
}
private void IterateBishopMoveSet(BoardVector from, MoveSetCallback callback)
private void IterateBishopMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
@@ -396,7 +354,7 @@ namespace Gameboard.ShogiUI.BoardState
BoardStep(from, direction.Down, callback);
}
}
private void IterateRookMoveSet(BoardVector from, MoveSetCallback callback)
private void IterateRookMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
var direction = new Direction(piece.Owner);
@@ -412,7 +370,7 @@ namespace Gameboard.ShogiUI.BoardState
BoardStep(from, direction.DownRight, callback);
}
}
private void IterateKnightMoveSet(BoardVector from, MoveSetCallback callback)
private void IterateKnightMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
if (piece.IsPromoted)
@@ -426,7 +384,7 @@ namespace Gameboard.ShogiUI.BoardState
BoardStep(from, direction.KnightRight, callback);
}
}
private void IterateLanceMoveSet(BoardVector from, MoveSetCallback callback)
private void IterateLanceMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
if (piece.IsPromoted)
@@ -439,7 +397,7 @@ namespace Gameboard.ShogiUI.BoardState
BoardWalk(from, direction.Up, callback);
}
}
private void IteratePawnMoveSet(BoardVector from, MoveSetCallback callback)
private void IteratePawnMoveSet(Vector2 from, MoveSetCallback callback)
{
var piece = Board[from.X, from.Y];
if (piece?.WhichPiece == WhichPiece.Pawn)
@@ -459,15 +417,15 @@ namespace Gameboard.ShogiUI.BoardState
/// 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(BoardVector from, BoardVector direction, MoveSetCallback callback)
private void BoardWalk(Vector2 from, Vector2 direction, MoveSetCallback callback)
{
var foundAnotherPiece = false;
var to = from.Add(direction);
while (to.IsValidBoardPosition && !foundAnotherPiece)
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 = to.Add(direction);
to = Vector2.Add(to, direction);
foundAnotherPiece = piece != null;
}
}
@@ -475,10 +433,10 @@ namespace Gameboard.ShogiUI.BoardState
/// <summary>
/// Useful for iterating the board for pieces that move only one space.
/// </summary>
private void BoardStep(BoardVector from, BoardVector direction, MoveSetCallback callback)
private void BoardStep(Vector2 from, Vector2 direction, MoveSetCallback callback)
{
var to = from.Add(direction);
if (to.IsValidBoardPosition)
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);
}
@@ -540,5 +498,18 @@ namespace Gameboard.ShogiUI.BoardState
ResetRearRow(WhichPlayer.Player2);
}
#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;
}
}
}