Skip to content

Commit

Permalink
- Implemented an inverted ascii board when black is to move
Browse files Browse the repository at this point in the history
  • Loading branch information
RonSijm committed Jan 4, 2023
1 parent 87198f0 commit fbf8a3f
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 65 deletions.
56 changes: 45 additions & 11 deletions RonSijm.ButtFish.Tests/FENToAsciiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,66 @@ namespace RonSijm.ButtFish.Tests;
public class FENToAsciiTest
{
[Fact]
public void Should_Be_Able_To_Convert_Board_To_Ascii()
public void Should_Be_Able_To_Convert_White_Board_To_Ascii()
{
var fenCode = "rnb1kbnr/pppp1ppp/8/4p1q1/5P2/4PQ2/PPPP2PP/RNB1KBNR b KQkq - 2 3";
var fenCode = "k7/q1pppp2/p1bP4/r1n1P3/n1r2P1P/b5Pp/P4RpP/RNBQKBNp w Q - 0 1";
var boardModel = fenCode.ConvertToCharArray();
var boardAscii = BoardToAscii.ToAscii(boardModel);
var whiteToModel = fenCode.IsWhiteToMove();

var boardAscii = BoardToAscii.ToAscii(boardModel, whiteToModel);

var expected = @" ╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗
8 ║ R │ N │ B │   │ K │ B │ N │ R ║
8 ║ k │   │   │   │   │   │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
7 ║ P │ P │ P │ P │   │   │ P │ P ║
7 ║ q │   │ p │ p │ p │ p │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
6 ║   │   │   │   │ P │ Q │   │   ║
6 ║ p │   │ b │ P │   │   │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
5 ║   │   │   │   │   │ P │   │   ║
5 ║ r │   │ n │   │ P │   │   │   ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
4 ║   │   │   │   │ p │   │ q │   ║
4 ║ n │   │ r │   │   │ P │   │ P ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
3 ║   │   │   │   │   │   │   │   ║
3 ║ b │   │   │   │   │   │ P │ p ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
2 ║ p │ p │ p │ p │   │ p │ p │ p ║
2 ║ P │   │   │   │   │ R │ p │ P ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
1 ║ r │ n │ b │   │ k │ b │ n │ r ║
1 ║ R │ N │ B │ Q │ K │ B │ N │ p ║
╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝
a b c d e f g h
";

boardAscii.Should().Be(expected);
}

[Fact]
public void Should_Be_Able_To_Convert_Black_Board_To_Ascii()
{
var fenCode = "k7/q1pppp2/p1bP4/r1n1P3/n1r2P1P/b5Pp/P4RpP/RNBQKBNp b Q - 0 1";
var boardModel = fenCode.ConvertToCharArray();
var whiteToModel = fenCode.IsWhiteToMove();

var boardAscii = BoardToAscii.ToAscii(boardModel, whiteToModel);

var expected = @" ╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗
1 ║ p │ N │ B │ K │ Q │ B │ N │ R ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
2 ║ P │ p │ R │   │   │   │   │ P ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
3 ║ p │ P │   │   │   │   │   │ b ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
4 ║ P │   │ P │   │   │ r │   │ n ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
5 ║   │   │   │ P │   │ n │   │ r ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
6 ║   │   │   │   │ P │ b │   │ p ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
7 ║   │   │ p │ p │ p │ p │   │ q ║
╟───┼───┼───┼───┼───┼───┼───┼───╢
8 ║   │   │   │   │   │   │   │ k ║
╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝
h g f e d c b a
";

boardAscii.Should().Be(expected);
}
}
1 change: 1 addition & 0 deletions RonSijm.ButtFish.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FEN/@EntryIndexedValue">FEN</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MS/@EntryIndexedValue">MS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PV/@EntryIndexedValue">PV</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UCI/@EntryIndexedValue">UCI</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Buttfish/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Buttplug/@EntryIndexedValue">True</s:Boolean>
Expand Down
2 changes: 1 addition & 1 deletion RonSijm.ButtFish/Ascii/AsciiToColerfulOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RonSijm.ButtFish.Ascii;

