Exemplo n.º 1
0
        public Bitmap TakePicture(int w, int h)
        {
            Bitmap bmp = new Bitmap(w, h);

            IsCapturedMode = true;

            for (int x = 1; x <= 8; x++)
            {
                for (int y = 1; y <= 8; y++)
                {
                    UcChessPiece p = arrChessCell[x, y].ChessPiece;
                    if (p != null)
                    {
                        this.Controls.Remove(p);
                        arrChessCell[x, y].ChessPiece = p;
                    }
                }
            }
            this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height));
            IsCapturedMode = false;
            for (int x = 1; x <= 8; x++)
            {
                for (int y = 1; y <= 8; y++)
                {
                    UcChessPiece p = arrChessCell[x, y].ChessPiece;
                    if (p != null)
                    {
                        arrChessCell[x, y].Controls.Remove(p);
                        arrChessCell[x, y].ChessPiece = p;
                    }
                }
            }
            return(bmp);
        }
Exemplo n.º 2
0
        //Nếu chơi với máy hì máy sẽ chon quân Hậu và không hieern thị form chọn
        //Nếu đóng Form thì mặc định sẽ chọn quân hậu
        public static bool Promotion(UcChessPiece UcPawn, ChessPieceType PromoteTo)
        {
            if ((UcPawn.PositionY == 8) || (UcPawn.PositionY == 1))
            {
                Bitmap queen  = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, ChessPieceType.Queen, UcPawn.Style);
                Bitmap root   = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, ChessPieceType.Rook, UcPawn.Style);
                Bitmap knight = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, ChessPieceType.Knight, UcPawn.Style);
                Bitmap bishop = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, ChessPieceType.Bishop, UcPawn.Style);

                if (PromoteTo == ChessPieceType.Null)
                {
                    Form f = new frmPromotion(queen, root, knight, bishop);
                    f.ShowDialog();
                    UcPawn.Type = frmPromotion.Type;
                }
                else
                {
                    UcPawn.Type = PromoteTo;
                }
                Bitmap image = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, UcPawn.Type, UcPawn.Style);
                UcPawn.Image = image;
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (IsCancelThinking == true)
            {
                return;
            }
            IsCancelThinking = false;
            IsThinking       = false;

            if (arrMove.Count > 0)
            {
                UcChessPiece Piece = this.arrChessCell[MyMove.CurPos.X, MyMove.CurPos.Y].ChessPiece;
                DoMove(Piece, MyMove);
            }
        }
Exemplo n.º 4
0
        //vd: E2E3, E7E8=q
        public void MakeMove(string strMove)
        {
            if (strMove == "")
            {
                return;
            }
            clsMove MyMove = new clsMove(strMove);


            UcChessPiece Piece = this.arrChessCell[MyMove.CurPos.X, MyMove.CurPos.Y].ChessPiece;

            if (Piece == null)
            {
                return;
            }

            arrMove = clsChessEngine.FindAllLegalMove(_BoardState, MyMove.CurPos, Piece.Type);
            DoMove(Piece, MyMove);
        }
