public void Get_Piece_Added_To_Position() { var pawn = Substitute.For <IChessPiece>(); _chessBoard.Add(pawn, 0, 0, pawn.Color); IChessPiece piece; Assert.That(_chessBoard.TryGetPieceOn(0, 0, out piece), Is.True); Assert.That(piece, Is.SameAs(pawn)); }
private bool CheckEnPassant(IChessBoard board, Move move) { // en passant can only apply on ranks three from the enemies home if (move.Piece.Color == PieceColor.White && move.EndingY != board.Height - 3) { return(false); } if (move.Piece.Color == PieceColor.Black && move.EndingY != 2) { return(false); } // get the piece behind the target move point int forwardDirection = move.Piece.Color == PieceColor.White ? 1 : -1; IChessPiece targetPiece = null; bool targetSquareOccupied = board.TryGetPieceOn(move.EndingX, move.EndingY - forwardDirection, out targetPiece); if (!targetSquareOccupied) { return(false); } if (!(targetPiece is IPawn)) { return(false); } // En Passant can only be applied on the next turn after the piece moves. IPawn pawn = targetPiece as IPawn; return(board.CurrentTurn == pawn.FirstMovedOn + 1); }
public bool IsMoveValid(IChessBoard board, Move move) { IChessPiece capture; bool occupied = board.TryGetPieceOn(move.EndingX, move.EndingY, out capture); return(!occupied || move.Piece.Color != capture.Color); }
private void UnoccupyBoard(IChessBoard board) { IChessPiece outVar = null; board.TryGetPieceOn(Arg.Any <int>(), Arg.Any <int>(), out outVar).Returns(args => { args[2] = null; return(false); }); }
private void OccupyRow(IChessBoard board, PieceColor color, int y) { IChessPiece outVar = null; var occupier = Substitute.For <IPawn>(); occupier.Color.Returns(color); occupier.FirstMovedOn.Returns(1); board.TryGetPieceOn(Arg.Any <int>(), y, out outVar).Returns(args => { args[2] = occupier; return(true); }); board.TryGetPieceOn(Arg.Any <int>(), Arg.Is <int>(y1 => y1 != y), out outVar).Returns(args => { args[2] = null; return(false); }); }
/// <summary> /// Checks board state to see if the move is valid. /// </summary> /// <param name="board">Board state to validate against</param> /// <param name="move">Move to be performed</param> /// <returns>True if the move is legal.</returns> public bool IsMoveValid(IChessBoard board, Move move) { int forwardDirection = move.Piece.Color == PieceColor.White ? 1 : -1; // pawns can only move forwards if (((move.EndingY - move.StartingY) * forwardDirection) <= 0) { return(false); } // pawns can only move forwards two squares at most if (((move.EndingY - move.StartingY) * forwardDirection) > 2) { return(false); } // pawns cannot move horiztally more than one square if (move.StartingX - move.EndingX > 1 || move.EndingX - move.StartingX > 1) { return(false); } // pawns can only move forward two squares from their home rank if (move.EndingY - move.StartingY == 2) // for white { if (move.Piece.Color == PieceColor.Black) { return(false); } if (move.StartingY != 1) { return(false); } if (move.StartingX != move.EndingX) { return(false); // cannot move horizontally during double step } } if (move.StartingY - move.EndingY == 2) // for black { if (move.Piece.Color == PieceColor.White) { return(false); } if (move.StartingY != 6) { return(false); } if (move.StartingX != move.EndingX) { return(false); // cannot move horizontally during double step } } IChessPiece targetPiece = null; bool targetSquareOccupied = board.TryGetPieceOn(move.EndingX, move.EndingY, out targetPiece); // pawns cannot move directly forward into an occupied square if (move.StartingX - move.EndingX == 0) { return(!targetSquareOccupied); } // capture move check if (move.StartingX - move.EndingX == 1 || move.EndingX - move.StartingX == 1) { return((targetSquareOccupied && targetPiece.Color != move.Piece.Color) || CheckEnPassant(board, move)); } return(false); }