示例#1
0
        public void PerformChessMove(Position source, Position target)
        {
            Piece capturedPiece = PerformMove(source, target);

            if (IsACheck(CurrentPlayer))
            {
                UndoMove(source, target, capturedPiece);
                throw new BoardException("You can't put yourself in Check!");
            }

            Piece p = Board.Piece(target);

            // #Special move - En Passant
            if (p is Pawn)
            {
                if ((p.Color == Color.White && target.Row == 0) || (p.Color == Color.Black && target.Row == 7))
                {
                    p = Board.RemovePiece(target);
                    Pieces.Remove(p);
                    Piece queen = new Queen(Board, p.Color);
                    Board.PlacePiece(queen, target);
                    Pieces.Add(queen);
                }
            }

            if (IsACheck(OpponentPlayer(CurrentPlayer)))
            {
                Check = true;
            }
            else
            {
                Check = false;
            }

            if (IsACheckMate(OpponentPlayer(CurrentPlayer)))
            {
                GameOver = true;
            }
            else
            {
                Turn++;
                ChangePlayer();
            }

            // #Special move - En Passant

            if (p is Pawn && (target.Row == source.Row - 2 || target.Row == source.Row + 2))
            {
                EnPassantVulnerable = p;
            }
            else
            {
                EnPassantVulnerable = null;
            }
        }
