public void MakeMove_GivenAValidTakingMove_RaisesPieceTakenEvent() { PieceTakenEventArgs pieceTakenEventArgs = null; var chessBoard = new ChessBoard(); var blackKnight = new Knight(Colour.Black); var whitePawn = new Pawn(Colour.White); var boardPosition1 = new BoardPosition("A1"); var boardPosition2 = new BoardPosition("B3"); chessBoard.SetInitialPosition(boardPosition1, blackKnight); chessBoard.SetInitialPosition(boardPosition2, whitePawn); chessBoard.PieceTaken += (sender, e) => { pieceTakenEventArgs = e; }; chessBoard.MakeMove(boardPosition1, boardPosition2); Assert.That(pieceTakenEventArgs, Is.Not.Null); Assert.That(pieceTakenEventArgs.PieceTaken, Is.Not.Null); Assert.That(pieceTakenEventArgs.PieceTaken, Is.InstanceOf<Pawn>()); Assert.That(pieceTakenEventArgs.PieceTaken.Colour, Is.EqualTo(Colour.White)); Assert.That(pieceTakenEventArgs.Square, Is.Not.Null); Assert.That(pieceTakenEventArgs.Square.ChessPiece, Is.Not.Null); Assert.That(pieceTakenEventArgs.Square.ChessPiece, Is.InstanceOf<Knight>()); Assert.That(pieceTakenEventArgs.Square.ChessPiece.Colour, Is.EqualTo(Colour.Black)); }
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; }
/// <summary> /// Determine whether the given move is valid for this chess piece on the given chess board. /// </summary> /// <param name="chessBoard">The chess board on which the chess piece is being moved.</param> /// <param name="from">The board position from which the chess piece is being moved.</param> /// <param name="to">The board position to which the chess piece is being moved.</param> /// <returns></returns> internal virtual MoveResult ValidateMove( ChessBoard chessBoard, BoardPosition from, BoardPosition to, out ExceptionReasonDetail exceptionReasonDetail) { exceptionReasonDetail = ExceptionReasonDetail.None; return MoveResult.Illegal; }
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 static ChessBoard GetChessBoard() { ChessBoard chessBoard = null; try { chessBoard = ScenarioContext.Current.Get<ChessBoard>(CHESSBOARD_KEY); } catch (KeyNotFoundException) { } if (chessBoard == null) { chessBoard = new ChessBoard(); ScenarioContext.Current[CHESSBOARD_KEY] = chessBoard; } return chessBoard; }
public void MakeMove_GivenAnInvalidTakingMove_ThrowsException() { var chessBoard = new ChessBoard(); var blackKnight = new Knight(Colour.Black); var blackPawn = new Pawn(Colour.Black); var boardPosition1 = new BoardPosition("A1"); var boardPosition2 = new BoardPosition("B3"); chessBoard.SetInitialPosition(boardPosition1, blackKnight); chessBoard.SetInitialPosition(boardPosition2, blackPawn); try { chessBoard.MakeMove(boardPosition1, boardPosition2); Assert.Fail("Expected a ChessBoardException to be thrown!"); } catch (ChessBoardException ex) { Assert.That(ex.ExceptionReason, Is.EqualTo(ExceptionReason.IllegalMove)); } }
public void MakeMove_GivenAValidTakingMove_RaisesGameOverEvent() { GameOverEventArgs gameOverEventArgs = null; var chessBoard = new ChessBoard(); var blackKnight = new Knight(Colour.Black); var whitePawn = new Pawn(Colour.White); var boardPosition1 = new BoardPosition("A1"); var boardPosition2 = new BoardPosition("B3"); chessBoard.SetInitialPosition(boardPosition1, blackKnight); chessBoard.SetInitialPosition(boardPosition2, whitePawn); chessBoard.GameOver += (sender, e) => { gameOverEventArgs = e; }; chessBoard.MakeMove(boardPosition1, boardPosition2); Assert.That(gameOverEventArgs, Is.Not.Null); Assert.That(gameOverEventArgs.Result, Is.EqualTo(GameResult.BlackWin)); }
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 SetInitialPosition_GivenTwoPiecesAtSameLocation_ThrowsException() { var chessBoard = new ChessBoard(); var boardPosition = new BoardPosition("A1"); chessBoard.SetInitialPosition(boardPosition, new Knight(Colour.Black)); try { chessBoard.SetInitialPosition(boardPosition, new Pawn(Colour.White)); Assert.Fail("Expected a ChessBoardException to be thrown!"); } catch (ChessBoardException ex) { Assert.That(ex.ExceptionReason, Is.EqualTo(ExceptionReason.InitialPositionAlreadyOccupied)); } }
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)); }