예제 #1
0
파일: Board.cs 프로젝트: mariocervera/Chess
        private void listenerSquare(Square sq, bool whiteTurn)
        {
            List      pWhite = null;
            List      pBlack = null;
            ImageList iWhite = new ImageList();
            ImageList iBlack = new ImageList();

            Piece piece = null;

            if (whiteTurn)
            {
                piece = Board.getInstance().consultPiece(sq.getPosition(), true);
            }
            else
            {
                piece = Board.getInstance().consultPiece(sq.getPosition(), false);
            }

            if (piece != null)
            {
                if (MainForm.selectedPiece != null)
                {
                    deletePieceSquare(MainForm.selectedPiece);
                    deleteAllowedMovements(MainForm.selectedPiece.getAllowedMovements());
                }

                MainForm.selectedPiece = piece;
                paintPiece(MainForm.selectedPiece);
                paintAllowedMovements(MainForm.selectedPiece.getAllowedMovements());
            }
            else   //Empty square or occupied by an opponent's piece

            {
                Color turnColor;
                if (whiteTurn)
                {
                    turnColor = Color.White;
                }
                else
                {
                    turnColor = Color.Black;
                }

                if (MainForm.selectedPiece != null && MainForm.selectedPiece.getColor() == turnColor)
                { // A piece of the current player is selected
                    Position posIni = MainForm.selectedPiece.getPosition();
                    Position posFin = sq.getPosition();

                    Piece deletedPiece = null;                      //Deleted piece. It is returned in case an undo action is triggered.

                    if (MainForm.selectedPiece.isvalidMove(posFin)) //If the movement is valid ...
                    {
                        if (Caretaker.mementoBoard != null)
                        {
                            pWhite = Caretaker.mementoBoard.getWhitePieces().clone();
                            pBlack = Caretaker.mementoBoard.getBlackPieces().clone();
                        }
                        if (Caretaker.mementoImages != null)
                        {
                            foreach (Image i in Caretaker.mementoImages.getWhiteImages().Images)
                            {
                                iWhite.Images.Add(i);
                            }
                            foreach (Image i in Caretaker.mementoImages.getBlackImages().Images)
                            {
                                iBlack.Images.Add(i);
                            }
                        }
                        createMementos();
                        deletedPiece = realizeMovement(posFin);
                        BoardLogic.updateListAllowedMovements();

                        bool kingCheck = false;

                        if (whiteTurn)
                        {
                            kingCheck = BoardLogic.isCheck(true);
                        }
                        else
                        {
                            kingCheck = BoardLogic.isCheck(false);
                        }

                        if (kingCheck)
                        {
                            //Undo the movement

                            realizeMovement(posIni);

                            Caretaker.mementoBoard.setWhitePieces(pWhite);
                            Caretaker.mementoBoard.setBlackPieces(pBlack);
                            Caretaker.mementoImages.setWhiteImages(iWhite);
                            Caretaker.mementoImages.setBlackImages(iBlack);

                            if (deletedPiece != null)
                            {
                                //Return the deleted piece

                                if (whiteTurn)
                                {
                                    blackPieces.insert(deletedPiece);
                                }
                                else
                                {
                                    whitePieces.insert(deletedPiece);
                                }

                                //Delete inserted image

                                if (whiteTurn)
                                {
                                    mainForm.deleteLastImage(mainForm.getBlackImages());
                                }
                                else
                                {
                                    mainForm.deleteLastImage(mainForm.getWhiteImages());
                                }

                                drawPiece(deletedPiece);
                            }
                            MessageBox.Show(this, "Your king would be in check if you carried out this movement", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else   //The king is not in check

                        {
                            int lastRow = -1;

                            if (whiteTurn)
                            {
                                lastRow = 0;
                            }
                            else
                            {
                                lastRow = 7;
                            }

                            if (MainForm.selectedPiece is Pawn && posFin.getRow() == lastRow)
                            {
                                //Promotion

                                FormPromotion f = new FormPromotion(MainForm.selectedPiece as Pawn);
                                f.ShowDialog();
                            }

                            BoardLogic.updateListAllowedMovements();

                            bool doCheck;

                            if (whiteTurn)
                            {
                                doCheck = BoardLogic.isCheck(false);
                            }
                            else
                            {
                                doCheck = BoardLogic.isCheck(true);
                            }

                            if (doCheck)
                            {
                                MessageBox.Show(this, "King is in check!", "Check", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }

                            if (deletedPiece != null)
                            {
                                if (whiteTurn)
                                {
                                    mainForm.paintDeletedBlackPieces();
                                }
                                else
                                {
                                    mainForm.paintDeletedWhitePieces();
                                }
                            }

                            mainForm.showInLog(MainForm.selectedPiece.ToString() + " --> " + "From " + posIni.ToString() + " to " + posFin.ToString());
                            mainForm.enableUndo();
                            mainForm.shiftTurn();
                        }
                    }
                }
            }
        }
예제 #2
0
파일: Board.cs 프로젝트: mariocervera/Chess
        private void listenerSquare(Square sq, bool whiteTurn)
        {
            List pWhite = null;
            List pBlack = null;
            ImageList iWhite = new ImageList();
            ImageList iBlack = new ImageList();

            Piece piece = null;

            if (whiteTurn)
            {
                piece = Board.getInstance().consultPiece(sq.getPosition(), true);
            }
            else {
                piece = Board.getInstance().consultPiece(sq.getPosition(), false);
            }

            if (piece != null)
            {
                if (MainForm.selectedPiece != null)
                {
                    deletePieceSquare(MainForm.selectedPiece);
                    deleteAllowedMovements(MainForm.selectedPiece.getAllowedMovements());
                }

                MainForm.selectedPiece = piece;
                paintPiece(MainForm.selectedPiece);
                paintAllowedMovements(MainForm.selectedPiece.getAllowedMovements());
            }
            else { //Empty square or occupied by an opponent's piece

                Color turnColor;
                if (whiteTurn)
                {
                    turnColor = Color.White;
                }
                else {
                    turnColor = Color.Black;
                }

                if (MainForm.selectedPiece != null && MainForm.selectedPiece.getColor() == turnColor)
                { // A piece of the current player is selected

                    Position posIni = MainForm.selectedPiece.getPosition();
                    Position posFin = sq.getPosition();

                    Piece deletedPiece = null; //Deleted piece. It is returned in case an undo action is triggered.

                    if (MainForm.selectedPiece.isvalidMove(posFin)) //If the movement is valid ...
                    { 
                        if (Caretaker.mementoBoard != null)
                        {
                            pWhite = Caretaker.mementoBoard.getWhitePieces().clone();
                            pBlack = Caretaker.mementoBoard.getBlackPieces().clone();
                        }
                        if (Caretaker.mementoImages != null)
                        {
                            foreach (Image i in Caretaker.mementoImages.getWhiteImages().Images)
                            {
                                iWhite.Images.Add(i);
                            }
                            foreach (Image i in Caretaker.mementoImages.getBlackImages().Images)
                            {
                                iBlack.Images.Add(i);
                            }
                        }
                        createMementos();
                        deletedPiece = realizeMovement(posFin);
                        BoardLogic.updateListAllowedMovements();

                        bool kingCheck = false;

                        if (whiteTurn)
                        {
                            kingCheck = BoardLogic.isCheck(true);
                        }
                        else {
                            kingCheck = BoardLogic.isCheck(false);
                        }

                        if (kingCheck)
                        {
                            //Undo the movement

                            realizeMovement(posIni);

                            Caretaker.mementoBoard.setWhitePieces(pWhite);
                            Caretaker.mementoBoard.setBlackPieces(pBlack);
                            Caretaker.mementoImages.setWhiteImages(iWhite);
                            Caretaker.mementoImages.setBlackImages(iBlack);

                            if (deletedPiece != null)
                            {
                                //Return the deleted piece

                                if (whiteTurn)
                                {
                                    blackPieces.insert(deletedPiece);
                                }
                                else {
                                    whitePieces.insert(deletedPiece);
                                }

                                //Delete inserted image

                                if (whiteTurn)
                                {

                                    mainForm.deleteLastImage(mainForm.getBlackImages());
                                }
                                else {
                                    mainForm.deleteLastImage(mainForm.getWhiteImages());
                                }

                                drawPiece(deletedPiece);
                            }
                            MessageBox.Show(this, "Your king would be in check if you carried out this movement", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else { //The king is not in check

                            int lastRow = -1;

                            if (whiteTurn)
                            {
                                lastRow = 0;
                            }
                            else {
                                lastRow = 7;
                            }

                            if (MainForm.selectedPiece is Pawn && posFin.getRow() == lastRow)
                            {
                                //Promotion

                                FormPromotion f = new FormPromotion(MainForm.selectedPiece as Pawn);
                                f.ShowDialog();
                            }

                            BoardLogic.updateListAllowedMovements();

                            bool doCheck;

                            if (whiteTurn)
                            {
                                doCheck = BoardLogic.isCheck(false);
                            }
                            else {
                                doCheck = BoardLogic.isCheck(true);
                            }

                            if (doCheck)
                            {
                                MessageBox.Show(this, "King is in check!", "Check", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }

                            if (deletedPiece != null)
                            {

                                if (whiteTurn)
                                {
                                    mainForm.paintDeletedBlackPieces();
                                }
                                else {
                                    mainForm.paintDeletedWhitePieces();
                                }
                            }

                            mainForm.showInLog(MainForm.selectedPiece.ToString() + " --> " + "From " + posIni.ToString() + " to " + posFin.ToString());
                            mainForm.enableUndo();
                            mainForm.shiftTurn();
                        }
                    }
                }
            }
        }