| | | 1 | | using Rudim.Board; |
| | | 2 | | using Rudim.Common; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | namespace Rudim.CLI.UCI |
| | | 7 | | { |
| | 11 | 8 | | public class PositionCommand(UciClient uciClient) : IUciCommand |
| | | 9 | | { |
| | | 10 | | public void Run(string[] parameters) |
| | | 11 | | { |
| | 3 | 12 | | string position = parameters[0]; |
| | 3 | 13 | | List<string> positionParameters = parameters.Skip(1).ToList(); |
| | | 14 | | |
| | 3 | 15 | | if (position == "startpos") |
| | | 16 | | { |
| | 2 | 17 | | List<string> moves = positionParameters.Skip(1).ToList(); |
| | 2 | 18 | | ParseStartPos(moves); |
| | | 19 | | } |
| | | 20 | | |
| | 3 | 21 | | if (position == "fen") |
| | | 22 | | { |
| | 1 | 23 | | string fen = string.Join(" ", positionParameters.Take(6)); |
| | 1 | 24 | | List<string> moves = positionParameters.Skip(7).ToList(); |
| | 1 | 25 | | ParseFen(fen, moves); |
| | | 26 | | } |
| | 3 | 27 | | } |
| | | 28 | | |
| | | 29 | | private void ParseFen(string fen, IList<string> moves) |
| | | 30 | | { |
| | 1 | 31 | | Global.Reset(); |
| | 1 | 32 | | uciClient.Board = BoardState.ParseFEN(fen); |
| | 1 | 33 | | ParseMoves(moves); |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | private void ParseStartPos(IList<string> moves) |
| | | 37 | | { |
| | 2 | 38 | | Global.Reset(); |
| | 2 | 39 | | uciClient.Board = BoardState.Default(); |
| | 2 | 40 | | ParseMoves(moves); |
| | 2 | 41 | | } |
| | | 42 | | |
| | | 43 | | private void ParseMoves(IList<string> moves) |
| | | 44 | | { |
| | 10 | 45 | | foreach (string moveString in moves) |
| | | 46 | | { |
| | 2 | 47 | | Move move = Move.ParseLongAlgebraic(moveString); |
| | 2 | 48 | | move = FindMoveFromMoveList(move); |
| | | 49 | | // Todo : Check move is valid before making move |
| | 2 | 50 | | if (move == Move.NoMove) |
| | | 51 | | { |
| | 0 | 52 | | CliClient.WriteLine("Invalid Move"); |
| | 0 | 53 | | return; |
| | | 54 | | } |
| | 2 | 55 | | uciClient.Board.MakeMove(move); |
| | | 56 | | } |
| | 3 | 57 | | } |
| | | 58 | | |
| | | 59 | | private Move FindMoveFromMoveList(Move move) |
| | | 60 | | { |
| | 2 | 61 | | uciClient.Board.GenerateMoves(); |
| | 2 | 62 | | List<Move> moves = uciClient.Board.Moves; |
| | 40 | 63 | | for (int i = 0; i < moves.Count; ++i) |
| | | 64 | | { |
| | 20 | 65 | | if (moves[i].Source == move.Source && moves[i].Target == move.Target) |
| | | 66 | | { |
| | | 67 | | // Cool little trick I saw on Cosette - requires keeping enums on certain numbers |
| | | 68 | | // So that the we they differ by only one bit |
| | | 69 | | // Refactor this concept better maybe - hardcoded in the enum and here for now |
| | 2 | 70 | | if (move.Type == MoveTypes.Quiet || ((byte)moves[i].Type.Value & ~8) == (byte)move.Type.Value) |
| | | 71 | | { |
| | 2 | 72 | | return moves[i]; |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | } |
| | 0 | 76 | | return Move.NoMove; |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | } |