コード例 #1
0
ファイル: MainForm.cs プロジェクト: YumingRong/MoleXiangqi
        void MakeMove(int sqFrom, int sqTo)
        {
            Graphics g = PanelBoard.CreateGraphics();

            if (pos.IsLegalMove(sqFrom, sqTo))
            {
                int  pcCaptured = pos.pcSquares[sqTo];
                MOVE step       = new MOVE(sqFrom, sqTo, pos.pcSquares[sqFrom], pcCaptured);
                pos.MakeMove(step);
                if (pos.CheckedBy(1 - pos.sdPlayer) > 0)
                {
                    PlaySound("ILLEGAL");
                    pos.UnmakeMove();
                    return;
                }
                engine.MakeMove(step);
                MoveList.Add(step);
                CommentList.Add(textBoxComment.Text);

                if (FENStep > 0)
                {
                    //擦除上一步的起始和结束位置选择框
                    DrawBoard(ptLastFrom, g);
                    DrawBoard(ptLastTo, g);
                    DrawPiece(ptLastTo, pcLast, g);
                }

                ptLastFrom = POSITION.UI_Coord2XY(sqFrom, bFlipped);
                ptLastTo   = POSITION.UI_Coord2XY(sqTo, bFlipped);
                pcLast     = cnPieceImages[step.pcSrc];

                //擦除原来的位置
                DrawBoard(ptLastFrom, g);
                DrawSelection(ptLastFrom, g);
                //移动到新位置
                DrawSelection(ptLastTo, g);
                DrawPiece(ptLastTo, pcLast, g);
                bSelected = false;

                FENStep++;
                string label = step.ToString();
                if (FENStep % 2 == 1)
                {
                    label = ((FENStep / 2 + 1).ToString() + "." + label);
                }
                label = label.PadLeft(8);
                ListboxMove.Items.Add(label);
                if (pos.pcSquares[sqTo] > 0)
                {
                    PlaySound("CAPTURE");
                }
                else
                {
                    PlaySound("MOVE");
                }

                if (pos.IsMate())
                {//直接吃王或者绝杀
                    if (pos.sdPlayer == 1 && MenuAIBlack.Checked && !MenuAIRed.Checked ||
                        pos.sdPlayer == 0 && MenuAIRed.Checked && !MenuAIBlack.Checked)
                    {
                        PlaySound("WIN");
                    }
                    else if (pos.sdPlayer == 1 && !MenuAIBlack.Checked && MenuAIRed.Checked ||
                             pos.sdPlayer == 0 && !MenuAIRed.Checked && MenuAIBlack.Checked)
                    {
                        PlaySound("LOSS");
                    }
                    if (pos.sdPlayer == 0)
                    {
                        MessageBox.Show("黑方胜!");
                    }
                    else
                    {
                        MessageBox.Show("红方胜!");
                    }
                    App_inGame = false;
                }
            }
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: YumingRong/MoleXiangqi
        private async void PanelBoard_MouseClick(object sender, MouseEventArgs e)
        {
            if (!App_inGame)
            {
                return;
            }
            int x = e.X / gridSize;
            int y = e.Y / gridSize;

            if (x < 0 || x > 9 || y < 0 || y > 10)
            {
                return;
            }
            int piece;

            if (bFlipped)
            {
                piece = pos.UI_GetFlippedPiece(x, y);
            }
            else
            {
                piece = pos.UI_GetPiece(x, y);
            }

            Graphics g = PanelBoard.CreateGraphics();

            if (bSelected)
            {
                if (POSITION.SIDE(piece) == pos.sdPlayer && ptSelected != new Point(x, y))
                {
                    //又选择别的己方子
                    DrawBoard(ptSelected, g);
                    DrawPiece(ptSelected, pcSelected, g);
                    ptSelected = new Point(x, y);
                    pcSelected = cnPieceImages[piece];
                    DrawSelection(ptSelected, g);
                    PlaySound("CLICK");
                }
                else
                {
                    int sqFrom, sqTo;
                    if (bFlipped)
                    {
                        sqFrom = POSITION.UI_XY2Coord(8 - ptSelected.X, 9 - ptSelected.Y);
                        sqTo   = POSITION.UI_XY2Coord(8 - x, 9 - y);
                    }
                    else
                    {
                        sqFrom = POSITION.UI_XY2Coord(ptSelected.X, ptSelected.Y);
                        sqTo   = POSITION.UI_XY2Coord(x, y);
                    }
                    MakeMove(sqFrom, sqTo);
                    await GoAsync();
                }
            }
            else if (POSITION.SIDE(piece) == pos.sdPlayer)
            {
                if (pos.sdPlayer == 0 && !MenuAIRed.Checked || pos.sdPlayer == 1 && !MenuAIBlack.Checked)
                {
                    ptSelected = new Point(x, y);
                    pcSelected = cnPieceImages[piece];
                    DrawSelection(ptSelected, g);
                    bSelected = true;
                }
            }
        }