Пример #1
0
        public chessBasic canMoveRoute(int x, int y, chessSquare cq)
        {
            chessBasic cb = new chessBasic();

            int[,] dir = { { -1,  2 }, {  1, -2 },
                           { -1, -2 }, { -2,  1 },
                           {  2, -1 }, { -2, -1 },
                           {  1,  2 }, {  2,  1 } };
            for (int i = 0; i < 8; i++)//騎士的8個走法
            {
                int X = x + dir[i, 0];
                int Y = y + dir[i, 1];
                if (X >= 0 && X < 8 && Y >= 0 && Y < 8)//棋盤內
                {
                    if ((cq.PSquare[x, y] == chessSquare.chessSides.Black && cq.PSquare[X, Y] == chessSquare.chessSides.Black) ||
                        (cq.PSquare[x, y] == chessSquare.chessSides.White && cq.PSquare[X, Y] == chessSquare.chessSides.White))
                    {
                        continue;                                                                                                        //不能黑棋吃黑棋、白棋吃白棋
                    }
                    if (cq.mSquare[X, Y] != chessSquare.chessName.Null)
                    {
                        cb.LCanCheck.Add(new int[] { X, Y });
                    }
                    else
                    {
                        cb.LCanWalk.Add(new int[] { X, Y });
                    }
                }
            }
            return(cb);
        }
Пример #2
0
 public WalkRoute(chessSquare csqIn, chessWalkPoint cwpIn, int scoreIn, chessSquare.chessSides csideIn, int TeamNumIn, int mNumIn, int pMaxScoreIn, int pMixScoreIn)
 {
     csq       = csqIn;
     cwp       = cwpIn;
     score     = scoreIn;
     cside     = csideIn;
     TeamNum   = TeamNumIn;
     mNum      = mNumIn;
     PMaxScore = pMaxScoreIn;
     PMinScore = pMixScoreIn;
 }
Пример #3
0
        public chessBasic canMoveRoute(int x, int y, chessSquare cq)
        {
            cwp = DGetWalkRouteFuc();
            if (cwp == null)
            {
                cwp = new chessWalkPoint(-1, -1, -1, -1);
            }

            cq1 = cq;
            int[,] dir;
            cb = new chessBasic();
            if (cq1.PSquare[x, y] == chessSquare.chessSides.Black)
            {
                if (y == 6)
                {
                    dir = new int[, ] {
                        { -1, -1 }, { 0, -1 }, { 1, -1 }, { 0, -2 }
                    };                                                               //左,中1,右,中2
                    checkRoute(x, y, dir);
                }
                else
                {
                    dir = new int[, ] {
                        { -1, -1 }, { 0, -1 }, { 1, -1 }
                    };                                                    //左,中1,右
                    checkRoute(x, y, dir);
                }
            }
            else
            {
                if (y == 1)
                {
                    dir = new int[, ] {
                        { -1, 1 }, { 0, 1 }, { 1, 1 }, { 0, 2 }
                    };                                                           //左,中1,右,中2
                    checkRoute(x, y, dir);
                }
                else
                {
                    dir = new int[, ] {
                        { -1, 1 }, { 0, 1 }, { 1, 1 }
                    };                                                 //左,中1,右
                    checkRoute(x, y, dir);
                }
            }
            dir = new int[, ] {
                { -1, 0 }, { 1, 0 }
            };
            specialPawnCheckRoute(x, y, dir);//吃過路兵
            return(cb);
        }
Пример #4
0
        /// <summary>
        /// AI啟動計算函式
        /// </summary>
        /// <param name="csq">當前地圖</param>
        /// <param name="cside">當前陣營方</param>
        /// <param name="stepNum">預計算之步數</param>
        public void AIStart(chessSquare csq, chessSquare.chessSides cside, int stepNum)
        {
            stepNumTot = stepNum;
            chessSquare.chessSides Ncside = cside;
            AIside = cside;
            if ((int)cside == 1)
            {
                Ncside = chessSquare.chessSides.Black;
            }
            else
            {
                Ncside = chessSquare.chessSides.White;
            }
            Console.WriteLine("============");
            sw.WriteLine("==============");
            sw.WriteLine(csq.CoverToForsythEdwardsNotation());

            //清除資料
            CPSZero      = null;
            ZeroMaxScore = int.MinValue;

            //計算另外一方之aSquare
            //csq.chessPointQnsALL(Ncside);
            csq.ListChessUpdate(Ncside, true);
            csq.aSquareUpdate(Ncside);

            //=================計算我方之路線
            AImain(new WalkRoute(csq, null, 0, cside, 0, stepNum, int.MinValue, int.MaxValue));
            //}
            //如果最終選擇之路線被宣告為無效者,且分數為負者,則棄手。
            if (CPSZero.FinalSelectWR.RouteIsInValid == true && CPSZero.FinalSelectWR.score < 0)
            {
                Rcwp           = null;
                AIGiveUpAction = true;
            }
            else
            {
                Rcwp = CPSZero.FinalSelectWR.cwp;
            }
            Console.WriteLine("============");
            sw.WriteLine("==============");
        }
