public bool AlongFileOrRank(Square destination) { if (AlongRank(destination)) return true; if (AlongFile(destination)) return true; return false; }
public VisualPawn(Canvas gameBoard, Square square) { _gameBoard = gameBoard; _square = square; // _definingGeometry = CreateGeometry(); // RenderTransform = CreateTransform(); Name = CreateName(square); }
public void GetPosition_SecondFileAndRankWhenBoardIsEmpty_ReturnsEmptyPosition() { Square expected = new Square(2, 2, new NullPiece(Board)); Square actual = GetSquare(2, 2); Assert.AreEqual(expected, actual); }
public bool DiagonallyForwardTo(Square destination) { if (Math.Abs(File - destination.File) != Math.Abs(Rank - destination.Rank)) return false; if (DiagonallyForwardToLeftForWhite(destination)) return true; if (DiagonallyForwardToRightForWhite(destination)) return true; if (DiagonallyForwardToLeftForBlack(destination)) return true; if (DiagonallyForwardToRightForBlack(destination)) return true; return false; }
public bool DiagonallyTo(Square destination) { if (Math.Abs(File - destination.File) != Math.Abs(Rank - destination.Rank)) return false; if (File < destination.File && Rank < destination.Rank) return true; if (File > destination.File && Rank > destination.Rank) return true; if (File > destination.File && Rank < destination.Rank) return true; if (File < destination.File && Rank > destination.Rank) return true; return false; }
public bool MoveIsLegal(Square origin, Square destination) { if (AreOutsideBoardBoundaries(origin.File, origin.Rank)) return false; if (AreOutsideBoardBoundaries(destination.File, destination.Rank)) return false; var occupier = origin.Occupier; var movingStrategy = occupier.GetMovingStrategy(); return occupier.IsLegalMove(origin, destination) || movingStrategy.IsSpecialMove(origin, destination); }
private static bool PlayerMovedPawnToPromotionRank(Square from, Square to) { if (!(from.Occupier is Pawn)) return false; var playerMovedPawnToPromotionRank = false; if (from.Color == PieceColor.White) playerMovedPawnToPromotionRank = from.Rank == 7 && to.Rank == 8; if (from.Color == PieceColor.Black) playerMovedPawnToPromotionRank = from.Rank == 2 && to.Rank == 1; return playerMovedPawnToPromotionRank; }
private void UpdateGameState(Square from, Square to, Position newPosition, PieceColor playerWhoDidTheMove) { if (PlayerMovedPawnToPromotionRank(from, to)) { State = GameState.Promotion; } else if (newPosition.WhiteKingIsInCheck() || newPosition.BlackKingIsInCheck()) { State = newPosition.HasLegalMoves(PlayerToMove) ? GameState.Check : GameState.CheckMate; } else { State = newPosition.HasLegalMoves(PlayerToMove) && newPosition.HasLegalMoves(playerWhoDidTheMove) ? GameState.Normal : GameState.StaleMate; } }
public void SetPosition_FirstFileAndRankWhenBoardIsEmpty_GetPositionForSecondFileAndRankReturnsEmptyPosition() { Square expected = new Square(2, 2, new NullPiece(Board)); Board.SetSquare(1, 1, new Pawn(Board, PieceColor.White)); Square actual = GetSquare(2, 2); Assert.AreEqual(expected, actual); }
protected bool IsLegalMove(Square origin, Square destination) { var position = Board.GetPosition(); return position.MoveIsLegal(origin, destination); }
public void SetPosition_BlackRookToFirstFileAndEightRankWhenBoardIsEmpty_GetPositionReturnsTheSetValue() { var expected = new Square(1, 8, new Rook(Board, PieceColor.Black)); Board.SetSquare(1, 8, new Rook(Board, PieceColor.Black)); var actual = GetSquare(1, 8); Assert.AreEqual(expected, actual); }
private bool KingIsInCheck(Square squareOccupiedByTheKing, IEnumerable<Square> squaresOccupiedByTheOppositeColor) { if (squareOccupiedByTheKing == null || squaresOccupiedByTheOppositeColor == null) return false; var kingIsInCheck = false; foreach (var squareOccupiedByTheOppositeColor in squaresOccupiedByTheOppositeColor) { if (MoveIsLegal(squareOccupiedByTheOppositeColor, squareOccupiedByTheKing)) { kingIsInCheck = true; break; } } return kingIsInCheck; }
public VisualPawn(Canvas pieceLayer, Square square) { _pieceLayer = pieceLayer; _square = square; }
public void SetPosition_WhiteRookToFirstFileAndRankWhenBoardIsEmpty_GetPositionReturnsTheSetValue() { var expected = new Square(1, 1, new Rook(Board, PieceColor.White)); Board.SetSquare(1, 1, new Rook(Board, PieceColor.White)); var actual = GetSquare(1, 1); Assert.AreEqual(expected, actual); }
public bool AlongFile(Square destination) { return Rank == destination.Rank && File > destination.File || Rank == destination.Rank && File < destination.File; }
public bool DistanceOfRanksIsZeroTo(Square destination) { return (Math.Abs(Rank - destination.Rank) == 0); }
private Position SimulateMove(Square origin, Square destination) { var simulationBoard = new Board(); foreach (var square in _allSquares) simulationBoard.SetSquare(square.File, square.Rank, square.Occupier.Clone(simulationBoard)); simulationBoard.Move(simulationBoard.GetSquare(origin.File, origin.Rank), simulationBoard.GetSquare(destination.File, destination.Rank)); return simulationBoard.GetPosition(); }
private bool DiagonallyForwardToRightForBlack(Square destination) { return Color == PieceColor.Black && File > destination.File && Rank > destination.Rank; }
public bool ForwardTo(Square destination) { if (Color == PieceColor.White && Rank < destination.Rank && File == destination.File) return true; if (Color == PieceColor.Black && Rank > destination.Rank && File == destination.File) return true; return false; }
public override void OnMove(Square from, Square to, Position newPosition) { var playerWhoDidTheMove = from.Color; UpdatePlayerToMove(playerWhoDidTheMove); UpdateGameState(from, to, newPosition, playerWhoDidTheMove); }
public bool DistanceOfFilesIsNotTwoTo(Square destination) { return (Math.Abs(File - destination.File) != 2); }
private bool DiagonallyForwardToRightForWhite(Square destination) { return Color == PieceColor.White && File > destination.File && Rank < destination.Rank; }
public void SetPosition_FirstFileAndRankWhenBoardIsEmpty_GetPositionReturnsTheValueThatWasSet() { Square expected = new Square(1, 1, new Pawn(Board, PieceColor.White)); Board.SetSquare(1, 1, new Pawn(Board, PieceColor.White)); Square actual = GetSquare(1, 1); Assert.AreEqual(expected, actual); }
public bool MoveIsIllegal(Square origin, Square destination) { return !MoveIsLegal(origin, destination); }
public void SetPosition_WhiteRookToFirstFileAndRankWhenBoardIsEmpty_GetPositionFromDifferingCoordinatesIsNotEqual() { var expected = new Square(1, 1, new Rook(Board, PieceColor.White)); Board.SetSquare(1, 1, new Rook(Board, PieceColor.White)); var actual = GetSquare(2, 4); Assert.AreNotEqual(expected, actual); }
public bool Equals(Square other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other.Occupier, Occupier) && other._rank == _rank && other._file == _file; }
private bool KingIsReleasedBy(Square origin, Square destination) { if (MoveIsIllegal(origin, destination)) return false; var simulatedPosition = SimulateMove(origin, destination); var squareOccupiedByTheKing = simulatedPosition.SquareOccupiedByTheKingWith(origin.Color); var oppositeColorPieces = simulatedPosition.SquaresOccupiedByPiecesWith(origin.Color.GetOppositeColor()); return !simulatedPosition.KingIsInCheck(squareOccupiedByTheKing, oppositeColorPieces); }
public bool AlongRank(Square destination) { return Rank > destination.Rank && File == destination.File || Rank < destination.Rank && File == destination.File; }
private static string CreateName(Square square) { return square.Occupier.GetType().Name + Random.Next(100000000, 1000000000).ToString(CultureInfo.InvariantCulture); }
public bool DistanceOfFilesIsZeroTo(Square destination) { return (Math.Abs(File - destination.File) == 0); }