示例#2
0
        public void Initialize()
        {
            //Initialize all squares
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    Square square = new Square();
                    square.File = (File)(i);
                    square.Rank = j;
                    BoardCoordinates.Add(square);
                }
            }

            //Initialize all pieces

            int whiteMajorPieceRank = 1;
            int whitePawnRank       = 2;
            int blackPawnRank       = 7;
            int blackMajorPieceRank = 8;

            File queenRookFile   = File.A;
            File queenKnightFile = File.B;
            File queenBishopFile = File.C;
            File queenFile       = File.D;
            File kingFile        = File.E;
            File kingBishopFile  = File.F;
            File kingKnightFile  = File.G;
            File kingRookFile    = File.H;

            #region Pawns
            for (int i = 0; i < 16; i++)
            {
                Pawn pawn = new Pawn();
                pawn.PieceColor       = i < 8 ? PieceColor.White : PieceColor.Black;
                pawn.Coordinates.Rank = i < 8 ? whitePawnRank : blackPawnRank;
                pawn.Coordinates.File = (File)(i % 8);
                pawn.IsOnBoard        = true;
                Pieces.Add(pawn);
            }
            #endregion

            #region Rooks
            Rook whiteKingRook = new Rook();
            whiteKingRook.Coordinates.Rank = whiteMajorPieceRank;
            whiteKingRook.Coordinates.File = kingRookFile;
            whiteKingRook.IsOnBoard        = true;
            whiteKingRook.PieceColor       = PieceColor.White;
            Pieces.Add(whiteKingRook);

            Rook whiteQueenRook = new Rook();
            whiteQueenRook.Coordinates.Rank = whiteMajorPieceRank;
            whiteQueenRook.Coordinates.File = queenRookFile;
            whiteQueenRook.IsOnBoard        = true;
            whiteQueenRook.PieceColor       = PieceColor.White;
            Pieces.Add(whiteQueenRook);

            Rook blackKingRook = new Rook();
            blackKingRook.Coordinates.Rank = blackMajorPieceRank;
            blackKingRook.Coordinates.File = kingRookFile;
            blackKingRook.IsOnBoard        = true;
            blackKingRook.PieceColor       = PieceColor.Black;
            Pieces.Add(blackKingRook);

            Rook blackQueenRook = new Rook();
            blackQueenRook.Coordinates.File = queenRookFile;
            blackQueenRook.Coordinates.Rank = blackMajorPieceRank;
            blackQueenRook.IsOnBoard        = true;
            blackQueenRook.PieceColor       = PieceColor.Black;
            Pieces.Add(blackQueenRook);

            #endregion

            #region Knights
            Knight whiteQueenKnight = new Knight();
            whiteQueenKnight.Coordinates.File = queenKnightFile;
            whiteQueenKnight.Coordinates.Rank = whiteMajorPieceRank;
            whiteQueenKnight.IsOnBoard        = true;
            whiteQueenKnight.PieceColor       = PieceColor.White;
            Pieces.Add(whiteQueenKnight);

            Knight whiteKingKnight = new Knight();
            whiteKingKnight.Coordinates.File = kingKnightFile;
            whiteKingKnight.Coordinates.Rank = whiteMajorPieceRank;
            whiteKingKnight.IsOnBoard        = true;
            whiteKingKnight.PieceColor       = PieceColor.White;
            Pieces.Add(whiteKingKnight);

            Knight blackQueenKnight = new Knight();
            blackQueenKnight.Coordinates.File = queenKnightFile;
            blackQueenKnight.Coordinates.Rank = blackMajorPieceRank;
            blackQueenKnight.IsOnBoard        = true;
            blackQueenKnight.PieceColor       = PieceColor.Black;
            Pieces.Add(blackQueenKnight);

            Knight blackKingKnight = new Knight();
            blackKingKnight.Coordinates.File = kingKnightFile;
            blackKingKnight.Coordinates.Rank = blackMajorPieceRank;
            blackQueenKnight.IsOnBoard       = true;
            blackQueenKnight.PieceColor      = PieceColor.Black;
            Pieces.Add(blackKingKnight);
            #endregion

            #region Bishop
            Bishop whiteKingBishop = new Bishop();
            whiteKingBishop.Coordinates.Rank = whiteMajorPieceRank;
            whiteKingBishop.Coordinates.File = kingBishopFile;
            whiteKingBishop.IsOnBoard        = true;
            whiteKingBishop.PieceColor       = PieceColor.White;
            Pieces.Add(whiteKingBishop);

            Bishop whiteQueenBishop = new Bishop();
            whiteQueenBishop.Coordinates.Rank = whiteMajorPieceRank;
            whiteQueenBishop.Coordinates.File = queenBishopFile;
            whiteQueenBishop.IsOnBoard        = true;
            whiteQueenBishop.PieceColor       = PieceColor.White;
            Pieces.Add(whiteQueenBishop);

            Bishop blackKingBishop = new Bishop();
            blackKingBishop.Coordinates.Rank = blackMajorPieceRank;
            blackKingBishop.Coordinates.File = kingBishopFile;
            blackKingBishop.IsOnBoard        = true;
            blackKingBishop.PieceColor       = PieceColor.Black;
            Pieces.Add(blackKingBishop);

            Bishop blackQueenBishop = new Bishop();
            blackQueenBishop.Coordinates.Rank = blackMajorPieceRank;
            blackQueenBishop.Coordinates.File = queenBishopFile;
            blackQueenBishop.IsOnBoard        = true;
            blackQueenBishop.PieceColor       = PieceColor.Black;
            Pieces.Add(blackQueenBishop);

            #endregion

            #region Queens
            Queen whiteQueen = new Queen();
            whiteQueen.Coordinates.Rank = whiteMajorPieceRank;
            whiteQueen.Coordinates.File = queenFile;
            whiteQueen.IsOnBoard        = true;
            whiteQueen.PieceColor       = PieceColor.White;
            Pieces.Add(whiteQueen);

            Queen blackQueen = new Queen();
            blackQueen.Coordinates.Rank = blackMajorPieceRank;
            blackQueen.Coordinates.File = queenFile;
            blackQueen.IsOnBoard        = true;
            blackQueen.PieceColor       = PieceColor.Black;
            Pieces.Add(blackQueen);

            #endregion

            #region Kings
            King whiteKing = new King();
            whiteKing.Coordinates.Rank = whiteMajorPieceRank;
            whiteKing.Coordinates.File = kingFile;
            whiteKing.IsOnBoard        = true;
            whiteKing.PieceColor       = PieceColor.White;
            Pieces.Add(whiteKing);

            King blackKing = new King();
            blackKing.Coordinates.File = kingFile;
            blackKing.Coordinates.Rank = blackMajorPieceRank;
            blackKing.IsOnBoard        = true;
            blackKing.PieceColor       = PieceColor.Black;
            Pieces.Add(blackKing);
            #endregion
        }
示例#3
0
 public void PlaceNewPiece(char column, int row, Piece p)
 {
     Board.PlacePiece(p, new ChessPosition(column, row).ToPosition());
     Pieces.Add(p);
 }
示例#4
0
 /// <summary>
 /// This constructor takes 2 ChessPiece objects as parameters
 /// </summary>
 /// <param name="first">First Piece</param>
 /// <param name="second">Second Piece</param>
 public PositionAnalyzer(ChessPiece first, ChessPiece second)
 {
     Pieces.Add(first);
     Pieces.Add(second);
 }