Пример #5
0
        public bool CheckchessDraw(chessSquare csq)
        {
            //=============3次重複原則
            var data = (from a in WRListInfTot
                        group a by a into g
                        where g.Count() >= 3
                        select g.Key);

            if (data.Count() > 0)
            {
                DrawNum = 1;
                return(true);
            }
            //==============50步規則
            if (csq.ChessSquareDrawWalk[0] >= 50 && csq.ChessSquareDrawWalk[1] >= 50)
            {
                DrawNum = 2;
                return(true);
            }
            DrawNum = 0;
            return(false);
        }
Пример #6
0
            public bool CheckChessWin(chessSquare csq, chessSquare.chessSides cside)
            {
                chessSquare.chessSides Ncside;

                if ((int)cside == 1)
                {
                    Ncside = chessSquare.chessSides.Black;
                }
                else
                {
                    Ncside = chessSquare.chessSides.White;
                }

                int TX = csq.ChessSquareXY[(int)Ncside - 1][(int)chessSquare.chessName.King - 1][0][0];
                int TY = csq.ChessSquareXY[(int)Ncside - 1][(int)chessSquare.chessName.King - 1][0][1];

                if (TX == -1 && TY == -1)
                {
                    return(true);
                }
                return(false);
            }
Пример #7
0
        public chessBasic canMoveRoute(int x, int y, chessSquare cq)
        {
            chessBasic cb = new chessBasic();

            int[,] dir = { { -1,  1 }, {  0,  1 },
                           {  1,  1 }, { -1,  0 },
                           {  1,  0 }, { -1, -1 },
                           {  0, -1 }, {  1, -1 } };
            for (int i = 0; i < 8; i++)//皇后8個方向
            {
                int X    = x + dir[i, 0];
                int Y    = y + dir[i, 1];
                int flag = 1;
                for (int j = 0; j < 8 && flag == 1; j++)
                {
                    if (X >= 0 && X < 8 && Y >= 0 && Y < 8)//棋盤內
                    {
                        if ((cq.PSquare[x, y] == chessSquare.chessSides.Black && cq.PSquare[X, Y] == chessSquare.chessSides.Black) ||
                            (cq.PSquare[x, y] == chessSquare.chessSides.White && cq.PSquare[X, Y] == chessSquare.chessSides.White))
                        {
                            continue;                                                                                                        //不能黑棋吃黑棋、白棋吃白棋
                        }
                        if (cq.mSquare[X, Y] != chessSquare.chessName.Null)
                        {
                            cb.LCanCheck.Add(new int[] { X, Y });
                            flag = 0;
                        }
                        else
                        {
                            cb.LCanWalk.Add(new int[] { X, Y });
                        }
                    }
                    X = X + dir[i, 0];
                    Y = Y + dir[i, 1];
                }
            }
            return(cb);
        }
