public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } BoardDistance distance = (BoardDistance)obj; return(X == distance.X && Y == distance.Y); }
public override bool IsLegalMove(Move move, ChessGame game) { ChessUtility.ThrowIfNull(move, "move"); ChessUtility.ThrowIfNull(game, "game"); BoardPosition origin = move.OriginalPosition; BoardPosition destination = move.NewPosition; BoardDistance posDelta = new BoardDistance(origin, destination); if (posDelta.X != 0 && posDelta.Y != 0) { return(false); } bool increasingRank = destination.Rank > origin.Rank; bool increasingFile = (int)destination.File > (int)origin.File; if (posDelta.X == 0) { int f = (int)origin.File; for (int r = origin.Rank + (increasingRank ? 1 : -1); increasingRank?r <destination.Rank : r> destination.Rank; r += increasingRank ? 1 : -1) { if (game.GetPieceAt((ChessFile)f, r) != null) { return(false); } } } else // (posDelta.DeltaY == 0) { int r = origin.Rank; for (int f = (int)origin.File + (increasingFile ? 1 : -1); increasingFile?f <(int)destination.File : f> (int) destination.File; f += increasingFile ? 1 : -1) { if (game.GetPieceAt((ChessFile)f, r) != null) { return(false); } } } return(true); }
public virtual MoveType ApplyMove(Move move, bool alreadyValidated) { ChessUtility.ThrowIfNull(move, "move"); if (!alreadyValidated && !IsValidMove(move)) { return(MoveType.Invalid); } MoveType type = MoveType.Move; ChessPiece movingPiece = GetPieceAt(move.OriginalPosition.File, move.OriginalPosition.Rank); ChessPiece capturedPiece = GetPieceAt(move.NewPosition.File, move.NewPosition.Rank); ChessPiece newPiece = movingPiece; bool isCapture = capturedPiece != null; CastlingType castle = CastlingType.None; if (movingPiece is Pawn) { _halfMoveClock = 0; BoardDistance pd = new BoardDistance(move.OriginalPosition, move.NewPosition); if (pd.X == 1 && pd.Y == 1 && GetPieceAt(move.NewPosition) == null) { // en passant isCapture = true; SetPieceAt(move.NewPosition.File, move.OriginalPosition.Rank, null); } if (move.NewPosition.Rank == (move.Player == ChessPlayer.White ? 8 : 1)) { newPiece = MapPgnCharToPiece(move.Promotion.Value, move.Player); type |= MoveType.Promotion; } } else if (movingPiece is King) { if (movingPiece.Owner == ChessPlayer.White) { CanWhiteCastleKingSide = CanWhiteCastleQueenSide = false; } else { CanBlackCastleKingSide = CanBlackCastleQueenSide = false; } if (new BoardDistance(move.OriginalPosition, move.NewPosition).X == 2 && CastlingCanBeLegal) { castle = ApplyCastle(move); type |= MoveType.Castling; } } else if (movingPiece is Rook) { if (move.Player == ChessPlayer.White) { if (move.OriginalPosition.File == ChessFile.A && move.OriginalPosition.Rank == 1) { CanWhiteCastleQueenSide = false; } else if (move.OriginalPosition.File == ChessFile.H && move.OriginalPosition.Rank == 1) { CanWhiteCastleKingSide = false; } } else { if (move.OriginalPosition.File == ChessFile.A && move.OriginalPosition.Rank == 8) { CanBlackCastleQueenSide = false; } else if (move.OriginalPosition.File == ChessFile.H && move.OriginalPosition.Rank == 8) { CanBlackCastleKingSide = false; } } } if (isCapture) { type |= MoveType.Capture; _halfMoveClock = 0; if (move.NewPosition.File == ChessFile.A && move.NewPosition.Rank == 1) { CanWhiteCastleQueenSide = false; } else if (move.NewPosition.File == ChessFile.H && move.NewPosition.Rank == 1) { CanWhiteCastleKingSide = false; } else if (move.NewPosition.File == ChessFile.A && move.NewPosition.Rank == 8) { CanBlackCastleQueenSide = false; } else if (move.NewPosition.File == ChessFile.H && move.NewPosition.Rank == 8) { CanBlackCastleKingSide = false; } } if (!isCapture && !(movingPiece is Pawn)) { _halfMoveClock++; if (_halfMoveClock >= 100) { fiftyMoves = true; } else { fiftyMoves = false; } } if (move.Player == ChessPlayer.Black) { _fullMoveNumber++; } SetPieceAt(move.NewPosition.File, move.NewPosition.Rank, newPiece); SetPieceAt(move.OriginalPosition.File, move.OriginalPosition.Rank, null); WhoseTurn = ChessUtility.GetOpponentOf(move.Player); _moves.Add(new DetailedMove(move, movingPiece, isCapture, castle)); _positions.Add(GetPosition()); return(type); }