| | 1 | | using Rudim.Board; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace Rudim.CLI.UCI |
| | 7 | | { |
| | 8 | | public class UciClient : ICliCommand |
| | 9 | | { |
| | 10 | | private readonly Dictionary<string, IUciCommand> _commands; |
| | 11 | | public BoardState Board; |
| 8 | 12 | | private bool _debugMode = true; |
| 8 | 13 | | public ref bool DebugMode => ref _debugMode; |
| | 14 | |
|
| 8 | 15 | | public UciClient() |
| | 16 | | { |
| 8 | 17 | | GoCommand goCommand = new GoCommand(this); |
| 8 | 18 | | _commands = new Dictionary<string, IUciCommand> |
| 8 | 19 | | { |
| 8 | 20 | | ["isready"] = new IsReadyCommand(this), |
| 8 | 21 | | ["position"] = new PositionCommand(this), |
| 8 | 22 | | ["go"] = goCommand, |
| 8 | 23 | | ["stop"] = new StopCommand(goCommand), |
| 8 | 24 | | ["ucinewgame"] = new UciNewGameCommand(this), |
| 8 | 25 | | ["debug"] = new DebugCommand(this) |
| 8 | 26 | | }; |
| 8 | 27 | | Board = BoardState.Default(); |
| 8 | 28 | | } |
| | 29 | |
|
| | 30 | | public void Run(string[] parameters) |
| | 31 | | { |
| 0 | 32 | | WriteId(); |
| 0 | 33 | | while (true) |
| | 34 | | { |
| 0 | 35 | | string[] input = Console.ReadLine().Split(' '); |
| 0 | 36 | | string command = input[0]; |
| 0 | 37 | | string[] commandParameters = input.Skip(1).ToArray(); |
| | 38 | |
|
| 0 | 39 | | if (command == "quit") |
| | 40 | | { |
| 0 | 41 | | Environment.Exit(0); |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | if (_commands.ContainsKey(command)) |
| | 45 | | { |
| 0 | 46 | | _commands[command].Run(commandParameters); |
| | 47 | | } |
| | 48 | | else |
| | 49 | | { |
| 0 | 50 | | CliClient.WriteLine($"Unknown command {command}"); |
| | 51 | | } |
| | 52 | | } |
| | 53 | | } |
| | 54 | |
|
| | 55 | | private static void WriteId() |
| | 56 | | { |
| 0 | 57 | | CliClient.WriteLine("id name Rudim 1.4"); |
| 0 | 58 | | CliClient.WriteLine("id author Vishnu B"); |
| 0 | 59 | | CliClient.WriteLine("uciok"); |
| 0 | 60 | | } |
| | 61 | | } |
| | 62 | | } |