Better communication

This commit is contained in:
2021-02-19 20:19:11 -06:00
parent d76e4f7a8b
commit 8d79c75616
11 changed files with 156 additions and 64 deletions

View File

@@ -0,0 +1,41 @@
using System;
using System.Text.RegularExpressions;
namespace Gameboard.ShogiUI.Sockets.Models
{
public class Coords
{
private const string BoardNotationRegex = @"(?<file>[A-I])(?<rank>[1-9])";
private const char A = 'A';
public int X { get; }
public int Y { get; }
public Coords(int x, int y)
{
X = x;
Y = y;
}
public string ToBoardNotation()
{
var file = (char)(X + A);
var rank = Y + 1;
return $"{file}{rank}";
}
public static Coords FromBoardNotation(string notation)
{
if (string.IsNullOrEmpty(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 Coords(file - A, rank);
}
throw new ArgumentException("Board notation not recognized."); // TODO: Move this error handling to the service layer.
}
return new Coords(-1, -1); // Temporarily this is how I tell Gameboard.API that a piece came from the hand.
}
}
}

View File

@@ -0,0 +1,27 @@
namespace Gameboard.ShogiUI.Sockets.Models
{
public class Move
{
public string PieceFromCaptured { get; set; }
public Coords From { get; set; }
public Coords To { get; set; }
public bool IsPromotion { get; set; }
public Move() { }
public Move(ServiceModels.Socket.Types.Move move)
{
From = Coords.FromBoardNotation(move.From);
To = Coords.FromBoardNotation(move.To);
PieceFromCaptured = move.PieceFromCaptured;
IsPromotion = move.IsPromotion;
}
public ServiceModels.Socket.Types.Move ToServiceModel() => new ServiceModels.Socket.Types.Move
{
From = From.ToBoardNotation(),
IsPromotion = IsPromotion,
PieceFromCaptured = PieceFromCaptured,
To = To.ToBoardNotation()
};
}
}