public static class AsciiToColerfulOutput
public static class AsciiToColorfulOutput
{
public static void AsciiToConsole(string ascii)
{
Expand Down
16 changes: 9 additions & 7 deletions RonSijm.ButtFish/Ascii/BoardToAscii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ namespace RonSijm.ButtFish.Ascii;

public static class BoardToAscii
{
public static string ToAscii(char[,] model)
public static string ToAscii(char[,] model, bool isWhiteToMove)
{
var lastLoop = isWhiteToMove ? 7 : 0;

var bob = new StringBuilder();
bob.AppendLine(" ╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗");

for (var horizontal = 8 - 1; horizontal >= 0; horizontal--)
foreach (var horizontal in Loop.Between(0, 8, !isWhiteToMove))
{
bob.Append(" " + (horizontal + 1) + "");
bob.Append(" " + (8 - horizontal) + "");

for (var vertical = 0; vertical < 8; vertical++)
foreach (var vertical in Loop.Between(0, 8, !isWhiteToMove))
{
bob.Append(' ');
bob.Append(model[horizontal, vertical]);

if (vertical != 7)
if (vertical != lastLoop)
{
bob.Append(" │");
}
Expand All @@ -30,12 +32,12 @@ public static string ToAscii(char[,] model)

bob.AppendLine("");

bob.AppendLine(horizontal != 0
bob.AppendLine(horizontal != lastLoop
? " ╟───┼───┼───┼───┼───┼───┼───┼───╢"
: " ╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝");
}

bob.AppendLine(" a b c d e f g h ");
bob.AppendLine(isWhiteToMove ? " a b c d e f g h " : " h g f e d c b a ");

return bob.ToString();
}
Expand Down
19 changes: 18 additions & 1 deletion RonSijm.ButtFish/Ascii/FENToCharArrayConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public static class FENToCharArrayConverter
public static char[,] ConvertToCharArray(this string fenCode)
{
var result = new char[8, 8];
var fenLineSplit = fenCode.Split(' ');

var piecePositionsArray = ReplaceDigitsWithEmptyStrings(fenCode.Split(' ')[0]).Split('/');
var piecePositionsArray = ReplaceDigitsWithEmptyStrings(fenLineSplit[0]).Split('/');

for (var index = 0; index < piecePositionsArray.Length; index++)
{
Expand All @@ -28,6 +29,22 @@ public static class FENToCharArrayConverter
return result;
}

public static bool IsWhiteToMove(this string fenCode)
{
var fenLineSplit = fenCode.Split(' ');
if (fenLineSplit[1] == "w")
{
return true;
}

if (fenLineSplit[1] == "b")
{
return false;
}

throw new ArgumentOutOfRangeException();
}

private static string ReplaceDigitsWithEmptyStrings(string position)
{
var isMatch = DigitsRegex.Match(position);
Expand Down
7 changes: 4 additions & 3 deletions RonSijm.ButtFish/Ascii/FENToOutputFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace RonSijm.ButtFish.Ascii;

public class FENToOutputFacade
public static class FENToOutputFacade
{
public static bool PaintBoard(string fenPosition)
{
try
{
var boardModel = fenPosition.ConvertToCharArray();
var boardAscii = BoardToAscii.ToAscii(boardModel);
AsciiToColerfulOutput.AsciiToConsole(boardAscii);
var whiteToModel = fenPosition.IsWhiteToMove();
var boardAscii = BoardToAscii.ToAscii(boardModel, whiteToModel);
AsciiToColorfulOutput.AsciiToConsole(boardAscii);

return true;
}
Expand Down
23 changes: 23 additions & 0 deletions RonSijm.ButtFish/Ascii/LoopHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace RonSijm.ButtFish.Ascii
{
internal static class Loop
{
internal static IEnumerable<int> Between(int minValue, int maxValue, bool inverted)
{
if (!inverted)
{
for (var i = minValue; i < maxValue; i++)
{
yield return i;
}
}
else
{
for (var i = maxValue - 1; i >= minValue; i--)
{
yield return i;
}
}
}
}
}
11 changes: 2 additions & 9 deletions RonSijm.ButtFish/Connectors/ButtplugConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@ public class ButtplugConnector

public async Task<List<IDeviceAbstraction>> GetDiscoveredDevices(string discoverAddress = null)
{
if (discoverAddress == null)
{
Console.WriteLine("Starting to scan for ButtPlug Devices...");
}
else
{
Console.WriteLine($"Attempting to connect to {discoverAddress}...");
}

Console.WriteLine(discoverAddress == null ? "Starting to scan for ButtPlug Devices..." : $"Attempting to connect to {discoverAddress}...");

Console.WriteLine("Will return results after 5 seconds.");

if (discoverAddress == null)
Expand Down
9 changes: 1 addition & 8 deletions RonSijm.ButtFish/DeviceDiscoveryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,7 @@ private static IDeviceAbstraction ListDiscoveredDevices(List<IDeviceAbstraction>
{
var deviceAbstraction = discoveredDevices[index];

if (deviceAbstraction is YeelightDevice)
{
Console.WriteLine($"{(index + 1)} - Yeelight device: {deviceAbstraction}");
}
else
{
Console.WriteLine($"{(index + 1)} - Butt device: {deviceAbstraction}");
}
Console.WriteLine(deviceAbstraction is YeelightDevice ? $"{(index + 1)} - Yeelight device: {deviceAbstraction}" : $"{(index + 1)} - Butt device: {deviceAbstraction}");
}

Colorful.Console.WriteLine("Which device do you want to use?", Color.Green);
Expand Down
6 changes: 3 additions & 3 deletions RonSijm.ButtFish/Models/ButtDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace RonSijm.ButtFish.Models;

public class ButtDevice : IDeviceAbstraction
{
public string Name { get; set; }
private readonly string _name;

private readonly ButtplugClientDevice _device;

public ButtDevice(ButtplugClientDevice device)
{
_device = device;
Name = _device.Name;
_name = _device.Name;
}

public async Task SendDuration(int time)
Expand All @@ -28,6 +28,6 @@ public void Dispose()

public override string ToString()
{
return Name;
return _name;
}
}
4 changes: 2 additions & 2 deletions RonSijm.UCIEngineInterop/Core/Stockfish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class UCIEngine : IUCIEngine

private int _skillLevel;

private UCIEngineProcess UCIEngineProcess { get; set; }
private UCIEngineProcess UCIEngineProcess { get; }

public Settings Settings { get; set; }
public Settings Settings { get; }

public int Depth { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions RonSijm.UCIEngineInterop/Core/UCIEngineProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace RonSijm.UCIEngineInterop.Core;

internal class UCIEngineProcess : IDisposable
{
private ProcessStartInfo ProcessStartInfo { get; set; }
private Process Process { get; set; }
private ProcessStartInfo ProcessStartInfo { get; }
private Process Process { get; }

public UCIEngineProcess(string path)
{
Expand Down
25 changes: 7 additions & 18 deletions RonSijm.UCIEngineInterop/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@ namespace RonSijm.UCIEngineInterop.Models;

public class Settings
{
public int Contempt { get; set; }
public int Threads { get; set; }
public bool Ponder { get; set; }
public int MultiPV { get; set; }
public int Threads { get; }
public bool Ponder { get; }
public int MultiPV { get; }
public int SkillLevel { get; set; }
public int MoveOverhead { get; set; }
public int SlowMover { get; set; }
public bool UCIChess960 { get; set; }
public int MoveOverhead { get; }
public int SlowMover { get; }
public bool UCIChess960 { get; }

public Settings(
int contempt = 0,
int threads = 0,
bool ponder = false,
int multiPV = 1,
int skillLevel = 20,
int moveOverhead = 30,
int slowMover = 80,
bool uciChess960 = false
)
public Settings(int threads = 0, bool ponder = false, int multiPV = 1, int skillLevel = 20, int moveOverhead = 30, int slowMover = 80, bool uciChess960 = false)
{
Contempt = contempt;
Ponder = ponder;
Threads = threads;
MultiPV = multiPV;
Expand Down

0 comments on commit fbf8a3f

Please sign in to comment.