Пример #1
0
        // 點擊棋子的事件
        public void pc_MouseClick(object sender, EventArgs e)
        {
            if (canMove)
            {
                Piece pc = (Piece)sender;
                if (pc.side == Convert.ToBoolean(Situation.side))  // 棋子是進攻方
                {
                    var targetList1 = PlaceToMove(pc.type, pc.side, pc.CoX, pc.CoY);
                    int CandNum     = targetList1.Item1;
                    if (CandNum != 0)             // 棋子可以移動
                    {
                        if (!Situation.selecting) // 非選擇中
                        {
                            Situation.selecting = true;
                            Situation.index     = pc.id;
                        }
                        else if (pc.id != Situation.index)
                        {
                            Situation.index = pc.id;
                            // 刪除之前的target
deleteTargert1:
                            foreach (Control c in this.Controls)
                            {
                                if (c is Target)
                                {
                                    c.Dispose();
                                    this.Controls.Remove(c);
                                }
                            }

                            foreach (Control c in this.Controls)
                            {
                                if (c is Target)
                                {
                                    goto deleteTargert1;
                                }
                            }
                        }
                        else
                        {
                            goto cantMove;
                        }
                        boolPoint[] CandPoint = new boolPoint[17];
                        CandPoint = targetList1.Item2;
                        for (int i = 0; i < CandNum; i++)
                        {
                            Piece canEat = CandPoint[i].canEat;
                            int   CoX    = CandPoint[i].Location.X;
                            int   CoY    = CandPoint[i].Location.Y;
                            DrawTarget(CoX, CoY, pc.side, pc.id, canEat);
                        }
                        cantMove :;
                    }
                }
            }
        }
