public virtual void OnUpdateGame(int x, int y)
 {
     if (this.IsLocalTurn())
     {
         if (this.m_MatchCount == 0)
         {
             this.m_MatchCount  = 1;
             this.m_MatchChess1 = this.m_MapChesses[x, y];
             this.m_MatchChess1.SetState(this.m_TurnIndex ? CChess.EChessState.RED : CChess.EChessState.BLUE, true);
         }
         else if (this.m_MatchCount == 1)
         {
             this.m_MatchCount  = 2;
             this.m_MatchChess2 = this.m_MapChesses[x, y];
             this.m_MatchChess2.SetState(this.m_TurnIndex ? CChess.EChessState.RED : CChess.EChessState.BLUE, true);
             this.m_Player.SendMatchingSpot(
                 this.m_MatchChess1.posX, this.m_MatchChess1.posY,
                 this.m_MatchChess2.posX, this.m_MatchChess2.posY
                 );
         }
     }
     else
     {
         this.m_Player.ShowMessage("This is not your turn.");
     }
     this.CheckTurn();
 }
示例#2
0
        public void Add(string umo)
        {
            CArrow arrow = new CArrow();

            arrow.color = color;
            CChess.UmoToSD(umo, out int sou, out int des);
            arrow.SetAB(sou, des);
            list.Add(arrow);
        }
示例#3
0
 public virtual bool CheckChess(CChess value, out List <CChess> results)
 {
     results = new List <CChess> ();
     if (value.chessState == CChess.EChessState.None)
     {
         return(false);
     }
     results = this.CheckChess(value);
     return(results != null && results.Count > 0);
 }
示例#4
0
        public static void ShowAttack(bool show, bool white)
        {
            List <int> ml = CChess.This.GenerateAllMoves(white, false);

            foreach (int m in ml)
            {
                int d = (m >> 8) & 0xff;
                int r = FormChess.chess.g_board[d] & 7;
                if (r > 0)
                {
                    if (show || (r == CChess.pieceKing))
                    {
                        int i = CChess.Con256To64(d);
                        arrField[i].attacked = true;
                    }
                }
            }
        }
    protected virtual void OnReceiveMatchingPosition(SocketIOEvent e)
    {
        var x1        = int.Parse(e.data.GetField("pos1X").ToString());
        var y1        = int.Parse(e.data.GetField("pos1Y").ToString());
        var x2        = int.Parse(e.data.GetField("pos2X").ToString());
        var y2        = int.Parse(e.data.GetField("pos2Y").ToString());
        var turnIndex = int.Parse(e.data.GetField("turnIndex").ToString());
        var result    = int.Parse(e.data.GetField("result").ToString()) == 1;
        var chess1    = this.m_MapChesses[x1, y1];
        var chess2    = this.m_MapChesses[x2, y2];

        this.m_TurnIndex = turnIndex == 1;
        chess1.SetState(this.m_TurnIndex ? CChess.EChessState.RED : CChess.EChessState.BLUE, true);
        chess2.SetState(this.m_TurnIndex ? CChess.EChessState.RED : CChess.EChessState.BLUE, true);
        chess1.UpdateResult(result);
        chess2.UpdateResult(result);
        this.m_MatchChess1 = null;
        this.m_MatchChess2 = null;
        this.m_MatchCount  = 0;
        this.AnimationYourTurn();
        this.CheckTurn();
    }
