Esempio n. 1
0
        public void Constructor_CreateBoardWithOtherBoard_Success()
        {
            empty_2x2_board.AddPiece(new Pawn(new Position(1, 0), white, Position.Up));

            Chessboard new_board = new Chessboard(empty_2x2_board);

            Assert.IsTrue(new_board.CompareBoardStates(empty_2x2_board));
        }
Esempio n. 2
0
        public void Revert(Chessboard board)
        {
            if (board == null)
            {
                throw new ArgumentNullException("Board can't be null");
            }

            ChessPiece movePiece     = board.GetPiece(EndPosition);
            var        startTileInfo = board.GetTileInfo(StartPosition);
            var        endTileInfo   = board.GetTileInfo(EndPosition);

            if (movePiece == null)
            {
                throw new NullPieceException("Can't revert move on empty square");
            }

            if (!startTileInfo.IsValid)
            {
                throw new InvalidPositionException(String.Format("Start position {0} is invalid in this board", StartPosition.ToString()));
            }

            if (!endTileInfo.IsValid)
            {
                throw new InvalidPositionException(String.Format("End position {0} is invalid in this board", EndPosition.ToString()));
            }

            board.MovePiece(EndPosition, StartPosition);

            movePiece.MoveCount--;

            if (Captures != null)
            {
                foreach (var capture in Captures)
                {
                    TileInfo tile = board.GetTileInfo(capture.Position);

                    if (!tile.IsValid)
                    {
                        throw new InvalidPositionException(String.Format("Capture's position {0} is invalid in this board", capture.Position.ToString()));
                    }

                    if (capture.Piece == null)
                    {
                        throw new IndexOutOfRangeException(String.Format("Capture's position {0} doesn't have a piece", capture.Position.ToString()));
                    }

                    board.AddPiece(capture.Piece);
                }
            }

            if (ExtraMoves != null)
            {
                foreach (var extraMove in ExtraMoves)
                {
                    extraMove.Revert(board);
                }
            }
        }
Esempio n. 3
0
        public void Execute(Chessboard board)
        {
            if (board == null || Piece == null)
            {
                throw new ArgumentNullException("Board or piece can't be null");
            }

            Piece.CurrentPosition = Position;

            board.AddPiece(Piece);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates the default chess board
        /// </summary>
        /// <exception cref="InvalidPlayerException"> </exception>
        /// <exception cref="ArgumentNullException"> </exception>
        public Chessboard CreateBoard(DefaultChessConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config", "Configuration can't be null");
            }

            try { ValidatePlayer(config.White); }
            catch (InvalidPlayerException e) { throw new InvalidPlayerException("Invalid white side Player: " + e.Message); }

            try { ValidatePlayer(config.Black); }
            catch (InvalidPlayerException e) { throw new InvalidPlayerException("Invalid black side Player: " + e.Message); }

            if (config.White.Id == config.Black.Id)
            {
                throw new InvalidPlayerException("Players can't have the same id");
            }

            var board = new Chessboard(BOARD_SIZE, BOARD_SIZE);

            for (int i = 0; i < board.Width; i++)
            {
                board.AddPiece(new Pawn(new Position(i, 1), config.White, Position.Up));
                board.AddPiece(new Pawn(new Position(i, 6), config.Black, Position.Down));
            }

            board.AddPiece(new Rook(new Position(0, 0), config.White));
            board.AddPiece(new Rook(new Position(7, 0), config.White));
            board.AddPiece(new Rook(new Position(0, 7), config.Black));
            board.AddPiece(new Rook(new Position(7, 7), config.Black));

            board.AddPiece(new Knight(new Position(1, 0), config.White));
            board.AddPiece(new Knight(new Position(6, 0), config.White));
            board.AddPiece(new Knight(new Position(1, 7), config.Black));
            board.AddPiece(new Knight(new Position(6, 7), config.Black));

            board.AddPiece(new Bishop(new Position(2, 0), config.White));
            board.AddPiece(new Bishop(new Position(5, 0), config.White));
            board.AddPiece(new Bishop(new Position(2, 7), config.Black));
            board.AddPiece(new Bishop(new Position(5, 7), config.Black));

            board.AddPiece(new Queen(new Position(3, 0), config.White));
            board.AddPiece(new King(new Position(4, 0), config.White));
            board.AddPiece(new Queen(new Position(3, 7), config.Black));
            board.AddPiece(new King(new Position(4, 7), config.Black));

            return(board);
        }