Пример #2
0
        // 依棋種不同,找出該棋可走的位置座標
        public Tuple <int, boolPoint[]> PlaceToMove(string type, bool side, int CoX, int CoY)
        {
            int moveNum = 0;

            boolPoint[] result = new boolPoint[17];
            switch (type)
            {
            case "car":
                int[] carstep    = { 1, 0, -1, 0, 0, 1, 0, -1 };
                int   carstepnum = carstep.Length;
                for (int i = 0; i < carstepnum; i += 2)
                {
                    for (int j = 1; j < Math.Abs(carstep[i]) * COL_NUM + Math.Abs(carstep[i + 1]) * ROW_NUM; j++)
                    {
                        if (OnBoard(CoX + carstep[i] * j, CoY + carstep[i + 1] * j))
                        {
                            if (PieceExist(CoX + carstep[i] * j, CoY + carstep[i + 1] * j) == null)
                            {
                                result[moveNum] = new boolPoint(null, new Point(CoX + carstep[i] * j, CoY + carstep[i + 1] * j));
                                moveNum++;
                            }
                            else
                            {
                                if (PieceExist(CoX + carstep[i] * j, CoY + carstep[i + 1] * j).side == !side)
                                {
                                    result[moveNum] = new boolPoint(PieceExist(CoX + carstep[i] * j, CoY + carstep[i + 1] * j), new Point(CoX + carstep[i] * j, CoY + carstep[i + 1] * j));
                                    moveNum++;
                                }
                                break;
                            }
                        }
                    }
                }

                break;

            case "horse":     // 馬的走法
                int[] horsestep    = { 2, 1, 1, 2, -1, 2, -2, 1, -2, -1, -1, -2, 1, -2, 2, -1 };
                int   horsestepnum = horsestep.Length;
                for (int i = 0; i < horsestepnum; i += 2)
                {
                    if (OnBoard(CoX + horsestep[i], CoY + horsestep[i + 1]))
                    {
                        if (PieceExist(CoX + (horsestep[i] - Math.Sign(horsestep[i]) * 1), CoY + (horsestep[i + 1] - Math.Sign(horsestep[i + 1]) * 1)) == null) //判斷是否拐馬腳
                        {
                            if (PieceExist(CoX + horsestep[i], CoY + horsestep[i + 1]) == null)                                                                 //判斷是否有棋子
                            {
                                result[moveNum] = new boolPoint(null, new Point(CoX + horsestep[i], CoY + horsestep[i + 1]));
                                //result[moveNum].Location = new Point(CoX + horsestep[i], CoY + horsestep[i + 1]);
                                moveNum++;
                            }
                            else if (PieceExist(CoX + horsestep[i], CoY + horsestep[i + 1]).side == !side)      //有棋子但是為對方棋子
                            {
                                result[moveNum] = new boolPoint(PieceExist(CoX + horsestep[i], CoY + horsestep[i + 1]), new Point(CoX + horsestep[i], CoY + horsestep[i + 1]));
                                moveNum++;
                            }
                        }
                    }
                }
                break;

            case "elephant":     // 象的走法
                int[] elephantstep    = { 2, 2, -2, 2, -2, -2, 2, -2 };
                int   elephantstepnum = elephantstep.Length;
                for (int i = 0; i < elephantstepnum; i += 2)
                {
                    if (OnBoard(CoX + elephantstep[i], CoY + elephantstep[i + 1]))
                    {
                        if (PieceExist(CoX + (elephantstep[i] - Math.Sign(elephantstep[i]) * 1), CoY + (elephantstep[i + 1] - Math.Sign(elephantstep[i + 1]) * 1)) == null) //判斷是否拐馬腳
                        {
                            if (PieceExist(CoX + elephantstep[i], CoY + elephantstep[i + 1]) == null)                                                                       //判斷是否有棋子
                            {
                                result[moveNum] = new boolPoint(null, new Point(CoX + elephantstep[i], CoY + elephantstep[i + 1]));
                                moveNum++;
                            }
                            else if (PieceExist(CoX + elephantstep[i], CoY + elephantstep[i + 1]).side == !side)        //有棋子但是為對方棋子
                            {
                                result[moveNum] = new boolPoint(PieceExist(CoX + elephantstep[i], CoY + elephantstep[i + 1]), new Point(CoX + elephantstep[i], CoY + elephantstep[i + 1]));
                                moveNum++;
                            }
                        }
                    }
                }
                break;

            case "guard":
                int[] guardstep    = { 1, 1, -1, 1, -1, -1, 1, -1 };
                int   guardstepnum = guardstep.Length;
                for (int i = 0; i < guardstepnum; i += 2)
                {
                    bool canMove = false;
                    if (OnBoard(CoX + guardstep[i], CoY + guardstep[i + 1]))
                    {
                        if (side && CoX + guardstep[i] <= 6 && CoX + guardstep[i] >= 4 && CoY + guardstep[i + 1] <= 3 && CoY + guardstep[i + 1] >= 1)
                        {
                            canMove = true;
                        }
                        else if (!side && CoX + guardstep[i] <= 6 && CoX + guardstep[i] >= 4 && CoY + guardstep[i + 1] <= 10 && CoY + guardstep[i + 1] >= 8)      //判斷是否拐馬腳
                        {
                            canMove = true;
                        }
                    }
                    if (canMove)
                    {
                        if (PieceExist(CoX + guardstep[i], CoY + guardstep[i + 1]) == null)        //判斷是否有棋子
                        {
                            result[moveNum] = new boolPoint(null, new Point(CoX + guardstep[i], CoY + guardstep[i + 1]));
                            moveNum++;
                        }
                        else if (PieceExist(CoX + guardstep[i], CoY + guardstep[i + 1]).side == !side)        //有棋子但是為對方棋子
                        {
                            result[moveNum] = new boolPoint(PieceExist(CoX + guardstep[i], CoY + guardstep[i + 1]), new Point(CoX + guardstep[i], CoY + guardstep[i + 1]));
                            moveNum++;
                        }
                    }
                }
                break;

            case "general":
                int[] generalstep    = { 1, 0, -1, 0, 0, 1, 0, -1 };
                int   generalstepnum = generalstep.Length;
                for (int i = 0; i < generalstepnum; i += 2)
                {
                    bool canMove = false;
                    if (OnBoard(CoX + generalstep[i], CoY + generalstep[i + 1]))
                    {
                        if (side && CoX + generalstep[i] <= 6 && CoX + generalstep[i] >= 4 && CoY + generalstep[i + 1] <= 3 && CoY + generalstep[i + 1] >= 1)
                        {
                            canMove = true;
                        }
                        else if (!side && CoX + generalstep[i] <= 6 && CoX + generalstep[i] >= 4 && CoY + generalstep[i + 1] <= 10 && CoY + generalstep[i + 1] >= 8)        //判斷是否拐馬腳
                        {
                            canMove = true;
                        }
                    }
                    if (canMove)
                    {
                        if (PieceExist(CoX + generalstep[i], CoY + generalstep[i + 1]) == null)        //判斷是否有棋子
                        {
                            result[moveNum] = new boolPoint(null, new Point(CoX + generalstep[i], CoY + generalstep[i + 1]));
                            moveNum++;
                        }
                        else if (PieceExist(CoX + generalstep[i], CoY + generalstep[i + 1]).side == !side)        //有棋子但是為對方棋子
                        {
                            result[moveNum] = new boolPoint(PieceExist(CoX + generalstep[i], CoY + generalstep[i + 1]), new Point(CoX + generalstep[i], CoY + generalstep[i + 1]));
                            moveNum++;
                        }
                    }
                }
                bool facetoface = true;
                int  king       = -1;
                for (int i = 0; i < ROW_NUM; i++)
                {
                    if (PieceExist(CoX, CoY + i + 1) != null)
                    {
                        if (PieceExist(CoX, CoY + i + 1).type == "general")
                        {
                            facetoface = true;
                            king       = i;
                            break;
                        }
                        else
                        {
                            facetoface = false;
                            break;
                        }
                    }
                }
                if (facetoface && king != -1)
                {
                    result[moveNum] = new boolPoint(PieceExist(CoX, CoY + king + 1), new Point(CoX, CoY + king + 1));
                    moveNum++;
                }
                break;

            case "bang":
                int[] bangstep    = { 1, 0, -1, 0, 0, 1, 0, -1 };
                int   bangstepnum = bangstep.Length;
                for (int i = 0; i < bangstepnum; i += 2)
                {
                    for (int j = 1; j < Math.Abs(bangstep[i]) * COL_NUM + Math.Abs(bangstep[i + 1]) * ROW_NUM; j++)
                    {
                        if (OnBoard(CoX + bangstep[i] * j, CoY + bangstep[i + 1] * j))
                        {
                            if (PieceExist(CoX + bangstep[i] * j, CoY + bangstep[i + 1] * j) == null)
                            {
                                result[moveNum] = new boolPoint(null, new Point(CoX + bangstep[i] * j, CoY + bangstep[i + 1] * j));
                                moveNum++;
                            }
                            else
                            {
                                for (int k = 1; k < Math.Abs(bangstep[i]) * COL_NUM + Math.Abs(bangstep[i + 1]) * ROW_NUM - j; k++)
                                {
                                    if (OnBoard(CoX + bangstep[i] * (j + k), CoY + bangstep[i + 1] * (j + k)))
                                    {
                                        if (PieceExist(CoX + bangstep[i] * (j + k), CoY + bangstep[i + 1] * (j + k)) != null)
                                        {
                                            if (PieceExist(CoX + bangstep[i] * (j + k), CoY + bangstep[i + 1] * (j + k)).side == !side)
                                            {
                                                result[moveNum] = new boolPoint(PieceExist(CoX + bangstep[i] * (j + k), CoY + bangstep[i + 1] * (j + k)), new Point(CoX + bangstep[i] * (j + k), CoY + bangstep[i + 1] * (j + k)));
                                                moveNum++;
                                                break;
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
                break;

            case "soldier":
                int[] soldierstep    = { 1, 0, -1, 0, 0, 1, 0, -1 };
                int   soldierstepnum = soldierstep.Length;
                for (int i = 0; i < soldierstepnum; i += 2)
                {
                    if (side)
                    {
                        if ((CoY >= 6 && i == 6) || (CoY < 6 && i != 4))
                        {
                            goto cantMove;
                        }
                    }
                    else
                    {
                        if ((CoY <= 5 && i == 4) || (CoY > 5 && i != 6))
                        {
                            goto cantMove;
                        }
                    }
                    if (OnBoard(CoX + soldierstep[i], CoY + soldierstep[i + 1]))
                    {
                        if (PieceExist(CoX + soldierstep[i], CoY + soldierstep[i + 1]) == null)        //判斷是否有棋子
                        {
                            result[moveNum] = new boolPoint(null, new Point(CoX + soldierstep[i], CoY + soldierstep[i + 1]));
                            moveNum++;
                        }
                        else if (PieceExist(CoX + soldierstep[i], CoY + soldierstep[i + 1]).side == !side)        //有棋子但是為對方棋子
                        {
                            result[moveNum] = new boolPoint(PieceExist(CoX + soldierstep[i], CoY + soldierstep[i + 1]), new Point(CoX + soldierstep[i], CoY + soldierstep[i + 1]));
                            moveNum++;
                        }
                    }

                    cantMove :;
                }
                break;

            default:
                break;
            }
            return(new Tuple <int, boolPoint[]>(moveNum, result));
        }