예제 #1
0
        static void Main()
        {
            string FigPos = Console.ReadLine();
            Horse  horse  = new Horse();
            Rook   rook   = new Rook();

            string text = "";

            using (StreamReader fs = new StreamReader(@"chess.txt"))
            {
                while (true)
                {
                    // Читаем строку из файла во временную переменную.
                    string temp = fs.ReadLine();

                    // Если достигнут конец файла, прерываем считывание.
                    if (temp == null)
                    {
                        break;
                    }

                    // Пишем считанную строку в итоговую переменную.
                    text += temp + " ";
                }
            }

            List <IFigure> lst = new List <IFigure>()
            {
            };

            string[] text2 = text.Split(' ');

            for (int i = 0; i < text2.Length; i += 2)
            {
                if (text2[i] == "R")
                {
                    rook.Pos  = text2[i + 1];
                    rook.Name = text2[i];

                    lst.Add(rook);
                    if (!rook.CheckPos(FigPos))
                    {
                        Console.WriteLine(false);
                        Console.ReadLine();
                        return;
                    }
                }

                if (text2[i] == "H")
                {
                    horse.Pos  = text2[i + 1];
                    horse.Name = text2[i];

                    lst.Add(horse);
                    if (horse.CheckPos(FigPos) == false)
                    {
                        Console.WriteLine(false);
                        Console.ReadLine();
                        return;
                    }
                }
            }

            Console.WriteLine(true);
            Console.ReadLine();
        }
예제 #2
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;
                            }
                        }
                    }
                }
            }
        }
예제 #3
0
        public ChessBoard(Player p1, Player p2)
        {
            //all white pieces
            whiteKing = new King(p1, 3, 0);
            ChessPiece whiteQueen = new Queen(p1, 4, 0);
            ChessPiece whiteRunner1 = new Runner(p1, 2, 0);
            ChessPiece whiteRunner2 = new Runner(p1, 5, 0);
            ChessPiece whiteHorse1 = new Horse(p1, 1, 0);
            ChessPiece whiteHorse2 = new Horse(p1, 6, 0);
            ChessPiece whiteTower1 = new Tower(p1, 0, 0);
            ChessPiece whiteTower2 = new Tower(p1, 7, 0);
            ChessPiece whiteFarmer1 = new Farmer(p1, 0, 1);
            ChessPiece whiteFarmer2 = new Farmer(p1, 1, 1);
            ChessPiece whiteFarmer3 = new Farmer(p1, 2, 1);
            ChessPiece whiteFarmer4 = new Farmer(p1, 3, 1);
            ChessPiece whiteFarmer5 = new Farmer(p1, 4, 1);
            ChessPiece whiteFarmer6 = new Farmer(p1, 5, 1);
            ChessPiece whiteFarmer7 = new Farmer(p1, 6, 1);
            ChessPiece whiteFarmer8 = new Farmer(p1, 7, 1);

            //all black pieces
            blackKing = new King(p2, 4, 7);
            ChessPiece blackQueen = new Queen(p2, 3, 7);
            ChessPiece blackRunner1 = new Runner(p2, 2, 7);
            ChessPiece blackRunner2 = new Runner(p2, 5, 7);
            ChessPiece blackHorse1 = new Horse(p2, 1, 7);
            ChessPiece blackHorse2 = new Horse(p2, 6, 7);
            ChessPiece blackTower1 = new Tower(p2, 0, 7);
            ChessPiece blackTower2 = new Tower(p2, 7, 7);
            ChessPiece blackFarmer1 = new Farmer(p2, 0, 6);
            ChessPiece blackFarmer2 = new Farmer(p2, 1, 6);
            ChessPiece blackFarmer3 = new Farmer(p2, 2, 6);
            ChessPiece blackFarmer4 = new Farmer(p2, 3, 6);
            ChessPiece blackFarmer5 = new Farmer(p2, 4, 6);
            ChessPiece blackFarmer6 = new Farmer(p2, 5, 6);
            ChessPiece blackFarmer7 = new Farmer(p2, 6, 6);
            ChessPiece blackFarmer8 = new Farmer(p2, 7, 6);

            //add all chesspieces to twodimensional array
            board[3, 0] = whiteKing;
            board[4, 0] = whiteQueen;
            board[2, 0] = whiteRunner1;
            board[5, 0] = whiteRunner2;
            board[1, 0] = whiteHorse1;
            board[6, 0] = whiteHorse2;
            board[0, 0] = whiteTower1;
            board[7, 0] = whiteTower2;
            board[0, 1] = whiteFarmer1;
            board[1, 1] = whiteFarmer2;
            board[2, 1] = whiteFarmer3;
            board[3, 1] = whiteFarmer4;
            board[4, 1] = whiteFarmer5;
            board[5, 1] = whiteFarmer6;
            board[6, 1] = whiteFarmer7;
            board[7, 1] = whiteFarmer8;

            board[4, 7] = blackKing;
            board[3, 7] = blackQueen;
            board[2, 7] = blackRunner1;
            board[5, 7] = blackRunner2;
            board[1, 7] = blackHorse1;
            board[6, 7] = blackHorse2;
            board[0, 7] = blackTower1;
            board[7, 7] = blackTower2;
            board[0, 6] = blackFarmer1;
            board[1, 6] = blackFarmer2;
            board[2, 6] = blackFarmer3;
            board[3, 6] = blackFarmer4;
            board[4, 6] = blackFarmer5;
            board[5, 6] = blackFarmer6;
            board[6, 6] = blackFarmer7;
            board[7, 6] = blackFarmer8;
        }
