internal override MoveResult ValidateMove( ChessBoard chessBoard, BoardPosition from, BoardPosition to, out ExceptionReasonDetail exceptionReasonDetail) { exceptionReasonDetail = ExceptionReasonDetail.None; MoveResult result = MoveResult.Illegal; int fileDiff = Math.Abs(from.InternalFile - to.InternalFile); int forward = (this.Colour == Colour.White) ? to.InternalRank - from.InternalRank : from.InternalRank - to.InternalRank; Square toSquare = chessBoard.GetSquare(to); if (forward == 1) { if (fileDiff == 0) { if (toSquare.ChessPiece != null) { result = MoveResult.Collision; } else { result = MoveResult.Valid; } } if (fileDiff == 1) { if (toSquare.ChessPiece != null && toSquare.ChessPiece.Colour != this.Colour) { result = MoveResult.Valid; } else { exceptionReasonDetail = ExceptionReasonDetail.PawnCannotMoveDiagonallyUnlessCapturing; } } } if (forward == 2 && fileDiff == 0) { bool onHomeRank = (this.Colour == Colour.White && from.Rank == 2) || (this.Colour == Colour.Black && from.Rank == 7); if (onHomeRank && chessBoard.Round == 1) { int inbetweenFile = from.InternalFile; int inbetweenRank = (this.Colour == Colour.White) ? from.InternalRank + 1 : from.InternalRank - 1; Square inbetweenSquare = chessBoard.GetSquare(new BoardPosition(inbetweenFile, inbetweenRank)); if (inbetweenSquare.ChessPiece != null) { result = MoveResult.Collision; } else { if (toSquare.ChessPiece == null) { result = MoveResult.Valid; } } } else { exceptionReasonDetail = ExceptionReasonDetail.PawnCannotMoveTwoSpacesAtThisTime; } } return result; }
internal override MoveResult ValidateMove( ChessBoard chessBoard, BoardPosition from, BoardPosition to, out ExceptionReasonDetail exceptionReasonDetail) { exceptionReasonDetail = ExceptionReasonDetail.None; MoveResult result = MoveResult.Illegal; int fileDiff = Math.Abs(from.InternalFile - to.InternalFile); int rankDiff = Math.Abs(from.InternalRank - to.InternalRank); if ((fileDiff == 2 && rankDiff == 1) || (fileDiff == 1 && rankDiff == 2)) { Square toSquare = chessBoard.GetSquare(to); if (toSquare.ChessPiece == null || toSquare.ChessPiece.Colour != this.Colour) { result = MoveResult.Valid; } } return result; }
public void MakeMove_GivenAValidMove_Succeeds() { var chessBoard = new ChessBoard(); var blackKnight = new Knight(Colour.Black); var boardPosition1 = new BoardPosition("A1"); var boardPosition2 = new BoardPosition("B3"); chessBoard.SetInitialPosition(boardPosition1, blackKnight); chessBoard.MakeMove(boardPosition1, boardPosition2); Assert.That(chessBoard.GetSquare(boardPosition1), Is.Not.Null); Assert.That(chessBoard.GetSquare(boardPosition1).ChessPiece, Is.Null); Assert.That(chessBoard.GetSquare(boardPosition2), Is.Not.Null); Assert.That(chessBoard.GetSquare(boardPosition2).ChessPiece, Is.Not.Null); Assert.That(chessBoard.GetSquare(boardPosition2).ChessPiece, Is.InstanceOf<Knight>()); Assert.That(chessBoard.GetSquare(boardPosition2).ChessPiece.Colour, Is.EqualTo(Colour.Black)); }
public void SetInitialPosition_GivenKnightAtA1_Succeeds() { var chessBoard = new ChessBoard(); var boardPosition = new BoardPosition("A1"); chessBoard.SetInitialPosition(boardPosition, new Knight(Colour.Black)); Assert.That(chessBoard.GetSquare(boardPosition), Is.Not.Null); Assert.That(chessBoard.GetSquare(boardPosition).ChessPiece, Is.Not.Null); Assert.That(chessBoard.GetSquare(boardPosition).ChessPiece, Is.InstanceOf<Knight>()); Assert.That(chessBoard.GetSquare(boardPosition).ChessPiece.Colour, Is.EqualTo(Colour.Black)); }