42 lines
1.1 KiB
C#
42 lines
1.1 KiB
C#
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.
|
|
}
|
|
}
|
|
}
|