Ejemplo n.º 1
0
        public bool Undo()
        {
            if (stackUndo.Count <= 0)
            {
                MessageBox.Show("Bàn cờ chưa được đi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return(false);
            }

            InfoUndo infoUndoDes = stackUndo.Pop();
            InfoUndo infoUndoSrc = stackUndo.Pop();

            Matrix[infoUndoSrc.X][infoUndoSrc.Y].BackgroundImage = Matrix[infoUndoDes.X][infoUndoDes.Y].BackgroundImage;
            Matrix[infoUndoSrc.X][infoUndoSrc.Y].Name            = Matrix[infoUndoDes.X][infoUndoDes.Y].Name;

            Matrix[infoUndoDes.X][infoUndoDes.Y].BackgroundImage = infoUndoDes.Image;
            Matrix[infoUndoDes.X][infoUndoDes.Y].Name            = infoUndoDes.Name;

            if (infoUndoDes.Image != null)
            {
                if (infoUndoDes.Name.Substring(0, 1) == "W")
                {
                    if (indexp2 > -1)
                    {
                        PnlPlayer2.Controls.RemoveAt(indexp2);
                        indexp2--;
                        DecreaseLocation(ref pointP2);
                    }
                }
                else
                {
                    if (indexp1 > -1)
                    {
                        PnlPlayer1.Controls.RemoveAt(indexp1);
                        indexp1--;
                        DecreaseLocation(ref pointP1);
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        public void MoveButton(Button button, string NameButton)
        {
            switch (NameButton)
            {
            case "W_T":
            case "B_T":
                Trooper t = new Trooper();
                sourceButton = button;

                Point pointTrooper = GetChess(Matrix, button);

                t.CanMove(Matrix, pointTrooper);
                t.CanKill(Matrix, pointTrooper);
                break;

            case "B_R":
            case "W_R":
                Rook r = new Rook();
                sourceButton = button;

                Point pointRook = GetChess(Matrix, button);
                r.CanMove(Matrix, pointRook);
                //r.CanKill(Matrix, pointRook);
                break;

            case "W_B":
            case "B_B":
                Bishop b = new Bishop();

                sourceButton = button;
                Point pointBishop = GetChess(Matrix, button);
                b.CanMove(Matrix, pointBishop);
                //r.CanKill(Matrix, pointRook);
                break;

            case "W_K":
            case "B_K":
                King k  = new King();
                King go = new King();
                sourceButton = button;
                Point pointKing = GetChess(Matrix, button);
                k.CanMove(Matrix, pointKing);
                //r.CanKill(Matrix, pointRook);
                break;

            case "W_Q":
            case "B_Q":
                Queen q = new Queen();

                sourceButton = button;
                Point pointQueen = GetChess(Matrix, button);
                q.CanMove(Matrix, pointQueen);

                break;

            case "W_N":
            case "B_N":
                Knight n = new Knight();

                sourceButton = button;
                Point pointKnight = GetChess(Matrix, button);
                n.CanMove(Matrix, pointKnight);
                break;

            case "A":
                desnitationButton = button;

                Point    pointDes    = GetChess(Matrix, desnitationButton);
                Point    pointSrc    = GetChess(Matrix, sourceButton);
                InfoUndo infoUndoDes = new InfoUndo(button.BackgroundImage, button.Name, pointDes.X, pointDes.Y);
                InfoUndo infoUndoSrc = new InfoUndo(sourceButton.BackgroundImage, sourceButton.Name, pointSrc.X, pointSrc.Y);

                stackUndo.Push(infoUndoSrc);
                stackUndo.Push(infoUndoDes);

                int x = GetChess(Matrix, sourceButton).Y - GetChess(Matrix, desnitationButton).Y;
                Move(Matrix, sourceButton, desnitationButton);


                if (x == 2 && (desnitationButton.Name == "B_K" || desnitationButton.Name == "W_K"))
                {
                    Point point = new Point();

                    point = GetChess(Matrix, desnitationButton);

                    Move(Matrix, Matrix[point.X][point.Y - 2], Matrix[point.X][point.Y + 1]);
                }
                else if (x == -2 && (desnitationButton.Name == "B_K" || desnitationButton.Name == "W_K"))
                {
                    Point point = new Point();

                    point = GetChess(Matrix, desnitationButton);

                    Move(Matrix, Matrix[point.X][point.Y + 1], Matrix[point.X][point.Y - 1]);
                }
                break;

            case "K":
                //Gán đích chuẩn bị thay thế
                desnitationButton = button;

                Point pointDess = GetChess(Matrix, desnitationButton);
                Point pointSrcs = GetChess(Matrix, sourceButton);

                InfoUndo infoUndoDess = new InfoUndo(button.BackgroundImage, button.Name, pointDess.X, pointDess.Y);
                InfoUndo infoUndoSrcs = new InfoUndo(sourceButton.BackgroundImage, sourceButton.Name, pointSrcs.X, pointSrcs.Y);

                stackUndo.Push(infoUndoSrcs);
                stackUndo.Push(infoUndoDess);

                //Lấy ảnh đưa vào panel
                if (button.Name.Substring(0, 1) == "W")
                {
                    CurrentPoint = pointP2;
                }
                else
                {
                    CurrentPoint = pointP1;
                }

                PictureBox picture = new PictureBox()
                {
                    Width                 = 50,
                    Height                = 50,
                    BackgroundImage       = button.BackgroundImage,
                    Location              = new Point(CurrentPoint.X, CurrentPoint.Y),
                    BackgroundImageLayout = ImageLayout.Stretch,
                    BorderStyle           = BorderStyle.Fixed3D,
                    Name = button.Name
                };
                if (picture.Name.Substring(0, 1) == "B")
                {
                    PnlPlayer1.Controls.Add(picture);
                    indexp1 += 1;
                    IncreaseLocation(ref pointP1);
                    if (Matrix[pointDess.X][pointDess.Y].Name == "B_K")
                    {
                        indexp1 = -9;
                    }
                }
                else
                {
                    PnlPlayer2.Controls.Add(picture);
                    indexp2 += 1;
                    IncreaseLocation(ref pointP2);
                    if (Matrix[pointDess.X][pointDess.Y].Name == "W_K")
                    {
                        indexp2 = -10;
                        indexp1 = -9;
                    }
                }

                Kill(Matrix, sourceButton, desnitationButton);
                if (indexp1 == -9)
                {
                    MessageBox.Show("Player 1 You Win ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ChessBoard.Enabled = false;
                }
                else if (indexp2 == -10 && indexp1 == -9)
                {
                    MessageBox.Show("Player 2 You Win ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ChessBoard.Enabled = false;
                }
                break;

            default:

                break;
            }
        }