예제 #1
0
        public static void CheckEnemyKingBeAttaced(int startX,
                                                   int startY,
                                                   int finishX,
                                                   int finishY,
                                                   bool[,] chessmanPresenceSign,
                                                   ControlCollection controls,
                                                   object sender,
                                                   Dictionary <string, int> moveOrder)
        {
            //Клонируем массив признака расположения шахмат, для фальсификации
            bool[,] fakeChessmanPresenceSign = new bool[8, 8];
            for (var i = 0; i < 8; i++)
            {
                for (var j = 0; j < 8; j++)
                {
                    fakeChessmanPresenceSign[i, j] = chessmanPresenceSign[i, j];
                }
            }
            fakeChessmanPresenceSign[startY, startX]   = false;
            fakeChessmanPresenceSign[finishY, finishX] = true;

            //Клонируем свойство расположения шахмат для возможности фальсификации
            foreach (var control in controls)
            {
                if (control is Chessman)
                {
                    ((Chessman)control).FakeLocation = ((Chessman)control).Location;
                }
            }

            //Проверка возможности шаха
            foreach (var control in controls)
            {
                if (control is King)
                {
                    if (((Chessman)control).ChessColor != ((Chessman)sender).ChessColor)
                    {
                        var kingfinishX = (((King)control).Location.X - 27) / 50;
                        var kingFinishY = (((King)control).Location.Y - 27) / 50;

                        ((Chessman)control).FakeCheck = true;
                        ((Chessman)sender).ShahSigne  = King.CheckKingBeAttacked(kingfinishX, kingFinishY,
                                                                                 fakeChessmanPresenceSign,
                                                                                 controls, control, false, sender);
                        ((Chessman)control).FakeCheck = false;
                    }
                }
            }


            //Проверка возможности мата
            if (((Chessman)sender).ShahSigne)
            {
                var  flag = true;
                int  chessStartX, chessStartY;
                bool result;

                //Клонируем свойство расположения шахмат для возможности фальсификации
                foreach (var control in controls)
                {
                    if (control is Chessman)
                    {
                        ((Chessman)control).FakeLocation = ((Chessman)control).Location;
                    }
                }

                foreach (var control in controls)
                {
                    if (control is Chessman && flag)
                    {
                        ((Chessman)sender).MateSigne = true;
                        if (((Chessman)control).ChessColor != ((Chessman)sender).ChessColor)
                        {
                            result = true;

                            // Делаем фэйковые координаты
                            chessStartX = (((Chessman)control).Location.X - 27) / 50;
                            chessStartY = (((Chessman)control).Location.Y - 27) / 50;


                            if (control is King)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = King.CheckKingMove(chessStartX, chessStartY, chessFinishX,
                                                                        chessFinishY,
                                                                        fakeChessmanPresenceSign, controls, control, sender);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (control is Queen)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        //Задаем фэйковое положение
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Queen.CheckQueenMove(chessStartX, chessStartY, chessFinishX,
                                                                          chessFinishY,
                                                                          fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }


                            if (control is Castle)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Castle.CheckCastleMove(chessStartX, chessStartY, chessFinishX,
                                                                            chessFinishY,
                                                                            fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            if (control is Elephant)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Elephant.CheckElephantMove(chessStartX, chessStartY, chessFinishX,
                                                                                chessFinishY,
                                                                                fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }


                            if (control is Horse)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Horse.CheckHorseMove(chessStartX, chessStartY, chessFinishX,
                                                                          chessFinishY,
                                                                          fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            if (control is Pawn)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Pawn.CheckPawnMove(chessStartX, chessStartY, chessFinishX,
                                                                        chessFinishY,
                                                                        fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (!result)
                            {
                                ((Chessman)sender).MateSigne = false;
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        private void CheckChessMove(object sender)
        {
            var startX  = (initialChessmanLocation.X - 27) / 50;
            var startY  = (initialChessmanLocation.Y - 27) / 50;
            var finishX = (CellsPositions[chessmansCellIndexRow, chessmansCellIndexColumn].X - 27) / 50;
            var finishY = (CellsPositions[chessmansCellIndexRow, chessmansCellIndexColumn].Y - 27) / 50;

            var impossibleMove = sender is Queen
                ? Queen.CheckQueenMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder)
                : sender is Castle
                    ? Castle.CheckCastleMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder)
                    : sender is Elephant
                        ? Elephant.CheckElephantMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder)
                        : sender is Horse
                            ? Horse.CheckHorseMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder)
                            : sender is King
                                ? King.CheckKingMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, sender)
                                : Pawn.CheckPawnMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder);

            if (impossibleMove)
            {
                MessageBox.Show(Resources.ImpossibleMoveMessage);

                chess          = (Chessman)sender;
                chess.Location = initialChessmanLocation;
            }
            else
            {
                //Проверка возможности превращения
                if (sender is Pawn)
                {
                    if (((Pawn)sender).CanTransform)
                    {
                        var dialogWindow = new PawnTransfomDialog();
                        dialogWindow.Owner = this;

                        DialogResult dialogResult = dialogWindow.ShowDialog();
                        if (dialogResult == DialogResult.OK && !dialogWindow.RBPawn.Checked)
                        {
                            var color       = ((Pawn)sender).ChessColor;
                            var finishPoint = new Point(finishX * 50 + 27, finishY * 50 + 27);

                            ((Pawn)sender).Dispose();

                            if (dialogWindow.RBCastle.Checked)
                            {
                                var index = Castles.Count + 1;
                                Castles.Add(color + "Castel" + index, new Castle(color));
                                Controls.Add(Castles[color + "Castel" + index]);
                                Castles[color + "Castel" + index].Location = finishPoint;

                                Castles[color + "Castel" + index].MouseLeave += Chessman_MouseLeave;
                                Castles[color + "Castel" + index].MouseDown  += Chessman_MouseDown;
                                Castles[color + "Castel" + index].MouseUp    += Chessman_MouseUp;
                                Castles[color + "Castel" + index].MouseMove  += Chessman_MouseMove;

                                sender = Castles[color + "Castel" + index];
                                Chessman.CheckEnemyKingBeAttaced(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls,
                                                                 sender, moveOrder);
                            }

                            if (dialogWindow.RBElephant.Checked)
                            {
                                var index = Elephants.Count + 1;
                                Elephants.Add(color + "Elephant" + index, new Elephant(color));
                                Controls.Add(Elephants[color + "Elephant" + index]);
                                Elephants[color + "Elephant" + index].Location = finishPoint;

                                Elephants[color + "Elephant" + index].MouseLeave += Chessman_MouseLeave;
                                Elephants[color + "Elephant" + index].MouseDown  += Chessman_MouseDown;
                                Elephants[color + "Elephant" + index].MouseUp    += Chessman_MouseUp;
                                Elephants[color + "Elephant" + index].MouseMove  += Chessman_MouseMove;

                                sender = Elephants[color + "Elephant" + index];
                                Chessman.CheckEnemyKingBeAttaced(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls,
                                                                 sender, moveOrder);
                            }

                            if (dialogWindow.RBHorse.Checked)
                            {
                                var index = Horses.Count + 1;
                                Horses.Add(color + "Horse" + index, new Horse(color));
                                Controls.Add(Horses[color + "Horse" + index]);
                                Horses[color + "Horse" + index].Location = finishPoint;

                                Horses[color + "Horse" + index].MouseLeave += Chessman_MouseLeave;
                                Horses[color + "Horse" + index].MouseDown  += Chessman_MouseDown;
                                Horses[color + "Horse" + index].MouseUp    += Chessman_MouseUp;
                                Horses[color + "Horse" + index].MouseMove  += Chessman_MouseMove;

                                sender = Horses[color + "Horse" + index];
                                Chessman.CheckEnemyKingBeAttaced(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls,
                                                                 sender, moveOrder);
                            }

                            if (dialogWindow.RBQueen.Checked)
                            {
                                var index = Queens.Count + 1;
                                Queens.Add(color + "Queen" + index, new Queen(color));
                                Controls.Add(Queens[color + "Queen" + index]);
                                Queens[color + "Queen" + index].Location = finishPoint;

                                Queens[color + "Queen" + index].MouseLeave += Chessman_MouseLeave;
                                Queens[color + "Queen" + index].MouseDown  += Chessman_MouseDown;
                                Queens[color + "Queen" + index].MouseUp    += Chessman_MouseUp;
                                Queens[color + "Queen" + index].MouseMove  += Chessman_MouseMove;

                                sender = Queens[color + "Queen" + index];
                                Chessman.CheckEnemyKingBeAttaced(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls,
                                                                 sender, moveOrder);
                            }
                        }
                    }
                }

                //Проверка мата или шаха
                ChessmanPresenceSign[startY, startX]   = false;
                ChessmanPresenceSign[finishY, finishX] = true;

                moveOrder[((Chessman)sender).ChessColor] += -2;

                if (((Chessman)sender).ShahSigne && !((Chessman)sender).MateSigne)
                {
                    MessageBox.Show(Resources.ShahMessage);
                    ((Chessman)sender).ShahSigne = false;
                }

                if (((Chessman)sender).MateSigne)
                {
                    DialogResult result = MessageBox.Show(Resources.MateMessage + Environment.NewLine + Resources.restartQuestion,
                                                          Resources.GameEndMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (result == DialogResult.Yes)
                    {
                        Application.Restart();
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
            }
        }