protected virtual void UseGameCreationData(GameCreationData data) { Board = CloneBoard(data.Board); WhoseTurn = data.WhoseTurn; ChessPiece e1 = GetPieceAt(ChessFile.E, 1); ChessPiece e8 = GetPieceAt(ChessFile.E, 8); ChessPiece a1 = GetPieceAt(ChessFile.A, 1); ChessPiece h1 = GetPieceAt(ChessFile.H, 1); ChessPiece a8 = GetPieceAt(ChessFile.A, 8); ChessPiece h8 = GetPieceAt(ChessFile.H, 8); CanBlackCastleKingSide = CanBlackCastleQueenSide = CanWhiteCastleKingSide = CanWhiteCastleQueenSide = CastlingCanBeLegal; if (CastlingCanBeLegal) { if (!(e1 is King) || e1.Owner != ChessPlayer.White) { CanWhiteCastleKingSide = CanWhiteCastleQueenSide = false; } if (!(e8 is King) || e8.Owner != ChessPlayer.Black) { CanBlackCastleKingSide = CanBlackCastleQueenSide = false; } if (!(a1 is Rook) || a1.Owner != ChessPlayer.White || !data.CanWhiteCastleQueenSide) { CanWhiteCastleQueenSide = false; } if (!(h1 is Rook) || h1.Owner != ChessPlayer.White || !data.CanWhiteCastleKingSide) { CanWhiteCastleKingSide = false; } if (!(a8 is Rook) || a8.Owner != ChessPlayer.Black || !data.CanBlackCastleQueenSide) { CanBlackCastleQueenSide = false; } if (!(h8 is Rook) || h8.Owner != ChessPlayer.Black || !data.CanBlackCastleKingSide) { CanBlackCastleKingSide = false; } } if (data.EnPassant != null) { DetailedMove latestMove = new DetailedMove(new Move(new BoardPosition(data.EnPassant.File, data.WhoseTurn == ChessPlayer.White ? 7 : 2), new BoardPosition(data.EnPassant.File, data.WhoseTurn == ChessPlayer.White ? 5 : 4), ChessUtility.GetOpponentOf(data.WhoseTurn)), new Pawn(ChessUtility.GetOpponentOf(data.WhoseTurn)), false, CastlingType.None); _moves.Add(latestMove); } _halfMoveClock = data.HalfMoveClock; _fullMoveNumber = data.FullMoveNumber; }
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); }
public virtual bool IsWinner(ChessPlayer player) { return(IsCheckmated(ChessUtility.GetOpponentOf(player))); }