Skip to content

Commit

Permalink
- Implemented Hikaru Optimization: Option to only receive end positi…
Browse files Browse the repository at this point in the history
…on and not start position (thought config)
  • Loading branch information
RonSijm committed Sep 29, 2022
1 parent eb9680d commit 25968e9
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 28 deletions.
13 changes: 10 additions & 3 deletions RonSijm.ButtFish/ButtFishCore.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using System.Drawing;
using RonSijm.ButtFish.Encoders;
using RonSijm.ButtFish.Models;
using RonSijm.ButtFish.Morse;
using Stockfish.NET;
using Stockfish.NET.Core;

namespace RonSijm.ButtFish;

public class ButtFishCore
{
private IStockfish _stockfish;

private readonly Options _options;
private readonly ICharacterEncoder _characterEncoder;

public ButtFishCore(ICharacterEncoder characterEncoder)
public ButtFishCore(Options options, ICharacterEncoder characterEncoder)
{
_options = options;
_characterEncoder = characterEncoder;
}

Expand Down Expand Up @@ -61,6 +63,11 @@ public async Task Start()

var nextPosition = _stockfish.GetBestMove();

if (_options.EndPositionOnly)
{
nextPosition = nextPosition.Substring(2, 2);
}

await SendNextMoveToDevice(nextPosition, device);
}
}
Expand Down
2 changes: 1 addition & 1 deletion RonSijm.ButtFish/Encoders/MorseConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RonSijm.ButtFish.Morse;
namespace RonSijm.ButtFish.Encoders;

/// <summary>
/// *Morse Code timing rules*
Expand Down
19 changes: 19 additions & 0 deletions RonSijm.ButtFish/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace RonSijm.ButtFish
{
public class Options
{
/// <summary>
/// Option to indicate that you don't want to transmit the start position,
/// And only want to transmit the end position
/// </summary>
public bool EndPositionOnly { get; set; }

/// <summary>
/// Option to indicate which encoder you want to use.
/// Current available:
/// - MorseEncoder (Default)
/// - SimplifiedPulse
/// </summary>
public string Encoder { get; set; }
}
}
18 changes: 5 additions & 13 deletions RonSijm.ButtFish/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,20 @@ public static async Task Main(string[] args)
Colorful.Console.WriteAscii("ButtFish", Color.FromArgb(0, 212, 255));
Console.WriteLine();

var encoder = GetCharacterEncoder();
var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true).Build();
var options = builder.Get<Options>();

ICharacterEncoder encoder = options.Encoder is not "SimplifiedPulse" ? new MorseEncoder() : new SimplifiedPulseEncoder();

var serviceProvider = new ServiceCollection()
.AddSingleton<ButtFishCore>()
.AddSingleton(encoder)
.AddSingleton(options)
.BuildServiceProvider();

var core = serviceProvider.GetRequiredService<ButtFishCore>();
await core.Start();

Console.ReadKey();
}

private static ICharacterEncoder GetCharacterEncoder()
{
var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true).Build();
var encoderConfig = builder["Encoder"];

ICharacterEncoder encoder = null;

encoder = encoderConfig is not "SimplifiedPulse" ? new MorseEncoder() : new SimplifiedPulseEncoder();

return encoder;
}
}
1 change: 1 addition & 0 deletions RonSijm.ButtFish/RonSijm.ButtFish.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Buttplug" Version="2.0.6" />
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="YeelightAPI" Version="1.10.2" />
Expand Down
3 changes: 2 additions & 1 deletion RonSijm.ButtFish/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"Encoder": "MorseEncoder"
"Encoder": "MorseEncoder",
"EndPositionOnly": false
}
5 changes: 2 additions & 3 deletions Stockfish.NET/Core/IStockfish.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using Stockfish.NET.Models;
using Stockfish.NET.Models;

namespace Stockfish.NET
namespace Stockfish.NET.Core
{
public interface IStockfish
{
Expand Down
4 changes: 1 addition & 3 deletions Stockfish.NET/Core/StockfishProcess.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace Stockfish.NET
namespace Stockfish.NET.Core
{
internal class StockfishProcess
{
Expand Down
11 changes: 7 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ As you can see by the diagram, if we'd have to wait for "the chosen one" with in
#### Encoders

- [x] (default) [A text to Morse Encoder](https://github.com/RonSijm/ButtFish/blob/main/RonSijm.ButtFish/Encoders/MorseEncoder.cs)
- [x] [A simplified Pulse Encoder](https://github.com/RonSijm/ButtFish/blob/main/RonSijm.ButtFish/Encoders/SimplifiedPulseEncoder.cs) - you can change this in `appsettings.json` by using "SimplifiedPulse"
- [x] [A simplified Pulse Encoder](https://github.com/RonSijm/ButtFish/blob/main/RonSijm.ButtFish/Encoders/SimplifiedPulseEncoder.cs)
- `appsettings.json` -> Encoder -> "SimplifiedPulse"

#### Config

- [x] End Position Only [Expert] By just knowing the end position, a serious chess player would know intuitively which piece belongs there. ~ [Hikaru](https://youtu.be/ifJnWVSoyAY?t=431)
- `appsettings.json` -> EndPositionOnly -> true

* * *

Expand Down Expand Up @@ -155,9 +161,6 @@ NASA and I have different priorities I suppose...
- [Sohu.com](https://www.sohu.com/a/588661192_610300) covered it in Chinese [[Google translate English](https://www-sohu-com.translate.goog/a/588661192_610300?_x_tr_sl=zh-CN&_x_tr_tl=en&_x_tr_hl=en)]
![Sohucom2](https://user-images.githubusercontent.com/337928/193144038-f80dddbb-afd9-40ab-86db-7bff14a20494.png)




### Library and Mentions (outgoing)

- Buttplug.io main website: https://buttplug.io
Expand Down

0 comments on commit 25968e9

Please sign in to comment.