public void CopyFrom(ChessBoard source) { for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { board[row, col] = source.board[row, col]; } } flags = source.flags; pawnJumpCol = source.pawnJumpCol; }
private void CheckForCheck() { flags &= ~(BoardFlags.BlackInCheck | BoardFlags.WhiteInCheck); for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { Coordinate source = new Coordinate(row, col); PieceColor sourceColor = PieceColorAt(source); if ((this[row, col] == ChessPiece.King) && IsUnderAttack(new Coordinate(row, col), PieceColor.Black)) { flags |= BoardFlags.WhiteInCheck; } if ((this[row, col] == ChessPiece.BlackKing) && IsUnderAttack(new Coordinate(row, col), PieceColor.White)) { flags |= BoardFlags.BlackInCheck; } } } }
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(); }
private void CheckForCheck() { flags &= ~(BoardFlags.BlackInCheck | BoardFlags.WhiteInCheck); for (int row = 0; row < 8; row++) for (int col = 0; col < 8; col++) { Coordinate source = new Coordinate(row, col); PieceColor sourceColor = PieceColorAt(source); if ((this[row, col] == ChessPiece.King) && IsUnderAttack(new Coordinate(row, col), PieceColor.Black)) flags |= BoardFlags.WhiteInCheck; if ((this[row, col] == ChessPiece.BlackKing) && IsUnderAttack(new Coordinate(row, col), PieceColor.White)) flags |= BoardFlags.BlackInCheck; } }
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 void CopyFrom(ChessBoard source) { for (int row = 0; row < 8; row++) for (int col = 0; col < 8; col++) { board[row, col] = source.board[row, col]; } flags = source.flags; pawnJumpCol = source.pawnJumpCol; }