/// <summary> /// Updates what types of castling is possible after the move has been performed. /// </summary> /// <param name="afterState">The "BoardState" to update.</param> protected void SetCastlingAvailabilety(ref BoardState afterState) { switch (m_from) { case Square.E1: afterState.WhiteCanCastleLong = false; afterState.WhiteCanCastleShort = false; break; case Square.E8: afterState.BlackCanCastleLong = false; afterState.BlackCanCastleShort = false; break; case Square.A1: afterState.WhiteCanCastleLong = false; break; case Square.A8: afterState.BlackCanCastleLong = false; break; case Square.H1: afterState.WhiteCanCastleShort = false; break; case Square.H8: afterState.BlackCanCastleShort = false; break; } switch (m_to) { case Square.A1: afterState.WhiteCanCastleLong = false; break; case Square.A8: afterState.BlackCanCastleLong = false; break; case Square.H1: afterState.WhiteCanCastleShort = false; break; case Square.H8: afterState.BlackCanCastleShort = false; break; } }
/// <summary> /// Updates if the board has a pawn that can be hit En-Passant after the move has been performed. /// </summary> /// <param name="afterState">The "BoardState" to update.</param> protected void SetEnPassentTarget(ref BoardState afterState) { if ((Board.Rank(m_from) == 1 && Board.Rank(m_to) == 3 && m_piece == Piece.WhitePawn) || (Board.Rank(m_from) == 6 && Board.Rank(m_to) == 4 && m_piece == Piece.BlackPawn)) { afterState.EnPassantTarget = m_to; } else { afterState.EnPassantTarget = Square.None; } }
/// <summary> /// Updates the board state as a result of a move has been performed. /// </summary> /// <param name="afterState">The "BoardState" to update.</param> protected void EndTurn(ref BoardState afterState) { if (m_capture != Piece.None || m_piece == Piece.WhitePawn || m_piece == Piece.BlackPawn) afterState.NonHitAndPawnMovesPlayed = 0; else afterState.NonHitAndPawnMovesPlayed++; switch (m_beforeState.ColorToPlay) { case PieceColor.White: afterState.ColorToPlay = PieceColor.Black; break; case PieceColor.Black: afterState.ColorToPlay = PieceColor.White; break; } }
/// <summary> /// Performs the move. I move is only pseudo legal (putting own king in check) /// the move isn't executed. /// </summary> /// <returns>True if move could be carried out, false otherwise.</returns> public virtual bool Execute() { BoardState afterState = m_beforeState = m_board.State; m_board.MovePiece(m_from, m_to); SetCastlingAvailabilety(ref afterState); SetEnPassentTarget(ref afterState); EndTurn(ref afterState); m_board.State = afterState; m_board.AddToHistory(); if (m_board.IsCheck(m_beforeState.ColorToPlay)) { UnExecute(); return false; } return true; }
public Board(Board board) { m_squares = new Piece[board.m_squares.Length]; for (int i = 0; i < m_squares.Length; ++i) m_squares[i] = board.m_squares[i]; m_state = board.m_state; m_history = new HashHistory(board.m_history); m_pieceFactory = FlyweightPieceFactory.Instance(); m_whiteLocationList = new PieceLocationManager(board.m_whiteLocationList); m_blackLocationList = new PieceLocationManager(board.BlackPieceLocations); m_whiteKingLocation = board.m_whiteKingLocation; m_blackKingLocation = board.m_blackKingLocation; m_boardHash = new ZobristHash(board.m_boardHash); }
/// <summary> /// Performs the move. If move is only pseudo legal (putting own king in check) /// the move isn't executed. /// </summary> /// <returns>True if move could be carried out, false otherwise.</returns> private bool StandardExecute(Board board) { BoardState afterState = m_beforeState = board.State; board.MovePiece(m_from, m_to); SetCastlingAvailabilety(ref afterState); SetEnPassentTarget(ref afterState); EndTurn(ref afterState); board.State = afterState; board.AddToHistory(); if (board.IsCheck(m_beforeState.ColorToPlay)) { StandardUnExecute(board); return false; } return true; }
/// <summary> /// Updates if the board has a pawn that can be hit En-Passant after the move has been performed. /// </summary> /// <param name="afterState">The "BoardState" to update.</param> private void SetEnPassentTarget(ref BoardState afterState) { if ((Board.Rank(From) == 1 && Board.Rank(To) == 3 && Piece == Piece.WhitePawn) || (Board.Rank(From) == 6 && Board.Rank(To) == 4 && Piece == Piece.BlackPawn)) { afterState.EnPassantTarget = To; } else { afterState.EnPassantTarget = Square.None; } }