Пример #8
0
        private void AImain(object OBIn)
        {
            WalkRoute WRMain = (WalkRoute)OBIn;
            int       score  = 0; //MinMax用
            int       scoreS = 0; //Select用
            int       mNum   = WRMain.mNum;

            chessSquare.chessSides cside   = WRMain.cside;
            chessSquare.chessSides Ncside  = cside;
            chessSquare.chessSides AINside = cside;
            chessSquare            csq     = WRMain.csq;
            chessWalkPoint         cwp     = WRMain.cwp;
            ChessPointStore        cpsMain = new ChessPointStore(WRMain);
            bool GameOverFlag = false; //遊戲是否已結束標記(國王被吃)

            if (mNum < stepNumTot)     //非開創之主節點(Select)
            {
                //推算對方陣營
                if ((int)cside == 1)
                {
                    Ncside = chessSquare.chessSides.Black;
                }
                else
                {
                    Ncside = chessSquare.chessSides.White;
                }
                if ((int)AIside == 1)
                {
                    AINside = chessSquare.chessSides.Black;
                }
                else
                {
                    AINside = chessSquare.chessSides.White;
                }

                if (cwp.SpecialNum == chessSquare.chessSpecial.Null)
                {
                    //當前路線分數判斷部分
                    if (csq.PSquare[cwp.nNumX - 1, cwp.nNumY - 1] == Ncside)
                    {
                        //若是check的話則加分
                        //scoreS += (int)csq.mSquare[cwp.nNumX - 1, cwp.nNumY - 1] * 5;
                        switch (csq.mSquare[cwp.nNumX - 1, cwp.nNumY - 1])
                        {
                        case chessSquare.chessName.King:
                            scoreS += 999;
                            break;

                        case chessSquare.chessName.Queen:
                            scoreS += 60;
                            break;

                        case chessSquare.chessName.Pawn:
                            scoreS += 5;
                            break;

                        default:
                            scoreS += 20;
                            break;
                        }
                    }
                }
                else if (cwp.SpecialNum == chessSquare.chessSpecial.S_No1)
                {
                    scoreS += 5;
                }
                else if (cwp.SpecialNum == chessSquare.chessSpecial.S_No3)
                {
                    scoreS += 100;
                }

                //如果該路線被吃之棋子為對手之國王時,視同結束本節點
                if (csq.mSquare[cwp.nNumX - 1, cwp.nNumY - 1] == chessSquare.chessName.King &&
                    csq.PSquare[cwp.nNumX - 1, cwp.nNumY - 1] == Ncside)
                {
                    scoreS             = 9999;
                    GameOverFlag       = true;
                    WRMain.RouteIsOver = true;
                }

                //移動棋子
                csq.movechess(cwp);
                //csq.aSquareUpdate(cside);
                if (cwp.SpecialNum == chessSquare.chessSpecial.Null)
                {
                    int[] chessnum = csq.chessCheckFucFull(csq, cside, cwp.nNumX - 1, cwp.nNumY - 1);
                    if (chessnum[1] > 0)
                    {
                        scoreS += chessnum[0];
                        scoreS -= chessnum[1];
                    }
                }
            }

            cpsMain.csq = csq;

            //如果國王已被吃掉時,直接結束本節點
            if (csq.ChessSquareXY[(int)AIside - 1][(int)chessSquare.chessName.King - 1][0][0] == -1 &&
                csq.ChessSquareXY[(int)AIside - 1][(int)chessSquare.chessName.King - 1][0][1] == -1)
            {
                WRMain.RouteIsOver = true;
                GameOverFlag       = true;
                score = -9999;
            }
            else if (csq.ChessSquareXY[(int)AINside - 1][(int)chessSquare.chessName.King - 1][0][0] == -1 &&
                     csq.ChessSquareXY[(int)AINside - 1][(int)chessSquare.chessName.King - 1][0][1] == -1)
            {
                WRMain.RouteIsOver = true;
                GameOverFlag       = true;
                score = 9999;
            }
            else if (mNum == 0)                  //末節點
            {
                int[]  Tscore      = new int[2]; //雙方評分分數
                bool[] IsKingAlive = new bool[2];

                for (int numSide = 0; numSide < 2; ++numSide)
                {
                    //對雙方棋子進行存在評分
                    foreach (int[][] Nchess in csq.ChessSquareXY[numSide])
                    {
                        foreach (int[] NAchess in Nchess)
                        {
                            if (NAchess[0] == -1 && NAchess[1] == -1)
                            {
                                continue;                                        //該棋子已消失
                            }
                            else
                            {
                                switch (csq.mSquare[NAchess[0], NAchess[1]])
                                {
                                case chessSquare.chessName.King:
                                    Tscore[numSide]     += 999;
                                    IsKingAlive[numSide] = true;
                                    break;

                                case chessSquare.chessName.Queen:
                                    Tscore[numSide] += 60;
                                    break;

                                case chessSquare.chessName.Pawn:
                                    Tscore[numSide] += 5;
                                    break;

                                default:
                                    Tscore[numSide] += 20;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (AIside == chessSquare.chessSides.White)
                {
                    score = Tscore[0] - Tscore[1];
                }
                else if (AIside == chessSquare.chessSides.Black)
                {
                    score = Tscore[1] - Tscore[0];
                }
                if (IsKingAlive[(int)AIside - 1] == false)
                {
                    WRMain.RouteIsOver = true;
                    GameOverFlag       = true;
                }
            }

            //============
            if (mNum > 0 || AIside == cside)  //非末節點或者該節點為AI方陣營者
            {
                if (csq.chessCheck_Check(cside) == true)
                {
                    if (mNum < stepNumTot)
                    {
                        //於進行本步下卻讓國王仍然或被置身於check狀態,視為違法棋步
                        WRMain.RouteIsInValid = true;
                        GameOverFlag          = true;
                        if (cside == AIside)
                        {
                            score = -99999;
                        }
                        else
                        {
                            score = 99999;
                        }
                        scoreS = -99999;
                    }
                    else
                    {
                        csq.ChessSquareCheckK[(int)cside - 1] = true;
                    }
                }
                else
                {
                    csq.ChessSquareCheckK[(int)cside - 1] = false;
                }
            }
            //============
            if (mNum > 0)  //非末節點
            {
                if (csq.chessCheck_Check(Ncside) == true)
                {
                    //scoreS += 2;
                    csq.ChessSquareCheckK[(int)Ncside - 1] = true;
                }
                else
                {
                    csq.ChessSquareCheckK[(int)Ncside - 1] = false;
                }
            }
            //============

            //非最後一階段函式計算路線並遞迴部分(下一步之敵方判斷)
            if (mNum > 0 && GameOverFlag == false)
            {
                List <chessWalkPoint> NcwpL = new List <chessWalkPoint>();  //每一條路線路徑
                //==========對我方做一階諮詢
                csq.ListChessUpdate(Ncside, false);
                csq.aSquareUpdate(Ncside);
                foreach (int[][] Nchess in csq.ChessSquareXY[(int)Ncside - 1])
                {
                    foreach (int[] NAchess in Nchess)
                    {
                        if (NAchess[0] == -1 && NAchess[1] == -1)
                        {
                            continue;                                        //該棋子已消失
                        }
                        //======================
                        foreach (int[] tNum in csq.LChessBasic[NAchess[3]].LCanWalk.ToArray())
                        {
                            chessWalkPoint Ncwp = new chessWalkPoint(NAchess[0] + 1, NAchess[1] + 1, tNum[0] + 1, tNum[1] + 1);
                            NcwpL.Add(Ncwp);
                        }
                        foreach (int[] tNum in csq.LChessBasic[NAchess[3]].LCanCheck.ToArray())
                        {
                            chessWalkPoint Ncwp = new chessWalkPoint(NAchess[0] + 1, NAchess[1] + 1, tNum[0] + 1, tNum[1] + 1);
                            NcwpL.Add(Ncwp);
                        }
                        foreach (int[] tNum in csq.LChessBasic[NAchess[3]].LSpecial.ToArray())
                        {
                            //如果該路線為特殊2號規則,且於整場棋局內已使用過的話則視同無效
                            if ((chessSquare.chessSpecial)tNum[2] == chessSquare.chessSpecial.S_No2 &&
                                csq.IsChessSpecialNo2Used[(int)Ncside - 1] == true)
                            {
                                continue;
                            }

                            chessWalkPoint Ncwp = new chessWalkPoint(NAchess[0] + 1, NAchess[1] + 1, tNum[0] + 1, tNum[1] + 1, (chessSquare.chessSpecial)tNum[2]);
                            NcwpL.Add(Ncwp);
                        }
                    }
                }
                //=============================================
                if (mNum == stepNumTot)
                {
                    TND         = new Thread[NcwpL.Count];
                    AIFirstFlag = new bool[NcwpL.Count];
                }
                for (int i = NcwpL.Count - 1; i >= 0; --i)
                {
                    int teamNum = WRMain.TeamNum;
                    if (mNum == stepNumTot)
                    {
                        teamNum = i;
                    }
                    WalkRoute WRNext = new WalkRoute(new chessSquare(csq, true), NcwpL[i], 0, Ncside, teamNum, mNum - 1, WRMain.PMaxScore, WRMain.PMinScore);
                    WRNext.scoreMS = scoreS;
                    //節點下路線新增
                    if (cpsMain != null)
                    {
                        cpsMain.AddRoute(WRNext);
                    }
                    if (mNum == stepNumTot)
                    {
                        if (Ncside == chessSquare.chessSides.Black && WRNext.cwp.nNumY < WRNext.cwp.pNumY)
                        {
                            WRNext.RouteIsForwardVerticalMove = true;
                        }
                        else if (Ncside == chessSquare.chessSides.White && WRNext.cwp.nNumY > WRNext.cwp.pNumY)
                        {
                            WRNext.RouteIsForwardVerticalMove = true;
                        }
                        else
                        {
                            WRNext.RouteIsForwardVerticalMove = false;
                        }
                        AIFirstFlag[i] = false;
                        TND[i]         = new Thread(AImain);
                        TND[i].Start(WRNext);
                    }
                    else
                    {
                        AImain(WRNext);
                        if (mNum % 2 == stepNumTot % 2) //我方陣營回合
                        {
                            WRMain.PMaxScore = Math.Max(WRMain.PMaxScore, WRNext.score);
                        }
                        else //敵方陣營回合
                        {
                            WRMain.PMinScore = Math.Min(WRMain.PMinScore, WRNext.score);
                        }
                        //本節點為第一層節點時更新開創之主節點之目前最高分數
                        if (mNum == stepNumTot - 1)
                        {
                            WRMain.PMaxScore = ZeroMaxScore;
                        }

                        /*當本節點目前選擇之最高路線分數
                         * ,仍無法相等或超過與上一節點最高分門檻時*/
                        if (WRMain.PMinScore < WRMain.PMaxScore)
                        {
                            break;
                        }
                    }
                }

                if (mNum == stepNumTot)
                {
                    while (true)
                    {
                        bool BF = true;
                        for (int i = 0; i < AIFirstFlag.Length; ++i)
                        {
                            if (AIFirstFlag[i] == false)
                            {
                                BF = false;
                                break;
                            }
                        }
                        if (BF == true)
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(0);
                        }
                    }
                }
                if (cpsMain.WalkRouteList.Count > 1 && cpsMain.FinalSelectWR == null)
                {
                    //取所有下一步中最高分數
                    if (mNum % 2 == stepNumTot % 2) //我方陣營回合
                    {
                        //取所有下一步中最高分數
                        var result = cpsMain.WalkRouteList.Where(n =>
                                                                 n.score == cpsMain.WalkRouteList.Max(x => x.score));
                        var result2 = result.Where(n =>
                                                   n.scoreMS == result.Max(x => x.scoreMS));
                        if (result2.Count() > 0)
                        {
                            WalkRoute wk = result2.ElementAt(rd.Next(result2.Count()));
                            score   = wk.score;
                            scoreS += -wk.scoreMS;
                            cpsMain.FinalSelectWR = wk;
                        }
                        else
                        {
                            WalkRoute wk2 = result.ElementAt(rd.Next(result.Count()));
                            score   = wk2.score;
                            scoreS += -wk2.scoreMS;
                            cpsMain.FinalSelectWR = wk2;
                        }
                    }
                    else //敵方陣營回合
                    {
                        //取所有下一步中最低分數
                        var result = cpsMain.WalkRouteList.Where(n =>
                                                                 n.score == cpsMain.WalkRouteList.Min(x => x.score));
                        var result2 = result.Where(n =>
                                                   n.scoreMS == result.Max(x => x.scoreMS));
                        if (result2.Count() > 0)
                        {
                            WalkRoute wk = result2.ElementAt(rd.Next(result2.Count()));
                            score   = wk.score;
                            scoreS += -wk.scoreMS;
                            cpsMain.FinalSelectWR = wk;
                        }
                        else
                        {
                            WalkRoute wk2 = result.ElementAt(rd.Next(result.Count()));
                            score   = wk2.score;
                            scoreS += -wk2.scoreMS;
                            cpsMain.FinalSelectWR = wk2;
                        }
                    }
                }
                else if (cpsMain.WalkRouteList.Count == 1 && cpsMain.FinalSelectWR == null)
                {
                    score   = cpsMain.WalkRouteList[0].score;
                    scoreS += -cpsMain.WalkRouteList[0].scoreMS;
                    cpsMain.FinalSelectWR = cpsMain.WalkRouteList[0];
                }
                //如果最終選擇之子路線已被宣告為無效者
                if (cpsMain.FinalSelectWR.RouteIsInValid == true)
                {
                    //如果本棋步未被check,但最終選擇之子路線已被宣告為無效者,視同逼和
                    if (csq.ChessSquareCheckK[(int)Ncside - 1] == false)
                    {
                        score  = 0;
                        scoreS = 0;
                    }
                    //本路線視同為結束宣告
                    WRMain.RouteIsOver = true;
                }
                //如果最終選擇之子路線已被宣告為結束者,本路線視同為結束宣告
                if (cpsMain.FinalSelectWR.RouteIsOver == true)
                {
                    //本路線視同為結束宣告
                    WRMain.RouteIsOver = true;
                }
            }

            //節點紀錄物件儲存
            if (cpsMain != null)
            {
                cpsMain.WalkRouteThis.score   = score;
                cpsMain.WalkRouteThis.scoreMS = scoreS;

                if (mNum == stepNumTot)  //遞迴開創之主
                {
                    CPSZero = cpsMain;
                }
            }

            if (mNum == stepNumTot - 1)
            {
                Console.WriteLine("[{0}]{1}{2}=>{3}{4} score={5} scoreMS={6} {7}{8}",
                                  mNum, (char)(cwp.pNumX + 96), cwp.pNumY, (char)(cwp.nNumX + 96), cwp.nNumY, score, scoreS, WRMain.RouteIsInValid ? "X" : "", WRMain.RouteIsOver ? "G" : "");
                AIFirstFlag[WRMain.TeamNum] = true;
                //====================
                //本節點為第一層節點時更新開創之主節點之目前分數
                lock (OBL)
                {
                    if (score > ZeroMaxScore)
                    {
                        ZeroMaxScore = score;
                    }
                }
            }

            ////Debug用
            //lock (OBL)
            //{
            //    if (mNum <= stepNumTot - 1)
            //    {
            //        sw.WriteLine("[{0}] {1}{2}=>{3}{4} score={5} scoreMS={6} {7}{8} ({9})",
            //            mNum, (char)(cwp.pNumX + 96), cwp.pNumY, (char)(cwp.nNumX + 96), cwp.nNumY, score, scoreS, WRMain.RouteIsInValid ? "X" : "", WRMain.RouteIsOver ? "G" : "", WRMain.TeamNum);
            //    }
            //    if (mNum == stepNumTot - 1)
            //    {
            //        Console.WriteLine("[{0}]{1}{2}=>{3}{4} score={5} scoreMS={6} {7}{8}",
            //            mNum, (char)(cwp.pNumX + 96), cwp.pNumY, (char)(cwp.nNumX + 96), cwp.nNumY, score, scoreS, WRMain.RouteIsInValid ? "X" : "", WRMain.RouteIsOver ? "G" : "");
            //        AIFirstFlag[WRMain.TeamNum] = true;
            //        //====================
            //        //本節點為第一層節點時更新開創之主節點之目前分數
            //        if (score > ZeroMaxScore)
            //        {
            //            ZeroMaxScore = score;
            //        }
            //    }
            //}
        }
Пример #9
0
        public chessBasic canMoveRoute(int x, int y, chessSquare cq)
        {
            cq1 = cq;
            chessBasic cb = new chessBasic();

            int[,] dir = { { -1,  1 }, {  0,  1 },
                           {  1,  1 }, { -1,  0 },
                           {  1,  0 }, { -1, -1 },
                           {  0, -1 }, {  1, -1 } };
            for (int i = 0; i < 8; i++)//國王的8個走法
            {
                int X = x + dir[i, 0];
                int Y = y + dir[i, 1];
                if (X >= 0 && X < 8 && Y >= 0 && Y < 8)//棋盤內
                {
                    if ((cq1.PSquare[x, y] == chessSquare.chessSides.Black && cq1.PSquare[X, Y] == chessSquare.chessSides.Black) ||
                        (cq1.PSquare[x, y] == chessSquare.chessSides.White && cq1.PSquare[X, Y] == chessSquare.chessSides.White))
                    {
                        continue;                                                                                                          //不能黑棋吃黑棋、白棋吃白棋
                    }
                    if (cq1.mSquare[X, Y] != chessSquare.chessName.Null)
                    {
                        cb.LCanCheck.Add(new int[] { X, Y });
                    }
                    else
                    {
                        cb.LCanWalk.Add(new int[] { X, Y });
                    }
                }
            }

            if (x == 4 && y == 0 && cq1.PSquare[4, 0] == chessSquare.chessSides.White)
            {
                //白國王[4,0]白城堡[0,0]
                if (cq1.mSquare[0, 0] == chessSquare.chessName.Rook && Rule1((int)chessSquare.chessSides.White, (int)chessSquare.chessName.King, (int)chessSquare.chessSides.White, (int)chessSquare.chessName.Rook, 0) &&
                    Rule2(y, 4, 0) && Rule3(0, y, 4, 0))
                {
                    cb.LSpecial.Add(new int[] { 2, 0, (int)chessSquare.chessSpecial.S_No2 });
                }
                //白國王[4,0]白城堡[7,0]
                if (cq1.mSquare[7, 0] == chessSquare.chessName.Rook && Rule1((int)chessSquare.chessSides.White, (int)chessSquare.chessName.King, (int)chessSquare.chessSides.White, (int)chessSquare.chessName.Rook, 1) &&
                    Rule2(y, 4, 7) && Rule3(0, y, 4, 7))
                {
                    cb.LSpecial.Add(new int[] { 6, 0, (int)chessSquare.chessSpecial.S_No2 });
                }
            }
            else if (x == 4 && y == 7 && cq1.PSquare[4, 7] == chessSquare.chessSides.Black)
            {
                // 黑國王[4,7]黑城堡[0,7]
                if (cq1.mSquare[0, 7] == chessSquare.chessName.Rook && Rule1((int)chessSquare.chessSides.Black, (int)chessSquare.chessName.King, (int)chessSquare.chessSides.Black, (int)chessSquare.chessName.Rook, 0) &&
                    Rule2(y, 4, 0) && Rule3(1, y, 4, 0))
                {
                    cb.LSpecial.Add(new int[] { 2, 7, (int)chessSquare.chessSpecial.S_No2 });
                }
                //黑國王[4,7]黑城堡[7,7]
                if (cq1.mSquare[7, 7] == chessSquare.chessName.Rook && Rule1((int)chessSquare.chessSides.Black, (int)chessSquare.chessName.King, (int)chessSquare.chessSides.Black, (int)chessSquare.chessName.Rook, 1) &&
                    Rule2(y, 4, 7) && Rule3(1, y, 4, 7))
                {
                    cb.LSpecial.Add(new int[] { 6, 7, (int)chessSquare.chessSpecial.S_No2 });
                }
            }
            return(cb);
        }
Пример #10
0
        /// <summary>
        /// 轉換目前指令動作字串為標準代數記號(SAN)
        /// </summary>
        /// <param name="wrIn">行動路徑</param>
        /// <param name="csq">棋盤(移動前)</param>
        /// <param name="csqAfter">棋盤(移動後)</param>
        /// <returns></returns>
        public string CoverToAlgebraicNotation(chessWalkPoint wrIn, chessSquare csq, chessSquare csqAfter)
        {
            char[] row    = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
            char[] type   = { 'P', 'N', 'B', 'R', 'Q', 'K' }; //照chessSquare.chessName排序
            string output = "";

            chessSquare.chessSides cside = csq.PSquare[wrIn.pNumX - 1, wrIn.pNumY - 1];
            chessSquare.chessSides Ncside;
            int tY = 0;

            switch (wrIn.SpecialNum)
            {
            case chessSquare.chessSpecial.Null:
                if (csq.mSquare[wrIn.pNumX - 1, wrIn.pNumY - 1] == chessSquare.chessName.Pawn)
                {
                    if (csq.mSquare[wrIn.nNumX - 1, wrIn.nNumY - 1] != chessSquare.chessName.Null)
                    {
                        //士兵-一般吃子
                        output = String.Format("{0}x{1}{2}", row[wrIn.pNumX - 1], row[wrIn.nNumX - 1], wrIn.nNumY);
                    }
                    else
                    {
                        output = String.Format("{0}{1}", row[wrIn.nNumX - 1], wrIn.nNumY);
                    }
                }
                else
                {
                    output = String.Format("{0}{1}{2}{3}",
                                           type[(int)csq.mSquare[wrIn.pNumX - 1, wrIn.pNumY - 1] - 1],
                                           csq.mSquare[wrIn.nNumX - 1, wrIn.nNumY - 1] != chessSquare.chessName.Null ? "x" : "",
                                           row[wrIn.nNumX - 1],
                                           wrIn.nNumY);
                }
                break;

            case chessSquare.chessSpecial.S_No1:
                if (wrIn.nNumY <= 4)     //黑方吃白方
                {
                    tY = wrIn.nNumY - 1; //被吃方旗子之後面
                }
                else if (wrIn.nNumY > 4) //白方吃黑方
                {
                    tY = wrIn.nNumY + 1; //被吃方旗子之後面
                }
                output = String.Format("{0}x{1}{2} e.p.", row[wrIn.pNumX - 1], row[wrIn.nNumX - 1], tY);
                break;

            case chessSquare.chessSpecial.S_No2:
                if (wrIn.nNumY == 8)      //黑方移動
                {
                    if (wrIn.nNumX == 7)  //短易位
                    {
                        output = "0-0";
                    }
                    else if (wrIn.nNumX == 3)      //長易位
                    {
                        output = "0-0-0";
                    }
                }
                else if (wrIn.nNumY == 1)     //白方移動
                {
                    if (wrIn.nNumX == 7)      //短易位
                    {
                        output = "0-0";
                    }
                    else if (wrIn.nNumX == 3)      //長易位
                    {
                        output = "0-0-0";
                    }
                }
                break;

            case chessSquare.chessSpecial.S_No3:
                output = String.Format("{0}{1}={2}", row[wrIn.nNumX - 1], wrIn.nNumY, type[(int)csqAfter.mSquare[wrIn.nNumX - 1, wrIn.nNumY - 1] - 1]);
                break;

            default:
                return(null);
            }
            if ((int)cside == 1)
            {
                Ncside = chessSquare.chessSides.Black;
            }
            else
            {
                Ncside = chessSquare.chessSides.White;
            }
            if (csqAfter.chessCheck_Check(Ncside) == true)
            {
                output = output + "+";
            }
            return(output);
        }
Пример #11
0
        static void Main(string[] args)
        {
            bool GameTest = false;  //是否開啟循環測試模式

            do
            {
                GameRecord GRM = new GameRecord();
                //================
                ChessAI_MS ai  = new ChessAI_MS();
                ChessAI_MS ai2 = new ChessAI_MS();
                //================
                GUIVS gvs = new GUIVS();
                Pawn.DGetWalkRouteFuc         = new D_GetWalkRoute(GRM.GetWalkRoute);
                chessSquare.dQnsSpecailNO3Fnc = new D_QNSSpecialNo3(gvs.QnsSpecialNO3CMD);
                chessWalkPoint         cwpUser = null;
                chessSquare.chessSides gamecsideUser;
                chessSquare.chessSides gamecsideAI;
                chessSquare            csq = new chessSquare();
                chessSquare            csqBefore;
                int step = 0;
                //=============================================
                gamecsideUser = gvs.WBQnsUI();
                if ((int)gamecsideUser == 1)
                {
                    gamecsideAI = chessSquare.chessSides.Black;
                }
                else
                {
                    gamecsideAI = chessSquare.chessSides.White;
                }
                //=============================================
                csq.DebugPrint();
                Console.WriteLine();
                //================
                while (true)
                {
                    csqBefore = new chessSquare(csq, true);
                    ++step;
                    //====================
                    if (gamecsideUser == chessSquare.chessSides.White)
                    {
                        gvs.GameNowTurn = 1;
                        cwpUser         = gvs.UserTurnModeUI();
                        csq.movechess(cwpUser);
                        GRM.AddWalkRoute(cwpUser, csq.SquareInformationString(), GRM.CoverToAlgebraicNotation(cwpUser, csqBefore, csq));
                        Console.WriteLine(step + "." + GRM.GetWalkSAN());
                    }
                    else if (gamecsideAI == chessSquare.chessSides.White)
                    {
                        gvs.GameNowTurn = 2;
                        ai2.AIStart(new chessSquare(csq, true), gamecsideAI, 4);
                        if (ai2.Rcwp == null && ai2.AIGiveUpAction == true)
                        {
                            Console.WriteLine("{0} Give Up.", gamecsideAI);
                            GRM.reportSentMessage = string.Format("{0} Give Up.", gamecsideAI);
                            break;
                        }
                        else
                        {
                            Console.WriteLine("\n{0}{1}=>{2}{3} Type={4}\n",
                                              (char)(ai2.Rcwp.pNumX + 96), ai2.Rcwp.pNumY, (char)(ai2.Rcwp.nNumX + 96), ai2.Rcwp.nNumY, ai2.Rcwp.SpecialNum);
                            csq.movechess(ai2.Rcwp);
                            GRM.AddWalkRoute(ai2.Rcwp, csq.SquareInformationString(), GRM.CoverToAlgebraicNotation(ai2.Rcwp, csqBefore, csq));
                            csq.DebugPrint();
                            Console.WriteLine(csq.CoverToForsythEdwardsNotation());
                            Console.WriteLine(step + "." + GRM.GetWalkSAN());
                        }
                    }
                    //====================
                    if (GRM.CheckchessDraw(csq) == true)
                    {
                        if (GRM.DrawNum == 1)
                        {
                            Console.WriteLine("Draw:3步重複規則。");
                            GRM.reportSentMessage = "Draw:3步重複規則。";
                        }
                        else if (GRM.DrawNum == 2)
                        {
                            Console.WriteLine("Draw:50步規則。");
                            GRM.reportSentMessage = "Draw:50步規則。";
                        }
                        break;
                    }
                    if (gvs.CheckChessWin(csq, chessSquare.chessSides.White) == true)
                    {
                        Console.WriteLine("{0} Win!", chessSquare.chessSides.White);
                        GRM.reportSentMessage = string.Format("{0} Win!", chessSquare.chessSides.White);
                        break;
                    }
                    //====================================================
                    csqBefore = new chessSquare(csq, true);
                    //====================================================
                    if (gamecsideAI == chessSquare.chessSides.Black)
                    {
                        gvs.GameNowTurn = 2;
                        ai.AIStart(new chessSquare(csq, true), gamecsideAI, 4);
                        if (ai.Rcwp == null && ai.AIGiveUpAction == true)
                        {
                            Console.WriteLine("{0} Give Up.", gamecsideAI);
                            GRM.reportSentMessage = string.Format("{0} Give Up.", gamecsideAI);
                            break;
                        }
                        else
                        {
                            Console.WriteLine("\n{0}{1}=>{2}{3} Type={4}\n",
                                              (char)(ai.Rcwp.pNumX + 96), ai.Rcwp.pNumY, (char)(ai.Rcwp.nNumX + 96), ai.Rcwp.nNumY, ai.Rcwp.SpecialNum);
                            csq.movechess(ai.Rcwp);
                            GRM.AddWalkRoute(ai.Rcwp, csq.SquareInformationString(), GRM.CoverToAlgebraicNotation(ai.Rcwp, csqBefore, csq));
                            csq.DebugPrint();
                            Console.WriteLine(step + "." + GRM.GetWalkSAN());
                        }
                    }
                    else if (gamecsideUser == chessSquare.chessSides.Black)
                    {
                        gvs.GameNowTurn = 1;
                        cwpUser         = gvs.UserTurnModeUI();
                        csq.movechess(cwpUser);
                        GRM.AddWalkRoute(cwpUser, csq.SquareInformationString(), GRM.CoverToAlgebraicNotation(cwpUser, csqBefore, csq));
                        Console.WriteLine(step + "." + GRM.GetWalkSAN());
                    }
                    //=================
                    if (GRM.CheckchessDraw(csq) == true)
                    {
                        if (GRM.DrawNum == 1)
                        {
                            Console.WriteLine("Draw:3步重複規則。");
                            GRM.reportSentMessage = "Draw:3步重複規則。";
                        }
                        else if (GRM.DrawNum == 2)
                        {
                            Console.WriteLine("Draw:50步規則。");
                            GRM.reportSentMessage = "Draw:50步規則。";
                        }
                        break;
                    }
                    if (gvs.CheckChessWin(csq, chessSquare.chessSides.Black) == true)
                    {
                        Console.WriteLine("{0} Win!", chessSquare.chessSides.Black);
                        GRM.reportSentMessage = string.Format("{0} Win!", chessSquare.chessSides.Black);
                        break;
                    }
                }
                if (GRM.GameTotNum > 0)
                {
                    GRM.recordExport();
                }
            } while (GameTest == true);
            if (GameTest == false)
            {
                Console.ReadLine();
            }
        }