public override bool IsValidMove(Move move, ChessGame game) { ChessUtilities.ThrowIfNull(move, nameof(move)); ChessUtilities.ThrowIfNull(game, nameof(game)); var origin = move.OriginalPosition; var destination = move.NewPosition; var posDelta = new SquareDistance(origin, destination); if (posDelta.DistanceX != posDelta.DistanceY) { return(false); } var increasingRank = destination.Rank > origin.Rank; var increasingFile = (int)destination.File > (int)origin.File; var f = (int)origin.File + (increasingFile ? 1 : -1); var r = origin.Rank + (increasingRank ? 1 : -1); while (increasingFile ? f <(int)destination.File : f> (int) destination.File) { if (game.GetPieceAt((File)f, r) != null) { return(false); } f += increasingFile ? 1 : -1; r += increasingRank ? 1 : -1; } return(true); }
public void GridSubValueComputesDistance([Values(1, 2, 3, 4, 5, 6, 7, 8, 9)] int x, [Values(1, 2, 3, 4, 5, 6, 7, 8, 9)] int y) { var a = new GridPoint(2, 4, 1); var b = new GridPoint(x, y, 2); new GridSubValue(a, b).DistanceValue.Should().Be(SquareDistance.FromPoints(a, b)); }
public void SquareDistanceCalculatorShouldWorkForOffsets(int a, int b) { Func <int, int, int> Computed = ((x, y) => x * x + y * y); SquareDistance.FromOffsets(a, b).Should().Be(Computed(a, b)); SquareDistance.FromOffsets(b, a).Should().Be(Computed(a, b)); SquareDistance.FromOffsets(b, a).Should().Be(Computed(b, a)); SquareDistance.FromOffsets(a, b).Should().Be(Computed(b, a)); }
public static void TestPositionDistance() { var square1 = new Square(File.A, 2); var square2 = new Square(File.A, 3); var distance1 = new SquareDistance(square1, square2); Assert.AreEqual(0, distance1.DistanceX); Assert.AreEqual(1, distance1.DistanceY); var distance2 = new SquareDistance(square2, square1); Assert.AreEqual(0, distance2.DistanceX); Assert.AreEqual(1, distance2.DistanceY); }
public int Evaluate() { int total = 0; foreach (var gridPoint in simpleGridIterator) { foreach (var secondGridPoint in GridIteratorStrictlyAfter(gridPoint)) { total += GCD.Of(gridPoint.Number, secondGridPoint.Number) * SquareDistance.FromPoints(gridPoint, secondGridPoint); } } return(total); }
public override bool IsValidMove(Move move, ChessGame game) { ChessUtilities.ThrowIfNull(move, nameof(move)); ChessUtilities.ThrowIfNull(game, nameof(game)); var origin = move.OriginalPosition; var destination = move.NewPosition; var posDelta = new SquareDistance(origin, destination); if ((posDelta.DistanceX != 2 || posDelta.DistanceY != 1) && (posDelta.DistanceX != 1 || posDelta.DistanceY != 2)) { return(false); } return(true); }
public override bool IsValidMove(Move move, ChessGame game) { ChessUtilities.ThrowIfNull(move, nameof(move)); var origin = move.OriginalPosition; var destination = move.NewPosition; var distance = new SquareDistance(origin, destination); if ((distance.DistanceX != 1 || distance.DistanceY != 1) && (distance.DistanceX != 0 || distance.DistanceY != 1) && (distance.DistanceX != 1 || distance.DistanceY != 0) && (distance.DistanceX != 2 || distance.DistanceY != 0)) { return(false); } if (distance.DistanceX != 2) { return(true); } return(CanCastle(origin, destination, game)); }
public override bool IsValidMove(Move move, ChessGame game) { ChessUtilities.ThrowIfNull(move, nameof(move)); ChessUtilities.ThrowIfNull(game, nameof(game)); var origin = move.OriginalPosition; var destination = move.NewPosition; var promotion = move.Promotion; var posDelta = new SquareDistance(origin, destination); if ((posDelta.DistanceX != 0 || posDelta.DistanceY != 1) && (posDelta.DistanceX != 1 || posDelta.DistanceY != 1) && (posDelta.DistanceX != 0 || posDelta.DistanceY != 2)) { return(false); } if (Owner == Player.White) { if (origin.Rank > destination.Rank) { return(false); } if (destination.Rank == 8 && promotion == null) { return(false); } } if (Owner == Player.Black) { if (origin.Rank < destination.Rank) { return(false); } if (destination.Rank == 1 && promotion == null) { return(false); } } bool checkEnPassant = false; if (posDelta.DistanceY == 2) { if ((origin.Rank != 2 && Owner == Player.White) || (origin.Rank != 7 && Owner == Player.Black)) { return(false); } if (origin.Rank == 2 && game.GetPieceAt(origin.File, 3) != null) { return(false); } if (origin.Rank == 7 && game.GetPieceAt(origin.File, 6) != null) { return(false); } } Piece pieceAtDestination = game.GetPieceAt(destination); if (posDelta.DistanceX == 0 && (posDelta.DistanceY == 1 || posDelta.DistanceY == 2)) { if (pieceAtDestination != null) { return(false); } } else { if (pieceAtDestination == null) { checkEnPassant = true; } else if (pieceAtDestination.Owner == Owner) { return(false); } } if (checkEnPassant) { ReadOnlyCollection <DetailedMove> _moves = game.Moves; if (_moves.Count == 0) { return(false); } if ((origin.Rank != 5 && Owner == Player.White) || (origin.Rank != 4 && Owner == Player.Black)) { return(false); } var latestMove = _moves[_moves.Count - 1]; if (latestMove.Player != ChessUtilities.GetOpponentOf(Owner)) { return(false); } if (game.GetPieceAt(latestMove.NewPosition).Owner == Owner) { return(false); } if (!(game.GetPieceAt(latestMove.NewPosition) is Pawn)) { return(false); } if (Owner == Player.White) { if (latestMove.OriginalPosition.Rank != 7 || latestMove.NewPosition.Rank != 5) { return(false); } } else // (m.Player == Players.Black) { if (latestMove.OriginalPosition.Rank != 2 || latestMove.NewPosition.Rank != 4) { return(false); } } if (destination.File != latestMove.NewPosition.File) { return(false); } } return(true); }