private static void GetKingCastlingPosition(ChessBoard board, char file, sbyte rank, List <ChessBoard> result, Square current) { ChessBoard tempboard; if (BlackKing.IsSafe(board) && DefaultInfo.BlackKingIsUnMoved && DefaultInfo.BlackHsideRookIsUnMoved) { char rookfile = 'h'; for (char tfile = file; tfile < 'h'; tfile++) { if (board[tfile, rank] == (sbyte)DefaultPieces.BlackRook) { rookfile = tfile; } } tempboard = board.ShallowCopy(); bool CastlingAvailable = true; for (char tfile = file; tfile <= 'g'; 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 >= 'f'; 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 <= 'f'; 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 <= 'g'; tfile++) { ChessBoard temp2board = board.ShallowCopy(); temp2board[file, rank] = 0; temp2board[tfile, rank] = (sbyte)DefaultPieces.BlackKing; if (!BlackKing.IsSafe(temp2board)) { CastlingAvailable = false; return; } } if (CastlingAvailable) { tempboard = Piece.PerformMove(board, current, new Square('g', rank)); tempboard = Piece.PerformMove(tempboard, new Square(rookfile, rank), new Square('f', rank)); result.Add(tempboard); } } }
private static void GetEnPassantPositions(ref List <ChessBoard> result, ChessBoard board, Square currentposition) { ChessBoard tempboard = board.ShallowCopy(); bool CheckEnPassant = DefaultInfo.EnPassantPossibleCapture.rank == currentposition.rank - 1 && (DefaultInfo.EnPassantPossibleCapture.file == (char)(currentposition.file + 1) || DefaultInfo.EnPassantPossibleCapture.file == (char)(currentposition.file - 1)); if (CheckEnPassant) { tempboard = Piece.PerformMove(board, currentposition, DefaultInfo.EnPassantPossibleCapture); tempboard[DefaultInfo.EnPassantPossibleCapture.file, DefaultInfo.EnPassantPossibleCapture.rank + 1] = 0; result = Piece.AddNewPosition(result, tempboard, false); } }
// Code Review: Надто об'ємний метод. // Code Review: Аргументи методу повинні починатися з малої літери. private static ChessBoard PerformCastling(ChessBoard board, bool IsWhite, bool IsKingCastling, out char kingfile) { ChessBoard tempboard = board.ShallowCopy(); sbyte castlingrank; sbyte kingsbyte; sbyte rooksbyte; char rookfile; if (IsWhite) { castlingrank = 1; kingsbyte = (sbyte)DefaultPieces.WhiteKing; rooksbyte = (sbyte)DefaultPieces.WhiteRook; } else { castlingrank = 8; kingsbyte = (sbyte)DefaultPieces.BlackKing; rooksbyte = (sbyte)DefaultPieces.BlackRook; } kingfile = Piece.GetPosition(board, kingsbyte).file; if (IsKingCastling) { rookfile = 'h'; for (char tfile = 'h'; tfile >= 'c'; tfile--) { if (board[tfile, castlingrank] == rooksbyte) { rookfile = tfile; break; } } tempboard = Piece.PerformMove(tempboard, new Square(kingfile, castlingrank), new Square('g', castlingrank)); tempboard = Piece.PerformMove(tempboard, new Square(rookfile, castlingrank), new Square('f', castlingrank)); } else { rookfile = 'a'; for (char tfile = 'a'; tfile <= 'f'; tfile++) { if (board[tfile, castlingrank] == rooksbyte) { rookfile = tfile; break; } } tempboard = Piece.PerformMove(tempboard, new Square(kingfile, castlingrank), new Square('c', castlingrank)); tempboard = Piece.PerformMove(tempboard, new Square(rookfile, castlingrank), new Square('d', castlingrank)); } return(tempboard); }
public static List <ChessBoard> GetPossiblePositions(ChessBoard board, char file, sbyte rank) { List <ChessBoard> result = new List <ChessBoard>(); ChessBoard tempboard; List <Square> moves = new List <Square>(); Square current = new Square(file, rank); WhitePiece.GetDiagonalDestinations(board, ref moves, current, 1, 1); WhitePiece.GetDiagonalDestinations(board, ref moves, current, 1, -1); WhitePiece.GetDiagonalDestinations(board, ref moves, current, -1, 1); WhitePiece.GetDiagonalDestinations(board, ref moves, current, -1, -1); foreach (Square move in moves) { tempboard = Piece.PerformMove(board, current, move); result = Piece.AddNewPosition(result, tempboard, true); } return(result); }
public static List <ChessBoard> GetPossiblePositions(ChessBoard board, char file, sbyte rank) { List <ChessBoard> result = new List <ChessBoard>(); ChessBoard tempboard; Square current = new Square(file, rank); Square[] moves = Piece.GetSimpleKingMoveDestinations(current); foreach (Square move in moves) { if (move.IsOK() && board[move] >= 0) { tempboard = Piece.PerformMove(board, current, move); result = Piece.AddNewPosition(result, tempboard, false); } } GetQueenCastlingPosition(board, file, rank, result, current); GetKingCastlingPosition(board, file, rank, result, current); return(result); }
public static List <ChessBoard> GetPossiblePositions(ChessBoard board, char file, sbyte rank) { List <ChessBoard> result = new List <ChessBoard>(); ChessBoard tempboard; List <Square> moves = new List <Square>(); Square current = new Square(file, rank); WhitePiece.GetVerticalUpDestinations(board, moves, current); WhitePiece.GetVerticalDownDestinations(board, moves, current); WhitePiece.GetHorizontalLeftDestinations(board, moves, current); WhitePiece.GetHorizontalRightDestinations(board, moves, current); Console.WriteLine(moves.Count); foreach (Square move in moves) { tempboard = Piece.PerformMove(board, current, move); result = Piece.AddNewPosition(result, tempboard, true); } Console.WriteLine(result.Count); return(result); }
public static List <ChessBoard> GetPossiblePositions(ChessBoard board, char file, sbyte rank) { List <ChessBoard> result = new List <ChessBoard>(); Square current = new Square(file, rank); Square[] moves = Piece.GetSimplekNightMoveDestinations(current); ChessBoard tempboard; foreach (Square move in moves) { if (move.IsOK()) { if (board[move] <= 0) { tempboard = Piece.PerformMove(board, current, move); result = Piece.AddNewPosition(result, tempboard, true); } } } return(result); }
public static List <ChessBoard> GetPossiblePositions(ChessBoard board, char file, sbyte rank) { List <ChessBoard> result = new List <ChessBoard>(); ChessBoard tempboard = board.ShallowCopy(); Square currentposition = new Square(file, rank); if (DefaultInfo.WhiteEnPassantEndangered) { GetEnPassantPositions(ref result, board, currentposition); } if (rank == 7 && board[file, rank - 1] == 0 && board[file, rank - 2] == 0) { tempboard = Piece.PerformMove(board, currentposition, new Square(file, rank - 2)); result = Piece.AddNewPosition(result, tempboard, false); } if (board[file, rank - 1] == 0) { tempboard = Piece.PerformMove(board, currentposition, new Square(file, rank - 1)); result = Piece.AddNewPosition(result, tempboard, false); } if (file >= 'a' && file <= 'g' && board[(char)(file + 1), rank - 1] > 0) { tempboard = Piece.PerformMove(board, currentposition, new Square((char)(file + 1), rank - 1)); result = Piece.AddNewPosition(result, tempboard, false); } if (file >= 'b' && file <= 'h' && board[(char)(file - 1), rank - 1] > 0) { tempboard = Piece.PerformMove(board, currentposition, new Square((char)(file - 1), rank - 1)); result = Piece.AddNewPosition(result, tempboard, false); } if (rank > 2 || result.Count == 0) { return(result); } else { return(GetPawnPromotionPositions(file, result, tempboard)); } }
private static void GetQueenCastlingPosition(ChessBoard board, char file, sbyte rank, List <ChessBoard> result, Square current) { ChessBoard tempboard; if (WhiteKing.IsSafe(board) && DefaultInfo.WhiteKingIsUnMoved && DefaultInfo.WhiteAsideRookIsUnMoved) { char rookfile = 'a'; for (char tfile = file; tfile > 'a'; tfile--) { if (board[tfile, rank] == (sbyte)DefaultPieces.WhiteRook) { 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.WhiteKing && board[tfile, rank] != (sbyte)DefaultPieces.WhiteRook) { CastlingAvailable = false; break; } } for (char tfile = rookfile; tfile <= 'd'; tfile++) { if (board[tfile, rank] != 0 && board[tfile, rank] != (sbyte)DefaultPieces.WhiteKing && board[tfile, rank] != (sbyte)DefaultPieces.WhiteRook) { CastlingAvailable = false; break; } } for (char tfile = rookfile; tfile >= 'd'; tfile--) { if (board[tfile, rank] != 0 && board[tfile, rank] != (sbyte)DefaultPieces.WhiteKing && board[tfile, rank] != (sbyte)DefaultPieces.WhiteRook) { CastlingAvailable = false; break; } } for (char tfile = file; tfile >= 'c'; tfile--) { ChessBoard temp2board = Piece.PerformMove(board, current, new Square(tfile, rank)); if (!WhiteKing.IsSafe(temp2board)) { CastlingAvailable = false; break; } } if (file == 'b') { ChessBoard temp2board = Piece.PerformMove(board, current, new Square('c', rank)); if (!WhiteKing.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); } } }