private bool TestCheckMate(Color color) { if (!TestCheck(color)) { return(false); } List <Piece> pieces = PiecesOnTheBoard.FindAll(x => ((ChessPiece)x).Color == color); foreach (Piece obj in pieces) { bool[,] mat = obj.PossibleMoves(); for (int i = 0; i < mat.GetLength(0); i++) { for (int j = 0; j < mat.GetLength(1); j++) { if (mat[i, j]) { Position source = ((ChessPiece)obj).GetChessPosition().ToPosition(); Position target = new Position(i, j); Piece capturedPiece = MakeMove(source, target); bool testCheck = TestCheck(color); UndoMove(source, target, capturedPiece); if (!testCheck) { return(false); } } } } } return(true); }
private Piece MakeMove(Position source, Position target) { ChessPiece sourcePiece = (ChessPiece)Board.RemovePiece(source); sourcePiece.IncreaseMoveCount(); Piece capturedPiece = Board.RemovePiece(target); Board.PlacePiece(sourcePiece, target); if (capturedPiece != null) { PiecesOnTheBoard.Remove(capturedPiece); CapturedPieces.Add(capturedPiece); } //SpecialMove Castling KingSide Rook if (sourcePiece is King && target.Column == source.Column + 2) { Position sourceRook = new Position(source.Row, source.Column + 3); Position targetRook = new Position(source.Row, source.Column + 1); ChessPiece rook = (ChessPiece)Board.RemovePiece(sourceRook); Board.PlacePiece(rook, targetRook); rook.IncreaseMoveCount(); } //SpecialMove Castling QueenSide Rook if (sourcePiece is King && target.Column == source.Column - 2) { Position sourceRook = new Position(source.Row, source.Column - 4); Position targetRook = new Position(source.Row, source.Column - 1); ChessPiece rook = (ChessPiece)Board.RemovePiece(sourceRook); Board.PlacePiece(rook, targetRook); rook.IncreaseMoveCount(); } //SpecialMove EnPassant if (sourcePiece is Pawn) { if (source.Column != target.Column && capturedPiece == null) { Position pawnPosition; if (sourcePiece.Color == Color.White) { pawnPosition = new Position(target.Row + 1, target.Column); } else { pawnPosition = new Position(target.Row - 1, target.Column); } capturedPiece = Board.RemovePiece(pawnPosition); CapturedPieces.Add(capturedPiece); PiecesOnTheBoard.Remove(capturedPiece); } } return(capturedPiece); }
private void UndoMove(Position source, Position target, Piece capturedPiece) { ChessPiece p = (ChessPiece)Board.RemovePiece(target); p.DecreaseMoveCount(); Board.PlacePiece(p, source); if (capturedPiece != null) { Board.PlacePiece(capturedPiece, target); CapturedPieces.Remove(capturedPiece); PiecesOnTheBoard.Add(capturedPiece); } //SpecialMove Castling KingSide Rook if (p is King && target.Column == source.Column + 2) { Position sourceRook = new Position(source.Row, source.Column + 3); Position targetRook = new Position(source.Row, source.Column + 1); ChessPiece rook = (ChessPiece)Board.RemovePiece(targetRook); Board.PlacePiece(rook, sourceRook); rook.DecreaseMoveCount(); } //SpecialMove Castling QueenSide Rook if (p is King && target.Column == source.Column - 2) { Position sourceRook = new Position(source.Row, source.Column - 4); Position targetRook = new Position(source.Row, source.Column - 1); ChessPiece rook = (ChessPiece)Board.RemovePiece(targetRook); Board.PlacePiece(rook, sourceRook); rook.DecreaseMoveCount(); } //SpecialMove EnPassant if (p is Pawn) { if (source.Column != target.Column && capturedPiece == EnPassantVulnerable) { ChessPiece pawn = (ChessPiece)Board.RemovePiece(target); Position pawnPosition; if (p.Color == Color.White) { pawnPosition = new Position(3, target.Column); } else { pawnPosition = new Position(3, target.Column); } Board.PlacePiece(pawn, pawnPosition); } } }
private ChessPiece GetKing(Color color) { List <Piece> auxList = PiecesOnTheBoard.FindAll(x => ((ChessPiece)x).Color == color); foreach (Piece obj in auxList) { if (obj is King) { return((ChessPiece)obj); } } throw new ApplicationException("There is no " + color + " king on the board.\n"); }
private bool TestCheck(Color color) { Position KingPosition = GetKing(color).GetChessPosition().ToPosition(); // GetKing(color).Position; -> Não seria mais pratico? List <Piece> opponentPieces = PiecesOnTheBoard.FindAll(x => ((ChessPiece)x).Color == OpponentColor(color)); foreach (Piece obj in opponentPieces) { bool[,] mat = obj.PossibleMoves(); if (mat[KingPosition.Row, KingPosition.Column]) { return(true); } } return(false); }
public ChessPiece ReplacePromotedPiece(String type) { if (Promoted == null) { throw new ApplicationException("There is no piece to be promoted.\n"); } if (!type.Equals("B") && !type.Equals("N") && !type.Equals("R") && !type.Equals("Q")) { return(Promoted); } Position promotedPosition = Promoted.GetChessPosition().ToPosition(); Piece promoted = Board.RemovePiece(promotedPosition); PiecesOnTheBoard.Remove(promoted); ChessPiece newPiece = NewPiece(type, Promoted.Color); Board.PlacePiece(newPiece, promotedPosition); PiecesOnTheBoard.Add(newPiece); return(newPiece); }
private void PlaceNewPiece(char column, int row, ChessPiece piece) { Board.PlacePiece(piece, new ChessPosition(column, row).ToPosition()); PiecesOnTheBoard.Add(piece); }