Exemple #1
0
        public bool AlongFileOrRank(Square destination)
        {
            if (AlongRank(destination)) return true;
            if (AlongFile(destination)) return true;

            return false;
        }
Exemple #2
0
 public VisualPawn(Canvas gameBoard, Square square)
 {
     _gameBoard = gameBoard;
     _square = square;
     //            _definingGeometry = CreateGeometry();
     //            RenderTransform = CreateTransform();
     Name = CreateName(square);
 }
Exemple #3
0
        public void GetPosition_SecondFileAndRankWhenBoardIsEmpty_ReturnsEmptyPosition()
        {
            Square expected = new Square(2, 2, new NullPiece(Board));

            Square actual = GetSquare(2, 2);

            Assert.AreEqual(expected, actual);
        }
Exemple #4
0
        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;
        }
Exemple #5
0
        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;
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        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;
        }
Exemple #8
0
 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;
     }
 }
Exemple #9
0
        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);
 }
Exemple #11
0
        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);
        }
Exemple #12
0
        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;
        }
Exemple #13
0
 public VisualPawn(Canvas pieceLayer, Square square)
 {
     _pieceLayer = pieceLayer;
     _square = square;
 }
Exemple #14
0
        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);
        }
Exemple #15
0
 public bool AlongFile(Square destination)
 {
     return Rank == destination.Rank && File > destination.File
            || Rank == destination.Rank && File < destination.File;
 }
Exemple #16
0
 public bool DistanceOfRanksIsZeroTo(Square destination)
 {
     return (Math.Abs(Rank - destination.Rank) == 0);
 }
Exemple #17
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();
        }
Exemple #18
0
 private bool DiagonallyForwardToRightForBlack(Square destination)
 {
     return Color == PieceColor.Black
            && File > destination.File && Rank > destination.Rank;
 }
Exemple #19
0
        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;
        }
Exemple #20
0
 public override void OnMove(Square from, Square to, Position newPosition)
 {
     var playerWhoDidTheMove = from.Color;
     UpdatePlayerToMove(playerWhoDidTheMove);
     UpdateGameState(from, to, newPosition, playerWhoDidTheMove);
 }
Exemple #21
0
 public bool DistanceOfFilesIsNotTwoTo(Square destination)
 {
     return (Math.Abs(File - destination.File) != 2);
 }
Exemple #22
0
 private bool DiagonallyForwardToRightForWhite(Square destination)
 {
     return Color == PieceColor.White
            && File > destination.File && Rank < destination.Rank;
 }
Exemple #23
0
        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);
        }
Exemple #24
0
 public bool MoveIsIllegal(Square origin, Square destination)
 {
     return !MoveIsLegal(origin, destination);
 }
Exemple #25
0
        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);
        }
Exemple #26
0
 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;
 }
Exemple #27
0
 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);
 }
Exemple #28
0
 public bool AlongRank(Square destination)
 {
     return Rank > destination.Rank && File == destination.File
            || Rank < destination.Rank && File == destination.File;
 }
Exemple #29
0
 private static string CreateName(Square square)
 {
     return square.Occupier.GetType().Name +
            Random.Next(100000000, 1000000000).ToString(CultureInfo.InvariantCulture);
 }
Exemple #30
0
 public bool DistanceOfFilesIsZeroTo(Square destination)
 {
     return (Math.Abs(File - destination.File) == 0);
 }