public void BeforeEachTest()
        {
            Piece = new Pawn();
            Target = new Board();

            Target.AddPiece(Piece, new BoardCoordinate(1, 1));
        }
示例#2
0
        public void PerformMovement(Position origin, Position destination)
        {
            Piece capturedPiece = ExecuteMovement(origin, destination);

            if (IsInCheck(ActualPlayer))
            {
                UndoMovement(origin, destination, capturedPiece);
                throw new BoardException("You cannot put your own king in check.");
            }

            Piece piece = Board.Piece(destination);

            // Special move: Promotion
            if (piece is Pawn)
            {
                if ((piece.Color == Color.White && destination.Row == 0) || (piece.Color == Color.Black && destination.Row == 7))
                {
                    piece = Board.RemovePiece(destination);
                    Pieces.Remove(piece);
                    Piece queen = new Queen(Board, piece.Color);
                    Board.AddPiece(queen, destination);
                    Pieces.Add(queen);
                }
            }

            if (IsInCheck(AdversaryColor(ActualPlayer)))
            {
                Check = true;
            }
            else
            {
                Check = false;
            }

            if (TestCheckMate(AdversaryColor(ActualPlayer)))
            {
                EndedMatch = true;
            }
            else
            {
                Turn++;
                ChangePlayer();
            }

            //Special move: En Passant
            if (piece is Pawn && (destination.Row == origin.Row - 2 || destination.Row == origin.Row + 2))
            {
                VulnerableToEnPassant = piece;
            }
            else
            {
                VulnerableToEnPassant = null;
            }
        }
示例#3
0
        public Piece ExecuteMovement(Position origin, Position destination)
        {
            Piece piece = Board.RemovePiece(origin);

            piece.IncreaseMovementCount();
            Piece capturedPiece = Board.RemovePiece(destination);

            Board.AddPiece(piece, destination);
            if (capturedPiece != null)
            {
                Captured.Add(capturedPiece);
            }

            //Special move: Castling
            if ((piece is King) && (destination.Column == origin.Column + 2))
            {
                Position originR      = new Position(origin.Row, origin.Column + 3);
                Position destinationR = new Position(origin.Row, origin.Column + 1);
                Piece    R            = Board.RemovePiece(originR);
                R.IncreaseMovementCount();
                Board.AddPiece(R, destinationR);
            }

            if ((piece is King) && (destination.Column == origin.Column - 2))
            {
                Position originR      = new Position(origin.Row, origin.Column - 4);
                Position destinationR = new Position(origin.Row, origin.Column - 1);
                Piece    R            = Board.RemovePiece(originR);
                R.IncreaseMovementCount();
                Board.AddPiece(R, destinationR);
            }

            //Special move: En Passant
            if (piece is Pawn)
            {
                if ((origin.Column != destination.Column) && (capturedPiece == null))
                {
                    Position positionP;
                    if (piece.Color == Color.White)
                    {
                        positionP = new Position(destination.Row + 1, destination.Column);
                    }
                    else
                    {
                        positionP = new Position(destination.Row - 1, destination.Column);
                    }
                    capturedPiece = Board.RemovePiece(positionP);
                    Captured.Add(capturedPiece);
                }
            }

            return(capturedPiece);
        }
示例#4
0
        public void AddPiece(BoardCoordinate square, string pieceString)
        {
            VerifyNonEmptyPieceStringOrThrow(pieceString);

            var pieceCode     = pieceString.Substring(1);
            var playerCode    = pieceString[0];
            var isFirstPlayer = DetermineIsFirstPlayer(playerCode);

            var pieceToAdd = BuildPiece(pieceCode, isFirstPlayer);

            _board.AddPiece(pieceToAdd, square);
        }
示例#5
0
        private void UndoMovement(Position origin, Position destination, Piece capturedPiece)
        {
            Piece piece = Board.RemovePiece(destination);

            piece.DecreaseMovementCount();
            if (capturedPiece != null)
            {
                Board.AddPiece(capturedPiece, destination);
                Captured.Remove(capturedPiece);
            }
            Board.AddPiece(piece, origin);

            //Special move: Castling
            if ((piece is King) && (destination.Column == origin.Column + 2))
            {
                Position originR      = new Position(origin.Row, origin.Column + 3);
                Position destinationR = new Position(origin.Row, origin.Column + 1);
                Piece    R            = Board.RemovePiece(destinationR);
                R.DecreaseMovementCount();
                Board.AddPiece(R, originR);
            }

            if ((piece is King) && (destination.Column == origin.Column - 2))
            {
                Position originR      = new Position(origin.Row, origin.Column - 4);
                Position destinationR = new Position(origin.Row, origin.Column - 1);
                Piece    R            = Board.RemovePiece(destinationR);
                R.DecreaseMovementCount();
                Board.AddPiece(R, originR);
            }

            //Special move: En Passant
            if (piece is Pawn)
            {
                if ((origin.Column != destination.Column) && (capturedPiece == VulnerableToEnPassant))
                {
                    Piece    pawn = Board.RemovePiece(destination);
                    Position positionP;
                    if (piece.Color == Color.White)
                    {
                        positionP = new Position(3, destination.Column);
                    }
                    else
                    {
                        positionP = new Position(4, destination.Column);
                    }
                    Board.AddPiece(pawn, positionP);
                }
            }
        }
示例#6
0
文件: Player.cs 项目: lalaalal/Chess
        public Player(Team team, Board board)
        {
            Team       = team;
            this.board = board;

            king = new King(Team);
            board.AddPiece(king);
            board.AddPiece(new Queen(Team));

            board.AddPiece(new Rook(0, team));
            board.AddPiece(new Rook(7, team));
            board.AddPiece(new Knight(1, team));
            board.AddPiece(new Knight(6, team));
            board.AddPiece(new Bishop(2, team));
            board.AddPiece(new Bishop(5, team));

            for (int i = 0; i < 8; i++)
            {
                board.AddPiece(new Pawn(i, team));
            }
        }
示例#7
0
文件: Player.cs 项目: lalaalal/Chess
        public bool MovePiece(Point from, Point to)
        {
            if (board.IsEmpty(from))
            {
                return(false);
            }
            if (king.IsEnemy(board[from]))
            {
                return(false);
            }

            Piece piece = board.PopPiece(from);

            if (IsCheck())
            {
                board.AddPiece(piece);
                return(false);
            }


            return(board.MovePiece(from, to));
        }
示例#8
0
 public void AddNewPiece(char column, int row, Piece piece)
 {
     Board.AddPiece(piece, new ChessPosition(column, row).ToPosition());
     Pieces.Add(piece);
 }