This commit is contained in:
2021-08-01 17:32:43 -05:00
parent 178cb00253
commit b10f61a489
76 changed files with 1655 additions and 1185 deletions

View File

@@ -1,18 +1,14 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using System;
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
using Gameboard.ShogiUI.Sockets.Utilities;
using System.Diagnostics;
using System.Numerics;
using System.Text.RegularExpressions;
namespace Gameboard.ShogiUI.Sockets.Models
{
[DebuggerDisplay("{From} - {To}")]
public class Move
{
private static readonly string BoardNotationRegex = @"(?<file>[A-I])(?<rank>[1-9])";
private static readonly char A = 'A';
public Vector2? From { get; }
public Vector2? From { get; } // TODO: Use string notation
public bool IsPromotion { get; }
public WhichPiece? PieceFromHand { get; }
public Vector2 To { get; }
@@ -37,8 +33,8 @@ namespace Gameboard.ShogiUI.Sockets.Models
/// <param name="isPromotion">If the moving piece should be promoted.</param>
public Move(string fromNotation, string toNotation, bool isPromotion = false)
{
From = FromBoardNotation(fromNotation);
To = FromBoardNotation(toNotation);
From = NotationHelper.FromBoardNotation(fromNotation);
To = NotationHelper.FromBoardNotation(toNotation);
IsPromotion = isPromotion;
}
@@ -52,35 +48,16 @@ namespace Gameboard.ShogiUI.Sockets.Models
{
From = null;
PieceFromHand = pieceFromHand;
To = FromBoardNotation(toNotation);
To = NotationHelper.FromBoardNotation(toNotation);
IsPromotion = isPromotion;
}
public ServiceModels.Socket.Types.Move ToServiceModel() => new()
public ServiceModels.Types.Move ToServiceModel() => new()
{
From = From.HasValue ? ToBoardNotation(From.Value) : null,
From = From.HasValue ? NotationHelper.ToBoardNotation(From.Value) : null,
IsPromotion = IsPromotion,
PieceFromCaptured = PieceFromHand.HasValue ? PieceFromHand : null,
To = ToBoardNotation(To)
To = NotationHelper.ToBoardNotation(To)
};
private static string ToBoardNotation(Vector2 vector)
{
var file = (char)(vector.X + A);
var rank = vector.Y + 1;
return $"{file}{rank}";
}
private static Vector2 FromBoardNotation(string notation)
{
if (Regex.IsMatch(notation, BoardNotationRegex))
{
var match = Regex.Match(notation, BoardNotationRegex);
char file = match.Groups["file"].Value[0];
int rank = int.Parse(match.Groups["rank"].Value);
return new Vector2(file - A, rank);
}
throw new ArgumentException($"Board notation not recognized. Notation given: {notation}");
}
}
}