Exemplo n.º 5
0
        public void DoMove(UcChessPiece Piece, clsMove move)
        {
            if ((Piece.Side == ChessSide.Black && this._WhiteToMove == true) || (Piece.Side == ChessSide.White && this._WhiteToMove == false))
            {
                return;
            }

            Point CurPos = move.CurPos;
            Point NewPos = move.NewPos;

            UcChessCell ct = (UcChessCell)this.arrChessCell[NewPos.X, NewPos.Y];

            //*********************************Kiểm tra nước đi************************
            //Kiểm tra Xem Quân Cờ có thể di chuyển từ CurPos đến NewPos hay không
            if (clsChessEngine.CanMove(arrMove, NewPos) == true)
            {
                UnHighlightLastMove();
                PushState();
                string strLastCell = Convert.ToChar(CurPos.X + 64) + CurPos.Y.ToString();
                string strNewCell  = Convert.ToChar(NewPos.X + 64) + NewPos.Y.ToString();


                this._BoardState[CurPos.X, CurPos.Y] = 0;//Đánh dấu vị trí cũ không chứa quân cờ
                this._BoardState[NewPos.X, NewPos.Y] = (int)Piece.Type * 10 + (int)Piece.Side;
                //Kiểm tra xem quân vua có bị chiếu không
                //****************Chưa tính trường hợp chiếu bí và Hòa***************


                bool bCapture = false;

                //Lấy vị trí Bắt tôt qua đường nếu có

                if (Piece.Type == ChessPieceType.Pawn)
                {
                    if (Math.Abs(NewPos.Y - CurPos.Y) == 2)
                    {
                        _EnPassantPoint = clsPawn.GetEnPassantPoint(this._BoardState, NewPos);
                    }
                    else if (CurPos.X == NewPos.X)
                    {
                        _EnPassantPoint = new Point();
                    }


                    //Thực hiện nước bắt tốt qua đường
                    if (NewPos == _EnPassantPoint)
                    {
                        Point CapturedPoint;

                        if (Piece.Side == ChessSide.White)
                        {
                            CapturedPoint = new Point(_EnPassantPoint.X, _EnPassantPoint.Y - 1);
                        }
                        else
                        {
                            CapturedPoint = new Point(_EnPassantPoint.X, _EnPassantPoint.Y + 1);
                        }

                        UcChessCell bt = arrChessCell[CapturedPoint.X, CapturedPoint.Y];

                        this._BoardState[CapturedPoint.X, CapturedPoint.Y] = 0;//Đánh dấu vị trí cũ không chứa quân cờ



                        stkCapturedPieces.Push(bt.ChessPiece);

                        bt.ChessPiece.Hide();
                        bt.ChessPiece   = null;
                        _EnPassantPoint = new Point();

                        bCapture = true;
                    }
                }
                else
                {
                    _EnPassantPoint = new Point();
                }

                if (ct.ChessPiece != null && ct.ChessPiece.Side != Piece.Side)//Nếu đã có quân cờ đặt tại ô đến
                {
                    UcChessPiece CapturedPiece = ct.ChessPiece;
                    stkCapturedPieces.Push(CapturedPiece);
                    CapturedPiece.Hide();//Xóa quân cờ tại ô Di chuyển đến

                    if (CapturedPiece.Type == ChessPieceType.Rook)
                    {
                        if (CapturedPiece.Side == ChessSide.White)
                        {
                            if (CapturedPiece.PositionX == 1)
                            {
                                QUEENsideCastling = false;
                            }
                            if (CapturedPiece.PositionX == 8)
                            {
                                KINGsideCastling = false;
                            }
                        }
                        else
                        {
                            if (CapturedPiece.PositionX == 1)
                            {
                                queensideCastling = false;
                            }
                            if (CapturedPiece.PositionX == 8)
                            {
                                kingsideCastling = false;
                            }
                        }
                    }
                    ct.ChessPiece = null;
                    bCapture      = true;
                }

                Piece.UConCell.ChessPiece = null;//Ô cờ trước đó ko còn chứa quân cờ này nữa
                //Dat Quan Co Tai Vi Tri Moi
                ct.ChessPiece  = Piece;
                Piece.UConCell = ct;

                //Gán tọa độ mới cho quân cờ
                Piece.PositionX = NewPos.X;
                Piece.PositionY = NewPos.Y;

                string strPromote = "";
                if (Piece.Type == ChessPieceType.Pawn)
                {
                    if (clsPawn.Promotion(Piece, move.PromoteTo) == true)
                    {
                        this._BoardState[Piece.PositionX, Piece.PositionY] = (int)Piece.Type * 10 + (int)Piece.Side;
                        switch (Piece.Type)
                        {
                        case ChessPieceType.Bishop: strPromote = "=B";
                            break;

                        case ChessPieceType.Knight: strPromote = "=N";
                            break;

                        case ChessPieceType.Rook: strPromote = "=R";
                            break;

                        case ChessPieceType.Queen: strPromote = "=Q";
                            break;
                        }
                        move.PromoteTo = Piece.Type;
                    }
                }

                if (bCapture == true)
                {
                    PushMove(Piece.Side, strLastCell + "X" + strNewCell + strPromote);
                    OnPieceCaptured(EventArgs.Empty);
                    PlayMusic(CapturedSound);
                }
                else
                {
                    PushMove(Piece.Side, strLastCell + "-" + strNewCell + strPromote);
                    PlayMusic(MovedSound);
                }

                //Cập Nhật trạng thái nhập thành
                if (Piece.Type == ChessPieceType.Rook)
                {
                    if (Piece.Side == ChessSide.White)
                    {
                        if (Piece.PositionX == 1)
                        {
                            QUEENsideCastling = false;
                        }
                        if (Piece.PositionX == 8)
                        {
                            KINGsideCastling = false;
                        }
                    }
                    else
                    {
                        if (Piece.PositionX == 1)
                        {
                            queensideCastling = false;
                        }
                        if (Piece.PositionX == 8)
                        {
                            kingsideCastling = false;
                        }
                    }
                }
                if (Piece.Type == ChessPieceType.King)
                {
                    if (Piece.Side == ChessSide.White)
                    {
                        QUEENsideCastling = false;
                        KINGsideCastling  = false;
                    }
                    else
                    {
                        queensideCastling = false;
                        kingsideCastling  = false;
                    }

                    if (Math.Abs(NewPos.X - CurPos.X) == 2)//Nhập Thành
                    {
                        if (NewPos.X == 3)
                        {
                            UcChessCell OldRookCell = (UcChessCell)this.arrChessCell[1, NewPos.Y];
                            UcChessCell NewRookCell = (UcChessCell)this.arrChessCell[4, NewPos.Y];

                            this._BoardState[1, NewPos.Y] = 0;                    //Đánh dấu quân xe không còn ở vị trí cũ
                            this._BoardState[4, NewPos.Y] = 40 + (int)Piece.Side; //Đặt quân xe tại vị trí mới

                            NewRookCell.ChessPiece = OldRookCell.ChessPiece;
                            OldRookCell.ChessPiece = null;

                            NewRookCell.ChessPiece.UConCell  = NewRookCell;
                            NewRookCell.ChessPiece.PositionX = NewRookCell.PositionX;
                            NewRookCell.ChessPiece.PositionY = NewRookCell.PositionY;
                        }
                        else
                        {
                            UcChessCell OldRookCell = (UcChessCell)this.arrChessCell[8, NewPos.Y];
                            UcChessCell NewRookCell = (UcChessCell)this.arrChessCell[6, NewPos.Y];

                            this._BoardState[8, NewPos.Y] = 0;                    //Đánh dấu quân xe không còn ở vị trí cũ
                            this._BoardState[6, NewPos.Y] = 40 + (int)Piece.Side; //Đặt quân xe tại vị trí mới

                            NewRookCell.ChessPiece = OldRookCell.ChessPiece;
                            OldRookCell.ChessPiece = null;

                            NewRookCell.ChessPiece.UConCell  = NewRookCell;
                            NewRookCell.ChessPiece.PositionX = NewRookCell.PositionX;
                            NewRookCell.ChessPiece.PositionY = NewRookCell.PositionY;
                        }
                    }
                }
                arrFEN.Add(clsFEN.GetPiecePlacementString(this._BoardState));
                //********Play Sound, Hiển thị thông báo*************
                this._GameStatus = clsChessEngine.GetGameStatus(this._BoardState, arrFEN, Piece.Side);

                if (this._GameStatus == GameStatus.BlackWin)
                {
                    PlayMusic(CheckMatedSound);
                    DisplayMessage("Quân Đen Thắng !");
                }
                if (this._GameStatus == GameStatus.WhiteWin)
                {
                    PlayMusic(CheckMatedSound);
                    DisplayMessage("Quân Trắng Thắng !");
                }
                if (this._GameStatus == GameStatus.Draw)
                {
                    if (clsChessEngine.CheckThreefoldRepetition(arrFEN))
                    {
                        DisplayMessage("Ván cờ Hòa do \"Bất Biến 3 Lần\"");
                    }
                    else if (clsChessEngine.CheckInsufficientMaterial(this._BoardState, Piece.Side))
                    {
                        DisplayMessage("Ván cờ Hòa do cả 2 bên không đủ quân chiếu bí đối phương");
                    }
                    else
                    {
                        DisplayMessage("Ván cờ Hòa do không còn nước đi hợp lệ");
                    }
                }

                if (this._GameStatus == GameStatus.NowPlaying && (clsKing.IsChecked(this._BoardState, ChessSide.Black) == true || clsKing.IsChecked(this._BoardState, ChessSide.White) == true))
                {
                    PlayMusic(CheckedSound);
                    if ((this._OwnSide == ChessSide.White && this._WhiteToMove == false) || (this._OwnSide == ChessSide.Black && this._WhiteToMove == true))
                    {
                        DisplayMessage("Chiếu !");
                    }
                }


                if (Piece.Side == this._OwnSide)
                {
                    AlreadyMakeMove = true;
                }

                //Chuyển lượt đi cho đối phương
                if (Piece.Side == ChessSide.Black)
                {
                    this.WhiteToMove = true;
                }
                else
                {
                    this.WhiteToMove = false;
                }


                //Cho máy đi
                if (Piece.Side == this.OwnSide && this.GameMode == GameMode.VsComputer)
                {
                    IsThinking = true;
                    timerComputerMove.Enabled = true;
                }

                LastMove = move;

                HighlightLastMove();


                OnMoveMaked(EventArgs.Empty);
            }
            else
            {
                Piece.UConCell.ChessPiece = Piece;// tra về ô cũ
            }
        }