private void ApplyMove(ChessMove move) { bool isBlack = PieceColorAt(move.Source) == PieceColor.Black; if (IsBlacksTurn) { if (!isBlack) throw new InvalidOperationException("Attempted to move a white piece on black's turn"); } else { if (isBlack) throw new InvalidOperationException("Attempted to move a black piece on white's turn"); } switch (this[move.Source] & ChessPiece.PieceMask) { case ChessPiece.Pawn: if ((move.Source.col != move.Target.col) && ((this[move.Target] & ChessPiece.PieceMask) == ChessPiece.Empty)) { // En Passant if (isBlack) board[move.Target.row - 1, move.Target.col] = ChessPiece.Empty; else board[move.Target.row + 1, move.Target.col] = ChessPiece.Empty; } if ((move.Target.row - move.Source.row > 1) || (move.Source.row - move.Target.row > 1)) pawnJumpCol = move.Source.col; else pawnJumpCol = 99; break; case ChessPiece.Rook: if (move.Source.col == 0) flags |= BoardFlags.WhiteQueenSideCastlingEnded; else flags |= BoardFlags.WhiteKingSideCastlingEnded; pawnJumpCol = 99; break; case ChessPiece.BlackRook: if (move.Source.col == 0) flags |= BoardFlags.BlackQueenSideCastlingEnded; else flags |= BoardFlags.BlackKingSideCastlingEnded; pawnJumpCol = 99; break; case ChessPiece.King: if (isBlack) flags |= BoardFlags.BlackKingSideCastlingEnded | BoardFlags.BlackQueenSideCastlingEnded; else flags |= BoardFlags.WhiteKingSideCastlingEnded | BoardFlags.WhiteQueenSideCastlingEnded; if (move.Target.col - move.Source.col > 1) { // Castling to king's rook board[move.Source.row, 5] = board[move.Source.row, 7]; board[move.Source.row, 7] = ChessPiece.Empty; } else if (move.Source.col - move.Target.col > 1) { // Castling to queen's rook board[move.Source.row, 3] = board[move.Source.row, 0]; board[move.Source.row, 0] = ChessPiece.Empty; } pawnJumpCol = 99; break; case ChessPiece.Empty: throw new InvalidOperationException("Attempted to move from a square without a piece."); default: pawnJumpCol = 99; break; } if ((isBlack && (move.Target.row == 7) && (board[move.Source.row, move.Source.col] == ChessPiece.BlackPawn)) || (!isBlack && (move.Target.row == 0) && (board[move.Source.row, move.Source.col] == ChessPiece.Pawn))) board[move.Target.row, move.Target.col] = move.Promotion | (isBlack ? ChessPiece.Black : 0); else board[move.Target.row, move.Target.col] = board[move.Source.row, move.Source.col]; board[move.Source.row, move.Source.col] = ChessPiece.Empty; flags ^= BoardFlags.BlacksTurn; CheckForCheck(); }
public ChessBoard Move(ChessMove move) { ChessBoard result; if (moveResults == null) GetValidMoves(); if (moveResults.TryGetValue(move, out result)) { if (result == null) { result = Clone(); result.ApplyMove(move); } moveResults[move] = result; return result; } throw new ArgumentException(string.Format("{0} is not a valid move.", move)); }
public EvaluatedMove(ChessMove move, ChessBoard resultingState, EvaluatedMove predecessor = null) { this.move = move; this.resultingState = resultingState; this.Previous = predecessor; }
public IEnumerable<ChessMove> GetValidMoves() { if (moveResults == null) { moveResults = new Dictionary<ChessMove, ChessBoard>(); for (int row = 0; row < 8; row++) for (int col = 0; col < 8; col++) { Coordinate source = new Coordinate(row, col); if ((IsBlacksTurn && (PieceColorAt(source) == PieceColor.Black)) || (!IsBlacksTurn && (PieceColorAt(source) == PieceColor.White))) { foreach (Coordinate target in GetMovesForPiece(source)) { if (((this[source] == ChessPiece.Pawn) && (target.row == 0)) || ((this[source] == ChessPiece.BlackPawn) && (target.row == 7))) { bool bFirstPromotion = true; foreach (ChessPiece promotion in new ChessPiece[] { ChessPiece.Rook, ChessPiece.Knight, ChessPiece.Bishop, ChessPiece.Queen }) { ChessBoard clone = Clone(); ChessMove move = new ChessMove(source, target, promotion); clone.ApplyMove(move); if (bFirstPromotion && ((IsBlacksTurn && clone.IsBlackInCheck) || (!IsBlacksTurn && clone.IsWhiteInCheck))) break; bFirstPromotion = false; moveResults[move] = clone; } } else { ChessBoard clone = Clone(); ChessMove move = new ChessMove(source, target); clone.ApplyMove(move); if (((IsBlacksTurn && !clone.IsBlackInCheck) || (!IsBlacksTurn && !clone.IsWhiteInCheck))) moveResults[move] = clone; } } } } } return moveResults.Keys; }