示例#6
0
    protected List <CChess> CheckNeighbours(CChess value)
    {
        var results = new List <CChess> ();

        if (value == null)
        {
            return(results);
        }
        var checkX = value.posX;
        var checkY = value.posY;

        // TOP == 1
        if (checkY - 1 >= 0)
        {
            var cell = this.m_MapChesses[checkX, checkY - 1];
            if (cell.chessState != CChess.EChessState.None)
            {
                results.Add(cell);
            }
        }
        // TOP RIGHT == 2
        if (checkX + 1 < this.m_MapColumn && checkY - 1 >= 0)
        {
            var cell = this.m_MapChesses[checkX + 1, checkY - 1];
            if (cell.chessState != CChess.EChessState.None)
            {
                results.Add(cell);
            }
        }
        // RIGHT == 3
        if (checkX + 1 < this.m_MapColumn)
        {
            var cell = this.m_MapChesses[checkX + 1, checkY];
            if (cell.chessState != CChess.EChessState.None)
            {
                results.Add(cell);
            }
        }
        // DOWN RIGHT == 4
        if (checkX + 1 < this.m_MapColumn && checkY + 1 < this.m_MapColumn)
        {
            var cell = this.m_MapChesses[checkX + 1, checkY + 1];
            if (cell.chessState != CChess.EChessState.None)
            {
                results.Add(cell);
            }
        }
        // DOWN == 5
        if (checkY + 1 < this.m_MapColumn)
        {
            var cell = this.m_MapChesses[checkX, checkY + 1];
            if (cell.chessState != CChess.EChessState.None)
            {
                results.Add(cell);
            }
        }
        // LEFT DOWN == 6
        if (checkX - 1 >= 0 && checkY + 1 < this.m_MapColumn)
        {
            var cell = this.m_MapChesses[checkX - 1, checkY + 1];
            if (cell.chessState != CChess.EChessState.None)
            {
                results.Add(cell);
            }
        }
        // LEFT == 7
        if (checkX - 1 >= 0)
        {
            var cell = this.m_MapChesses[checkX - 1, checkY];
            if (cell.chessState != CChess.EChessState.None)
            {
                results.Add(cell);
            }
        }
        // LEFT TOP == 8
        if (checkX - 1 >= 0 && checkY - 1 >= 0)
        {
            var cell = this.m_MapChesses[checkX - 1, checkY - 1];
            if (cell.chessState != CChess.EChessState.None)
            {
                results.Add(cell);
            }
        }
        return(results);
    }
