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,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);
}
}