/// <summary> /// Adds a piece to the board if the square isn't occupied /// </summary> /// <param name="piece">The piece to add to the board</param> /// <param name="x">The x coordinate of the square to place the piece at</param> /// <param name="y">The y coordinate of the square to place the piece at</param> /// <returns>True if the piece was added</returns> public bool Add(IChessPiece piece, int xCoordinate, int yCoordinate, PieceColor color) { if (!IsLegalBoardPosition(xCoordinate, yCoordinate)) { RemoveFromBoard(piece); return(false); } if (_piecesOnBoard[(int)PieceType.Pawn, (int)piece.Color].Count == _settings.MaxPawnsPerSide) { RemoveFromBoard(piece); return(false); } if (_pieces[xCoordinate, yCoordinate] == null) { _pieces[xCoordinate, yCoordinate] = piece; piece.XCoordinate = xCoordinate; piece.YCoordinate = yCoordinate; _piecesOnBoard[(int)PieceType.Pawn, (int)piece.Color].Add(piece); piece.SetBoard(this); return(true); } else { RemoveFromBoard(piece); return(false); } }
public void ChessPiece_Move_Sets_Y_Coordinate_For_Valid_Move() { IChessPiece piece = CreatePiece(PieceType.Pawn, PieceColor.Black); _chessBoard.Add(Arg.Any <IChessPiece>(), Arg.Do <int>(x => piece.XCoordinate = x), Arg.Do <int>(y => piece.YCoordinate = y), piece.Color).Returns(true).AndDoes(x => piece.SetBoard(_chessBoard)); _chessBoard.IsMoveValid(Arg.Any <Move>()).Returns(true); _chessBoard.Add(piece, 3, 6, piece.Color); piece.Move(MovementType.Move, 3, 4); Assert.That(piece.YCoordinate, Is.EqualTo(4)); }
private void RemoveFromBoard(IChessPiece piece) { piece.XCoordinate = OffBoardCoordinate; piece.YCoordinate = OffBoardCoordinate; piece.SetBoard(null); }