示例#7
0
    public virtual List <CChess> CheckChess(CChess value)
    {
        if (value == null)
        {
            return(null);
        }
        if (value.chessState == CChess.EChessState.None)
        {
            return(null);
        }
        var results   = new List <CChess> ();
        var checkX    = value.posX;
        var checkY    = value.posY;
        var dimension = 1;

        while (dimension < 9)
        {
            switch (dimension)
            {
            // TOP == 1
            case 1:
                if (checkY - 1 >= 0)
                {
                    var cell = this.m_MapChesses[checkX, checkY - 1];
                    if (cell.chessState == value.chessState)
                    {
                        results.Add(cell);
                        if (results.Count >= this.m_CheckValue - 1)
                        {
                            return(results);
                        }
                        checkX = cell.posX;
                        checkY = cell.posY;
                    }
                    else
                    {
                        results.Clear();
                        dimension += 1;
                        checkX     = value.posX;
                        checkY     = value.posY;
                    }
                }
                else
                {
                    results.Clear();
                    dimension += 1;
                    checkX     = value.posX;
                    checkY     = value.posY;
                }
                break;

            // TOP RIGHT == 2
            case 2:
                if (checkX + 1 < this.m_MapColumn && checkY - 1 >= 0)
                {
                    var cell = this.m_MapChesses[checkX + 1, checkY - 1];
                    if (cell.chessState == value.chessState)
                    {
                        results.Add(cell);
                        if (results.Count >= this.m_CheckValue - 1)
                        {
                            return(results);
                        }
                        checkX = cell.posX;
                        checkY = cell.posY;
                    }
                    else
                    {
                        results.Clear();
                        dimension += 1;
                        checkX     = value.posX;
                        checkY     = value.posY;
                    }
                }
                else
                {
                    results.Clear();
                    dimension += 1;
                    checkX     = value.posX;
                    checkY     = value.posY;
                }
                break;

            // RIGHT == 3
            case 3:
                if (checkX + 1 < this.m_MapColumn)
                {
                    var cell = this.m_MapChesses[checkX + 1, checkY];
                    if (cell.chessState == value.chessState)
                    {
                        results.Add(cell);
                        if (results.Count >= this.m_CheckValue - 1)
                        {
                            return(results);
                        }
                        checkX = cell.posX;
                        checkY = cell.posY;
                    }
                    else
                    {
                        results.Clear();
                        dimension += 1;
                        checkX     = value.posX;
                        checkY     = value.posY;
                    }
                }
                else
                {
                    results.Clear();
                    dimension += 1;
                    checkX     = value.posX;
                    checkY     = value.posY;
                }
                break;

            // DOWN RIGHT == 4
            case 4:
                if (checkX + 1 < this.m_MapColumn && checkY + 1 < this.m_MapColumn)
                {
                    var cell = this.m_MapChesses[checkX + 1, checkY + 1];
                    if (cell.chessState == value.chessState)
                    {
                        results.Add(cell);
                        if (results.Count >= this.m_CheckValue - 1)
                        {
                            return(results);
                        }
                        checkX = cell.posX;
                        checkY = cell.posY;
                    }
                    else
                    {
                        results.Clear();
                        dimension += 1;
                        checkX     = value.posX;
                        checkY     = value.posY;
                    }
                }
                else
                {
                    results.Clear();
                    dimension += 1;
                    checkX     = value.posX;
                    checkY     = value.posY;
                }
                break;

            // DOWN == 5
            case 5:
                if (checkY + 1 < this.m_MapColumn)
                {
                    var cell = this.m_MapChesses[checkX, checkY + 1];
                    if (cell.chessState == value.chessState)
                    {
                        results.Add(cell);
                        if (results.Count >= this.m_CheckValue - 1)
                        {
                            return(results);
                        }
                        checkX = cell.posX;
                        checkY = cell.posY;
                    }
                    else
                    {
                        results.Clear();
                        dimension += 1;
                        checkX     = value.posX;
                        checkY     = value.posY;
                    }
                }
                else
                {
                    results.Clear();
                    dimension += 1;
                    checkX     = value.posX;
                    checkY     = value.posY;
                }
                break;

            // LEFT DOWN == 6
            case 6:
                if (checkX - 1 >= 0 && checkY + 1 < this.m_MapColumn)
                {
                    var cell = this.m_MapChesses[checkX - 1, checkY + 1];
                    if (cell.chessState == value.chessState)
                    {
                        results.Add(cell);
                        if (results.Count >= this.m_CheckValue - 1)
                        {
                            return(results);
                        }
                        checkX = cell.posX;
                        checkY = cell.posY;
                    }
                    else
                    {
                        results.Clear();
                        dimension += 1;
                        checkX     = value.posX;
                        checkY     = value.posY;
                    }
                }
                else
                {
                    results.Clear();
                    dimension += 1;
                    checkX     = value.posX;
                    checkY     = value.posY;
                }
                break;

            // LEFT == 7
            case 7:
                if (checkX - 1 >= 0)
                {
                    var cell = this.m_MapChesses[checkX - 1, checkY];
                    if (cell.chessState == value.chessState)
                    {
                        results.Add(cell);
                        if (results.Count >= this.m_CheckValue - 1)
                        {
                            return(results);
                        }
                        checkX = cell.posX;
                        checkY = cell.posY;
                    }
                    else
                    {
                        results.Clear();
                        dimension += 1;
                        checkX     = value.posX;
                        checkY     = value.posY;
                    }
                }
                else
                {
                    results.Clear();
                    dimension += 1;
                    checkX     = value.posX;
                    checkY     = value.posY;
                }
                break;

            // LEFT TOP == 8
            case 8:
                if (checkX - 1 >= 0 && checkY - 1 >= 0)
                {
                    var cell = this.m_MapChesses[checkX - 1, checkY - 1];
                    if (cell.chessState == value.chessState)
                    {
                        results.Add(cell);
                        if (results.Count >= this.m_CheckValue - 1)
                        {
                            return(results);
                        }
                        checkX = cell.posX;
                        checkY = cell.posY;
                    }
                    else
                    {
                        results.Clear();
                        dimension += 1;
                        checkX     = value.posX;
                        checkY     = value.posY;
                    }
                }
                else
                {
                    results.Clear();
                    dimension += 1;
                    checkX     = value.posX;
                    checkY     = value.posY;
                }
                break;
            }
        }
        return(results);
    }
示例#8
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");
        }
示例#9
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;
                }
            }
        }