예제 #4
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();
                    }
                }
            }
        }
        public void PerformsMove(Position origin, Position destination)
        {
            Piece captured = ExecuteMove(origin, destination);

            if (IsKingInCheck(CurrentPlayer))
            {
                UndoMove(origin, destination, captured);
                throw new ChessBoardException("You can't put yourself in check!");
            }

            Piece piece = Board.GetPiece(destination);

            // #SpecialMove Pawn Promotion
            if (piece is Pawn)
            {
                if ((piece.Color == Color.White && destination.Line == 0) || (piece.Color == Color.Black && destination.Line == 7))
                {
                    piece = Board.RemovePiece(destination);
                    _pieces.Remove(piece);
                    Console.WriteLine("\nPawn Promotion!");
                    Console.WriteLine("Queen(Q), Rook(R), Bishop(B),Horse (H)");
                    Console.Write("Choose a new piece (Q/R/B/H): ");
                    char  pieceType = char.Parse(Console.ReadLine());
                    Piece newPiece;
                    if (pieceType == 'Q' || pieceType == 'q')
                    {
                        newPiece = new Queen(Board, piece.Color);
                    }
                    else if (pieceType == 'R' || pieceType == 'r')
                    {
                        newPiece = new Rook(Board, piece.Color);
                    }
                    else if (pieceType == 'B' || pieceType == 'b')
                    {
                        newPiece = new Bishop(Board, piece.Color);
                    }
                    else if (pieceType == 'H' || pieceType == 'h')
                    {
                        newPiece = new Horse(Board, piece.Color);
                    }
                    else
                    {
                        newPiece = new Queen(Board, piece.Color);
                    }

                    Board.InsertPiece(newPiece, destination);
                    _pieces.Add(newPiece);
                }
            }

            if (IsKingInCheck(Adversary(CurrentPlayer)))
            {
                InCheck = true;
                if (IsCheckMate(Adversary(CurrentPlayer)))
                {
                    IsMatchEnded = true;
                    return;
                }
            }
            else
            {
                InCheck = false;
            }
            Turn++;
            ChangeCurrentPlayer();

            piece = Board.GetPiece(destination);

            // #Special Move En Passant
            if (piece is Pawn && (destination.Line == origin.Line - 2 || destination.Line == origin.Line + 2) && piece.MovimentsQuantity == 1)
            {
                EnPassantVulnerable = piece;
            }
            else
            {
                EnPassantVulnerable = null;
            }
        }
예제 #6
0
        void INIT()
        {
            for (int r = 0; r < Dimension; r++)
            {
                for (int c = 0; c < Dimension; c++)
                {
                    if (r == 1)
                    {
                        Ps[r, c] = new Pawn(MYCOLOR.BLACK, "Black_Pawn.png");
                    }
                    else if (r == 6)
                    {
                        Ps[r, c] = new Pawn(MYCOLOR.WHITE, "White_Pawn.png");
                    }
                    else if (r == 0 && (c == 0 || c == 7))
                    {
                        Ps[r, c] = new Rook(MYCOLOR.BLACK, "Black_Rook.png");
                    }
                    else if (r == 7 && (c == 0 || c == 7))
                    {
                        Ps[r, c] = new Rook(MYCOLOR.WHITE, "White_Rook.png");
                    }
                    else if (r == 0 && (c == 1 || c == 6))
                    {
                        Ps[r, c] = new Horse(MYCOLOR.BLACK, "Black_Knight.png");
                    }
                    else if (r == 7 && (c == 1 || c == 6))
                    {
                        Ps[r, c] = new Horse(MYCOLOR.WHITE, "White_Knight.png");
                    }
                    else if (r == 0 && (c == 2 || c == 5))
                    {
                        Ps[r, c] = new Bishop(MYCOLOR.BLACK, "Black_Bishop.png");
                    }
                    else if (r == 7 && (c == 2 || c == 5))
                    {
                        Ps[r, c] = new Bishop(MYCOLOR.WHITE, "White_Bishop.png");
                    }
                    else if (r == 0 && c == 3)
                    {
                        Ps[r, c] = new Queen(MYCOLOR.BLACK, "Black_Queen.png");
                    }
                    else if (r == 7 && c == 3)
                    {
                        Ps[r, c] = new Queen(MYCOLOR.WHITE, "White_Queen.png");
                    }
                    else if (r == 0 && c == 4)
                    {
                        Ps[r, c] = new King(MYCOLOR.BLACK, "Black_King.png");
                    }
                    else if (r == 7 && c == 4)
                    {
                        Ps[r, c] = new King(MYCOLOR.WHITE, "White_King.png");
                    }
                    else
                    {
                        Ps[r, c] = null;
                    }


                    MYCOLOR cc = (((r + c) % 2 == 0) ? MYCOLOR.WHITE : MYCOLOR.BLACK);
                    Cs[r, c]        = new Cell(r, c, cc, Ps[r, c], ChessPanel.Width, ChessPanel.Height, Dimension);
                    Cs[r, c].Click += new System.EventHandler(CellSelected);
                    ChessPanel.Controls.Add(Cs[r, c]);
                }
            }
        }