Files
Shogi/Gameboard.ShogiUI.BoardState/BoardVector.cs
2021-02-23 18:03:23 -06:00

63 lines
2.9 KiB
C#

using System.Diagnostics;
namespace Gameboard.ShogiUI.BoardState
{
/// <summary>
/// Provides normalized BoardVectors 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 readonly WhichPlayer whichPlayer;
public Direction(WhichPlayer whichPlayer)
{
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);
}
}