private void MainLoop(StreamWriter writer) { //string bestMove = ""; Option <PositionCommandDto> positionCommand = None; var command = Read(writer); if (!"uci".IsEqual(command)) { throw new Exception("Expected UCI protocol"); } Send(writer, $"id name Chess.AF"); Send(writer, $"id author Ardy Foolen"); Send(writer, "uciok"); var game = new Game(); GameAgent = Agent.Start <Action <Game> >(run => run(game)); //GameAgent.Tell(g => g.Load()); while (!"quit".IsEqual(command)) { command = Read(writer); switch (command) { case "isready": Send(writer, $"readyok"); break; case "ucinewgame": //GameAgent.Tell(g => g.Load()); break; case "stop": break; } if (command.StartsWith("position")) { positionCommand = PositionCommandDto.Of(command) .Map(PositionSetup); } //CreatePosition(command).Match( // Exception: ex => throw ex, // Success: _ => _); //if (command.StartsWith("position startpos moves")) //{ // bestMove = CreateBestMoves(command); //} if (command.StartsWith("go")) { //Send(bestMove); //bestMove = ""; } } }
private static void MakeMove(Game game, PositionCommandDto positionCommand) { var found = Find(game, positionCommand); var move = CreateMove(found); move.Map <Move, Unit>(m => game.Move(m)); }
private void ChangePosition(Game game, PositionCommandDto positionCommand) { LoadGame(game, positionCommand); MakeMove(game, positionCommand); //var myMove = CreateMove(game.AllMoves().FirstOrDefault()); var myMove = CreateMove(game.AllMoves().Where(w => w.Piece.Is(PieceEnum.King) && w.MoveSquare.Equals(SquareEnum.c1)).FirstOrDefault()); myMove = myMove.Match( None: () => CreateMove(game.AllMoves().FirstOrDefault()), Some: (m) => m); myMove.Map <Move, Unit>(m => game.Move(m)); SendBestMove(myMove); }
private static void LoadGame(Game game, PositionCommandDto positionCommand) { if (!game.IsLoaded) { if (positionCommand.IsStartPos) { game.Load(); } else { game.Load(positionCommand.Fen); } } }
private PositionCommandDto PositionSetup(PositionCommandDto positionCommand) { GameAgent.Tell(g => ChangePosition(g, positionCommand)); return(positionCommand); }
private static Option <(PieceEnum Piece, SquareEnum Square, PieceEnum Promoted, SquareEnum MoveSquare)> Find(Game game, PositionCommandDto positionCommand) { foreach (var tuple in game.AllMoves()) { if (positionCommand.LastFrom == Some(tuple.Square) && positionCommand.LastTo == Some(tuple.MoveSquare)) { return(Some(tuple)); } } return(None); }