Skip to content

Commit

Permalink
- implemented better error handling and error recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
RonSijm committed Jan 6, 2023
1 parent fbf8a3f commit fedbe4d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
5 changes: 5 additions & 0 deletions RonSijm.ButtFish/Ascii/FENToCharArrayConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public static class FENToCharArrayConverter

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

if (piecePositionsArray.Length != 8)
{
return null;
}

for (var index = 0; index < piecePositionsArray.Length; index++)
{
var row = piecePositionsArray[index];
Expand Down
6 changes: 6 additions & 0 deletions RonSijm.ButtFish/Ascii/FENToOutputFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public static bool PaintBoard(string fenPosition)
try
{
var boardModel = fenPosition.ConvertToCharArray();

if (boardModel == null)
{
return false;
}

var whiteToModel = fenPosition.IsWhiteToMove();
var boardAscii = BoardToAscii.ToAscii(boardModel, whiteToModel);
AsciiToColorfulOutput.AsciiToConsole(boardAscii);
Expand Down
48 changes: 41 additions & 7 deletions RonSijm.ButtFish/ButtFishCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using RonSijm.ButtFish.Encoders;
using RonSijm.ButtFish.Models;
using RonSijm.UCIEngineInterop.Core;
using RonSijm.UCIEngineInterop.Exceptions;

namespace RonSijm.ButtFish;

Expand All @@ -21,10 +22,9 @@ public ButtFishCore(Options options, ICharacterEncoder characterEncoder)

public async Task Start()
{
Console.WriteLine("Welcome to Buttfish!");
Colorful.Console.WriteLine("Welcome to Buttfish!", Color.GreenYellow);
Console.WriteLine();
Console.WriteLine($"Using Encoder: {_characterEncoder.GetType().Name}");
Console.WriteLine("Checking engines");

var enginePath = EngineSelector.SelectEngine(_options.Engines);

Expand All @@ -41,22 +41,31 @@ public async Task Start()

_iuciEngine = new UCIEngine(enginePath.Path);

Console.WriteLine();
Console.WriteLine();

do
{
try
{
Console.WriteLine();
Colorful.Console.WriteLine("Set the current chess FEN Position...", Color.Green);
var fenPosition = Console.ReadLine();

_iuciEngine.StartNewGame();
_iuciEngine.SetFenPosition(fenPosition);
Console.WriteLine();
// If you just send a whitespace, the engine keeps waiting for the rest of the command
if (string.IsNullOrWhiteSpace(fenPosition))
{
Colorful.Console.WriteLine("You didn't have to press enter there...", Color.Orange);
continue;
}

var success = FENToOutputFacade.PaintBoard(fenPosition);

if (success)
{
_iuciEngine.StartNewGame();
_iuciEngine.SetFenPosition(fenPosition);
Console.WriteLine();

Console.WriteLine();
Console.WriteLine("Next Best Position:");

Expand All @@ -68,13 +77,38 @@ public async Task Start()
}

await SendNextMoveToDevice(nextPosition, device);
Console.WriteLine();
}
else
{
Colorful.Console.WriteLine("That doesn't look right to me...", Color.Red);
Colorful.Console.Write("Try Again - ", Color.Green);
}
}
// Error that happens when the chess-engine gets fucked up somehow.
catch(MaxTriesException e)
{
Colorful.Console.WriteLine("Error occurred.", Color.Red);
Colorful.Console.WriteLine(e, Color.Red);

Colorful.Console.WriteLine("Recycling chess engine...", Color.Green);
_iuciEngine = new UCIEngine(enginePath.Path);

// We don't automatically set the old FEN code, otherwise if you added a broken
// FEN code, the engine gets in a broken loop
Colorful.Console.WriteLine("Please try again", Color.Green);
}
catch (Exception e)
{
Console.WriteLine(); // Blank line because chess moves were not placed on a new line.
Colorful.Console.WriteLine("Error occurred.", Color.Red);
Colorful.Console.WriteLine(e, Color.Red);

if (!string.IsNullOrWhiteSpace(e.Source))
{
Colorful.Console.WriteLine($"Source:{e.Source}", Color.Red);
}

Console.WriteLine("Sometimes the program can recover, otherwise it's probably better to restart.");
}
} while (true);
Expand Down Expand Up @@ -119,6 +153,6 @@ private async Task SendNextMoveToDevice(string nextPosition, IDeviceAbstraction
}

Console.WriteLine();
Console.WriteLine("Finished sending command to device.");
Colorful.Console.WriteLine("Finished sending command to device.", Color.Green);
}
}
4 changes: 3 additions & 1 deletion RonSijm.ButtFish/DeviceDiscoveryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private static IDeviceAbstraction ListDiscoveredDevices(List<IDeviceAbstraction>
return null;
}

Console.WriteLine("Discovered the following devices:");
Console.WriteLine();
Colorful.Console.WriteLine("Discovered the following devices:", Color.Green);

for (var index = 0; index < discoveredDevices.Count; index++)
{
Expand All @@ -96,6 +97,7 @@ private static IDeviceAbstraction ListDiscoveredDevices(List<IDeviceAbstraction>
Console.WriteLine(deviceAbstraction is YeelightDevice ? $"{(index + 1)} - Yeelight device: {deviceAbstraction}" : $"{(index + 1)} - Butt device: {deviceAbstraction}");
}

Console.WriteLine();
Colorful.Console.WriteLine("Which device do you want to use?", Color.Green);
var deviceChoice = Console.ReadKey().Key.ToNumber();

Expand Down
3 changes: 2 additions & 1 deletion RonSijm.UCIEngineInterop/Core/Stockfish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private bool IsReady()
{
++tries;

if (UCIEngineProcess.ReadLine() == "readyok")
var engineReadLine = UCIEngineProcess.ReadLine();
if (engineReadLine == "readyok")
{
return true;
}
Expand Down

0 comments on commit fedbe4d

Please sign in to comment.