private static ChessBoard PerformBlackPawnMove(ChessBoard board, string notation, ChessBoard tempboard) { //simple move (not 5 rank) and promotion if (notation[1] != '5') { sbyte pawnrank = (sbyte)(Char.GetNumericValue(notation[1]) + 1); char pawnfile = notation[0]; if (board[pawnfile, pawnrank] != (sbyte)DefaultPieces.BlackPawn) { throw new ArgumentException("wrong move"); } tempboard[pawnfile, pawnrank] = 0; if (notation.Length == 2) { tempboard[pawnfile, pawnrank - 1] = (sbyte)DefaultPieces.BlackPawn; } else { tempboard[pawnfile, pawnrank - 1] = GetSbyteFromBlackPieceLetter(notation[2]); } if (BlackPawn.GetPossiblePositions(board, pawnfile, pawnrank).Contains(tempboard)) { return(tempboard); } else { throw new ArgumentException("Wrong move"); } } //simple move (5 rank) else { char pawnfile = notation[0]; sbyte pawnrank; if (board[pawnfile, (sbyte)(Char.GetNumericValue(notation[1]) + 1)] == (sbyte)DefaultPieces.BlackPawn) { pawnrank = (sbyte)(Char.GetNumericValue(notation[1]) + 1); } else if (board[pawnfile, (sbyte)(Char.GetNumericValue(notation[1]) + 2)] == (sbyte)DefaultPieces.BlackPawn && board[pawnfile, (sbyte)(Char.GetNumericValue(notation[1]) + 1)] == 0) { pawnrank = (sbyte)(Char.GetNumericValue(notation[1]) + 2); } else { throw new ArgumentException("Wrong move"); } tempboard[pawnfile, pawnrank] = 0; tempboard[pawnfile, (sbyte)Char.GetNumericValue(notation[1])] = (sbyte)DefaultPieces.BlackPawn; if (BlackPawn.GetPossiblePositions(board, pawnfile, pawnrank).Contains(tempboard)) { //en passant if (pawnrank == (sbyte)(Char.GetNumericValue(notation[1]) + 2)) { DefaultInfo.EnPassantPossibleCapture = new Square(notation[0], (sbyte)(Char.GetNumericValue(notation[1]) + 1)); DefaultInfo.BlackEnPassantEndangered = true; } return(tempboard); } else { throw new ArgumentException("Wrong move"); } } }
private static ChessBoard PerformBlackNonAmbiguousMove(ChessBoard board, string notation, ChessBoard tempboard) { char goalfile = notation[1]; sbyte goalrank = (sbyte)Char.GetNumericValue(notation[2]); //Check if piece is not-ambigious and get piece square List <Square> PossibleMovers = WhitePiece.GetPossibleBlackAttackersToSquare(board, new Square(goalfile, goalrank)); int NumberOfSamePieces = 0; Square PieceSquare = new Square(); foreach (Square tempsquare in PossibleMovers) { if (board[tempsquare.file, tempsquare.rank] == GetSbyteFromBlackPieceLetter(notation[0])) { NumberOfSamePieces++; PieceSquare = tempsquare; } } if (NumberOfSamePieces != 1) { throw new ArgumentException("wrong notation"); } tempboard[PieceSquare.file, PieceSquare.rank] = 0; tempboard[goalfile, goalrank] = GetSbyteFromBlackPieceLetter(notation[0]); GetPiecePositionsType piecefunction = GetBlackPiecePositionsType(notation[0]); if (piecefunction(board, PieceSquare.file, PieceSquare.rank).Contains(tempboard)) { if (board[PieceSquare.file, PieceSquare.rank] == (sbyte)DefaultPieces.BlackRook && (DefaultInfo.BlackAsideRookIsUnMoved || DefaultInfo.BlackHsideRookIsUnMoved)) { if (DefaultInfo.BlackAsideRookIsUnMoved && !DefaultInfo.BlackHsideRookIsUnMoved) { DefaultInfo.BlackAsideRookIsUnMoved = false; } else if (!DefaultInfo.BlackAsideRookIsUnMoved && DefaultInfo.BlackHsideRookIsUnMoved) { DefaultInfo.BlackHsideRookIsUnMoved = false; } else { char rookfile; for (rookfile = 'a'; rookfile <= 'h'; rookfile++) { if (board[rookfile, 8] == (sbyte)DefaultPieces.BlackRook) { break; } } if (rookfile < PieceSquare.file) { DefaultInfo.BlackHsideRookIsUnMoved = false; } else { DefaultInfo.BlackAsideRookIsUnMoved = false; } } } return(tempboard); } else { throw new ArgumentException("wrong move"); } }
private static ChessBoard PerformBlackPawnCapture(ChessBoard board, string notation, ChessBoard tempboard) { char pawnfile = notation[0]; char enemyfile = notation[1]; sbyte enemyrank = (sbyte)Char.GetNumericValue(notation[2]); sbyte pawnrank = (sbyte)(enemyrank + 1); if (board[pawnfile, pawnrank] != -1) { throw new ArgumentException("wrong notation"); } tempboard[pawnfile, pawnrank] = 0; if (DefaultInfo.WhiteEnPassantEndangered && enemyfile == DefaultInfo.EnPassantPossibleCapture.file && enemyrank == DefaultInfo.EnPassantPossibleCapture.rank) { tempboard[enemyfile, enemyrank + 1] = 0; } if (notation.Length == 3) { tempboard[enemyfile, enemyrank] = (sbyte)DefaultPieces.BlackPawn; } else { tempboard[enemyfile, enemyrank] = GetSbyteFromBlackPieceLetter(notation[3]); } if (BlackPawn.GetPossiblePositions(board, pawnfile, pawnrank).Contains(tempboard)) { return(tempboard); } else { throw new ArgumentException("Wrong move"); } }
// Code Review: Надто об'ємний метод. private static ChessBoard PerformBlackAmbiguousMove(ChessBoard board, string notation, ChessBoard tempboard) { char piecefile = default(char); sbyte piecerank = default(sbyte); char goalfile; sbyte goalrank; sbyte piecetype = GetSbyteFromBlackPieceLetter(notation[0]); //Ngf3 if (notation.Length == 4 && notation[1] >= 'a' && notation[1] <= 'h' && notation[2] >= 'a' && notation[2] <= 'h' && notation[3] >= '1' && notation[3] <= '8') { goalfile = notation[2]; goalrank = (sbyte)Char.GetNumericValue(notation[3]); piecefile = notation[1]; for (sbyte i = 1; i <= 8; i++) { if (board[piecefile, i] == piecetype) { piecerank = i; break; } } } //N5f3 else if (notation.Length == 4 && notation[1] >= '1' && notation[1] <= '8' && notation[2] >= 'a' && notation[2] <= 'h' && notation[3] >= '1' && notation[3] <= '8') { goalfile = notation[2]; goalrank = (sbyte)Char.GetNumericValue(notation[3]); piecerank = (sbyte)Char.GetNumericValue(notation[1]); for (char tfile = 'a'; tfile <= 'h'; tfile++) { if (board[tfile, piecerank] == piecetype) { piecefile = tfile; break; } } } //Ng5f3 else if (notation.Length == 5 && notation[1] >= 'a' && notation[1] <= 'h' && notation[2] >= '1' && notation[2] <= '8' && notation[3] >= 'a' && notation[3] <= 'h' && notation[4] >= '1' && notation[4] <= '8') { goalfile = notation[3]; goalrank = (sbyte)Char.GetNumericValue(notation[4]); piecefile = notation[1]; piecerank = (sbyte)Char.GetNumericValue(notation[2]); } else { throw new ArgumentException("wrong notation"); } tempboard[goalfile, goalrank] = piecetype; tempboard[piecefile, piecerank] = 0; GetPiecePositionsType piecefunction = GetBlackPiecePositionsType(notation[0]); if (piecefunction(board, piecefile, piecerank).Contains(tempboard)) { if (board[piecefile, piecerank] == (sbyte)DefaultPieces.BlackRook && (DefaultInfo.BlackAsideRookIsUnMoved || DefaultInfo.BlackHsideRookIsUnMoved)) { if (DefaultInfo.BlackAsideRookIsUnMoved && !DefaultInfo.BlackHsideRookIsUnMoved) { DefaultInfo.BlackAsideRookIsUnMoved = false; } else if (!DefaultInfo.BlackAsideRookIsUnMoved && DefaultInfo.BlackHsideRookIsUnMoved) { DefaultInfo.BlackHsideRookIsUnMoved = false; } else { char rookfile; for (rookfile = 'a'; rookfile <= 'h'; rookfile++) { if (board[rookfile, 8] == (sbyte)DefaultPieces.BlackRook) { break; } } if (rookfile < piecefile) { DefaultInfo.BlackHsideRookIsUnMoved = false; } else { DefaultInfo.BlackAsideRookIsUnMoved = false; } } } return(tempboard); } else { throw new ArgumentException("wrong move"); } }
private static void GetQueenCastlingPosition(ChessBoard board, char file, sbyte rank, List <ChessBoard> result, Square current) { ChessBoard tempboard; if (BlackKing.IsSafe(board) && DefaultInfo.BlackKingIsUnMoved && DefaultInfo.BlackAsideRookIsUnMoved) { char rookfile = 'a'; for (char tfile = file; tfile > 'a'; tfile--) { if (board[tfile, rank] == (sbyte)DefaultPieces.BlackRook) { rookfile = tfile; } } tempboard = board.ShallowCopy(); bool CastlingAvailable = true; for (char tfile = file; tfile >= 'c'; tfile--) { if (board[tfile, rank] != 0 && board[tfile, rank] != (sbyte)DefaultPieces.BlackKing && board[tfile, rank] != (sbyte)DefaultPieces.BlackRook) { CastlingAvailable = false; return; } } for (char tfile = rookfile; tfile <= 'd'; tfile++) { if (board[tfile, rank] != 0 && board[tfile, rank] != (sbyte)DefaultPieces.BlackKing && board[tfile, rank] != (sbyte)DefaultPieces.BlackRook) { CastlingAvailable = false; return; } } for (char tfile = rookfile; tfile >= 'd'; tfile--) { if (board[tfile, rank] != 0 && board[tfile, rank] != (sbyte)DefaultPieces.BlackKing && board[tfile, rank] != (sbyte)DefaultPieces.BlackRook) { CastlingAvailable = false; return; } } for (char tfile = file; tfile >= 'c'; tfile--) { ChessBoard temp2board = Piece.PerformMove(board, current, new Square(tfile, rank)); if (!BlackKing.IsSafe(temp2board)) { CastlingAvailable = false; return; } } if (file == 'b') { ChessBoard temp2board = Piece.PerformMove(board, current, new Square('c', rank)); if (!BlackKing.IsSafe(temp2board)) { CastlingAvailable = false; } } if (CastlingAvailable) { tempboard = Piece.PerformMove(board, current, new Square('c', rank)); tempboard = Piece.PerformMove(tempboard, new Square(rookfile, rank), new Square('d', rank)); result.Add(tempboard); } } }
public static List <ChessBoard> GetPossiblePositions(ChessBoard board, char file, sbyte rank) { sbyte piece = (sbyte)DefaultPieces.WhiteQueen; return(BlackPiece.GetReversedPossibleWhitePositions(board, file, rank, piece)); }
private static List <ChessBoard> GetPawnPromotionPositions(char file, List <ChessBoard> result, ChessBoard tempboard) { List <ChessBoard> resultpromotion = new List <ChessBoard>(); foreach (ChessBoard promotiontempboard in result) { tempboard = promotiontempboard.ShallowCopy(); char promotionfile = file; for (char tempfile = 'a'; tempfile <= 'h'; tempfile++) { if (tempboard[tempfile, 1] == (sbyte)DefaultPieces.BlackPawn) { promotionfile = tempfile; break; } } //now Promotions! tempboard[promotionfile, 1] = (sbyte)DefaultPieces.BlackkNight; resultpromotion.Add(tempboard.ShallowCopy()); tempboard[promotionfile, 1] = (sbyte)DefaultPieces.BlackBishop; resultpromotion.Add(tempboard.ShallowCopy()); tempboard[promotionfile, 1] = (sbyte)DefaultPieces.BlackRook; resultpromotion.Add(tempboard.ShallowCopy()); tempboard[promotionfile, 1] = (sbyte)DefaultPieces.BlackQueen; resultpromotion.Add(tempboard.ShallowCopy()); } return(resultpromotion); }
// Code Review: Назваргументів методу повинні починатися з малої літери. public static List <ChessBoard> AddNewPosition(List <ChessBoard> ResultedPositionsList, ChessBoard position, bool IsWhite) { if (IsWhite && WhiteKing.IsSafe(position)) { ResultedPositionsList.Add(position); } else if (!IsWhite && BlackKing.IsSafe(position)) { ResultedPositionsList.Add(position); } return(ResultedPositionsList); }