Skip to content

Commit

Permalink
implemented FEN export + added to "pretty print" of board
Browse files Browse the repository at this point in the history
  • Loading branch information
sictransit committed Nov 4, 2022
1 parent e92500c commit 3dddbd1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 5 deletions.
7 changes: 2 additions & 5 deletions Common/Extensions/BoardExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SicTransit.Woodpusher.Common.Interfaces;
using SicTransit.Woodpusher.Common.Parsing;
using SicTransit.Woodpusher.Model;
using SicTransit.Woodpusher.Model.Enums;
using SicTransit.Woodpusher.Model.Extensions;
Expand Down Expand Up @@ -41,11 +42,7 @@ public static string PrettyPrint(this IBoard b)
sb.AppendLine();

sb.AppendLine($"Hash: {b.Hash}");
sb.AppendLine($"Castlings: {b.Counters.Castlings}");
var target = b.Counters.EnPassantTarget == 0 ? "None" : b.Counters.EnPassantTarget.ToSquare().ToString();
sb.AppendLine($"En passant target: {target}");
var activeColor = b.ActiveColor == Piece.White ? "White" : "Black";
sb.AppendLine($"{activeColor} to play");
sb.AppendLine($"FEN: {ForsythEdwardsNotation.Export(b)}");

return sb.ToString();
}
Expand Down
77 changes: 77 additions & 0 deletions Common/Parsing/ForsythEdwardsNotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using SicTransit.Woodpusher.Model;
using SicTransit.Woodpusher.Model.Enums;
using SicTransit.Woodpusher.Model.Extensions;
using System.Text;
using System.Text.RegularExpressions;

namespace SicTransit.Woodpusher.Common.Parsing
Expand Down Expand Up @@ -91,6 +92,82 @@ private static (Bitboard, Bitboard) ParseBoard(string s)
return (white, black);
}

public static string Export(IBoard board)
{
var parts = new List<string>();

var boardParts = new List<string>();

var pieces = board.GetPieces().ToDictionary(p => p.GetSquare(), p => p);

for (var rank = 7; rank >= 0; rank--)
{
var emptySquares = 0;

var fileBuilder = new StringBuilder();

for (int file = 0; file < 8; file++)
{
if (pieces.TryGetValue(new Square(file, rank), out var piece))
{
if (emptySquares != 0)
{
fileBuilder.Append(emptySquares);
emptySquares = 0;
}

fileBuilder.Append(piece.ToAlgebraicNotation());
}
else
{
emptySquares++;
}
}

if (emptySquares != 0)
{
fileBuilder.Append(emptySquares);
}

boardParts.Add(fileBuilder.ToString());
}

parts.Add(string.Join('/', boardParts));

parts.Add(board.ActiveColor.Is(Piece.White) ? "w" : "b");

var castlings = new[] {
(Castlings.WhiteKingside, Piece.White | Piece.King),
(Castlings.WhiteQueenside, Piece.White | Piece.Queen),
(Castlings.BlackKingside, Piece.None | Piece.King),
(Castlings.BlackQueenside, Piece.None | Piece.Queen)
}.Where(c => board.Counters.Castlings.HasFlag(c.Item1)).Select(c => c.Item2.ToAlgebraicNotation()).ToArray();

if (castlings.Length == 0)
{
parts.Add("-");
}
else
{
parts.Add(new string(castlings));
}

if (board.Counters.EnPassantTarget == 0)
{
parts.Add("-");
}
else
{
parts.Add(board.Counters.EnPassantTarget.ToSquare().ToAlgebraicNotation());
}

parts.Add(board.Counters.HalfmoveClock.ToString());
parts.Add(board.Counters.FullmoveNumber.ToString());

return string.Join(" ", parts.ToArray());
}


private static int ParseFullmoveNumber(string s) => int.Parse(s);

private static int ParseHalfmoveClock(string s) => int.Parse(s);
Expand Down
33 changes: 33 additions & 0 deletions CommonTests/Parsing/ForsythEdwardsNotationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Serilog;
using SicTransit.Woodpusher.Common.Extensions;
using SicTransit.Woodpusher.Common.Parsing;
using SicTransit.Woodpusher.Model;
Expand All @@ -11,6 +12,12 @@ namespace SicTransit.Woodpusher.Common.Tests.Parsing
[TestClass()]
public class ForsythEdwardsNotationTests
{
[TestInitialize]
public void Initialize()
{
Logging.EnableUnitTestLogging(Serilog.Events.LogEventLevel.Information);
}

[TestMethod]
public void ParseSetupTest()
{
Expand Down Expand Up @@ -43,5 +50,31 @@ public void ParseMagnusCarlsenTest()
Assert.AreEqual(0, board.Counters.HalfmoveClock);
Assert.AreEqual(34, board.Counters.FullmoveNumber);
}

[TestMethod()]
public void ExportStartingPositionTest()
{
var board = ForsythEdwardsNotation.Parse(ForsythEdwardsNotation.StartingPosition);

var export = ForsythEdwardsNotation.Export(board);

Log.Information(export);

Assert.AreEqual(ForsythEdwardsNotation.StartingPosition, export);
}

[TestMethod()]
public void ExportMagnusCarlsenTest()
{
var fen = "5r2/2Q2n2/5k2/7r/P3P1p1/1B6/5P2/6K1 b - a3 0 34";

var board = ForsythEdwardsNotation.Parse(fen);

var export = ForsythEdwardsNotation.Export(board);

Log.Information(export);

Assert.AreEqual(fen, export);
}
}
}

0 comments on commit 3dddbd1

Please sign in to comment.