示例#1
0
        static void Main(string[] args)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            bool          getMove  = true;
            List <string> movesEng = new List <string>();
            CChess        Chess    = new CChess();
            CUci          Uci      = new CUci();
            string        ax       = "-ef";
            List <string> listEf   = new List <string>();
            List <string> listEa   = new List <string>();

            for (int n = 0; n < args.Length; n++)
            {
                string ac = args[n];
                switch (ac)
                {
                case "-ef":
                case "-ea":
                    ax = ac;
                    break;

                default:
                    switch (ax)
                    {
                    case "-ef":
                        listEf.Add(ac);
                        break;

                    case "-ea":
                        listEa.Add(ac);
                        break;
                    }
                    break;
                }
            }
            string  script        = "http://www.chessdb.cn/cdb.php";
            string  engineFile    = String.Join(" ", listEf);
            string  arguments     = String.Join(" ", listEa);
            Process engineProcess = null;

            if (File.Exists(engineFile))
            {
                engineProcess = new Process();
                engineProcess.StartInfo.FileName              = engineFile;
                engineProcess.StartInfo.WorkingDirectory      = Path.GetDirectoryName(engineFile);
                engineProcess.StartInfo.UseShellExecute       = false;
                engineProcess.StartInfo.RedirectStandardInput = true;
                engineProcess.StartInfo.Arguments             = arguments;
                engineProcess.Start();
                Console.WriteLine($"info string engine on");
            }
            else
            {
                if (engineFile != String.Empty)
                {
                    Console.WriteLine($"info string missing engine  [{engineFile}]");
                }
                engineFile = String.Empty;
            }
            if (script == "")
            {
                Console.WriteLine("info string missing script");
                return;
            }

            string GetMove(string fen)
            {
                var col = new NameValueCollection
                {
                    { "action", "querybest" },
                    { "board", fen }
                };

                byte[] data;
                try
                {
                    data = new WebClient().UploadValues(@script, col);
                }
                catch
                {
                    return("");
                }
                string msg = Encoding.UTF8.GetString(data);

                string[] tokens = msg.Split(':');
                if (tokens.Length > 1)
                {
                    string umo = tokens[1].Substring(0, 4);
                    if (Chess.IsValidMove(umo, out _))
                    {
                        return(umo);
                    }
                }
                return("");
            }

            do
            {
                string msg = Console.ReadLine();
                Uci.SetMsg(msg);
                if ((Uci.command != "go") && (engineFile != ""))
                {
                    engineProcess.StandardInput.WriteLine(msg);
                }
                switch (Uci.command)
                {
                case "ucinewgame":
                    getMove = true;
                    break;

                case "position":
                    string fen   = Uci.GetValue("fen", "moves");
                    string moves = Uci.GetValue("moves", "fen");
                    Chess.SetFen(fen);
                    Chess.MakeMoves(moves);
                    break;

                case "go":
                    string move = "";
                    if (getMove)
                    {
                        move    = GetMove(Chess.GetFen());
                        getMove = move != "";
                    }
                    if (getMove)
                    {
                        Console.WriteLine($"bestmove {move}");
                    }
                    else if (engineFile == "")
                    {
                        Console.WriteLine("enginemove");
                    }
                    else
                    {
                        engineProcess.StandardInput.WriteLine(msg);
                    }
                    break;
                }
            } while (Uci.command != "quit");
        }
示例#2
0
        static void Main()
        {
            string version = "2021-07-22";
            CUci   Uci     = new CUci();
            CChess Chess   = new CChess();

            while (true)
            {
                string msg = Console.ReadLine();
                Uci.SetMsg(msg);
                switch (Uci.command)
                {
                case "uci":
                    Console.WriteLine($"id name Rapcschess {version}");
                    Console.WriteLine("id author Thibor Raven");
                    Console.WriteLine("id link https://github.com/Thibor/RapChessCs");
                    Console.WriteLine("option name MultiPV type spin default 1 min 1 max 100");
                    Console.WriteLine("option name SkillLevel type spin default 100 min 0 max 100");
                    Console.WriteLine("uciok");
                    break;

                case "isready":
                    Console.WriteLine("readyok");
                    break;

                case "setoption":
                    switch (Uci.GetStr("name", ""))
                    {
                    case "MultiPV":
                        Chess.multiPv = Uci.GetInt("value", 1);
                        break;

                    case "SkillLevel":
                        int level = Uci.GetInt("value", 100);
                        Chess.FillBonPosition(level);
                        break;
                    }
                    break;

                case "position":
                    string fen = "";
                    int    lo  = Uci.GetIndex("fen", 0);
                    int    hi  = Uci.GetIndex("moves", Uci.tokens.Length);
                    if (lo > 0)
                    {
                        if (lo > hi)
                        {
                            hi = Uci.tokens.Length;
                        }
                        for (int n = lo; n < hi; n++)
                        {
                            if (n > lo)
                            {
                                fen += ' ';
                            }
                            fen += Uci.tokens[n];
                        }
                    }
                    Chess.SetFen(fen);
                    lo = Uci.GetIndex("moves", 0);
                    hi = Uci.GetIndex("fen", Uci.tokens.Length);
                    if (lo > 0)
                    {
                        if (lo > hi)
                        {
                            hi = Uci.tokens.Length;
                        }
                        for (int n = lo; n < hi; n++)
                        {
                            string m = Uci.tokens[n];
                            Chess.MakeMove(Chess.UmoToEmo(m));
                            if (Chess.g_move50 == 0)
                            {
                                Chess.undoIndex = 0;
                            }
                        }
                    }
                    break;

                case "go":
                    Chess.stopwatch.Restart();
                    int time     = Uci.GetInt("movetime", 0);
                    int depth    = Uci.GetInt("depth", 0);
                    int node     = Uci.GetInt("nodes", 0);
                    int infinite = Uci.GetIndex("infinite", 0);
                    if ((time == 0) && (depth == 0) && (node == 0) && (infinite == 0))
                    {
                        double ct = Chess.whiteTurn ? Uci.GetInt("wtime", 0) : Uci.GetInt("btime", 0);
                        double mg = Uci.GetInt("movestogo", 0x40);
                        time = Convert.ToInt32(ct / mg);
                        if (time < 1)
                        {
                            time = 1;
                        }
                    }
                    Chess.StartThread(depth, time, node);
                    break;

                case "stop":
                    Chess.synStop.SetStop(true);
                    break;

                case "quit":
                    Chess.synStop.SetStop(true);
                    return;
                }
            }
        }