private void AddPieces(PlayerPieceSet pieces, Players player) { Debug.Assert(pieces != null); AddPiece(player, Exports.Pieces.King, pieces.KingsCell); for (uint i = 0, e = pieces.QueensCount; i < e; ++i) { AddPiece(player, Exports.Pieces.Queen, pieces.GetQueenCell(i)); } for (uint i = 0, e = pieces.RooksCount; i < e; ++i) { AddPiece(player, Exports.Pieces.Rook, pieces.GetRookCell(i)); } for (uint i = 0, e = pieces.BishopsCount; i < e; ++i) { AddPiece(player, Exports.Pieces.Bishop, pieces.GetBishopCell(i)); } for (uint i = 0, e = pieces.KnightsCount; i < e; ++i) { AddPiece(player, Exports.Pieces.Knight, pieces.GetKnightCell(i)); } for (uint i = 0, e = pieces.PawnsCount; i < e; ++i) { AddPiece(player, Exports.Pieces.Pawn, pieces.GetPawnCell(i)); } }
private static void MakePawnMoves(PlayerPieceSet my_pieces, PlayerPieceSet other_pieces, bool is_white_to_move, int? en_passant_capture_column, List<Move> moves) { int move_row_diff = is_white_to_move ? 1 : -1; for (uint i = 0, e = my_pieces.PawnsCount; i < e; ++i) { uint cell_index; var pawn_cell = new ChessboardCell(my_pieces.GetPawnCell(i, out cell_index)); var new_cell_value = ChessboardCell.CalculateValue( pawn_cell.Row + move_row_diff, pawn_cell.Column); // Moves without captures if (!my_pieces.IsOccupiedCell(new_cell_value) && !other_pieces.IsOccupiedCell(new_cell_value)) { WritePawnMove(pawn_cell.Value, new_cell_value, cell_index, 0, moves); // Two-rows move from the initial rank int initial_row = is_white_to_move ? Chessboard.ROW_MIN + 1 : Chessboard.ROW_MAX - 1; if (pawn_cell.Row == initial_row) { new_cell_value = ChessboardCell.CalculateValue( pawn_cell.Row + 2 * move_row_diff, pawn_cell.Column); if (!my_pieces.IsOccupiedCell(new_cell_value) && !other_pieces.IsOccupiedCell(new_cell_value)) { WritePawnMove(pawn_cell.Value, new_cell_value, cell_index, 0, moves); } } } // Captures except en passant captures // delta_column can be -1 or 1 for (int delta_column = -1; delta_column <= 1; delta_column += 2) { int new_column = pawn_cell.Column + delta_column; if (new_column >= Chessboard.COLUMN_MIN && new_column <= Chessboard.COLUMN_MAX) { new_cell_value = ChessboardCell.CalculateValue(pawn_cell.Row + move_row_diff, new_column); if (other_pieces.IsOccupiedCell(new_cell_value)) { WritePawnMove(pawn_cell.Value, new_cell_value, cell_index, Move.Flags.Capture, moves); } } } // En passant capture if (en_passant_capture_column.HasValue && ((is_white_to_move && pawn_cell.Row == 4) || (!is_white_to_move && pawn_cell.Row == 3)) && Math.Abs(pawn_cell.Column - en_passant_capture_column.Value) == 1) { new_cell_value = ChessboardCell.CalculateValue(pawn_cell.Row + move_row_diff, en_passant_capture_column.Value); // Since en passant capture is allowed, we know that the destination // cell is not occupied WritePawnMove(pawn_cell.Value, new_cell_value, cell_index, Move.Flags.Capture | Move.Flags.EnPassantCapture, moves); } // TODO - add en passant captures // TODO - uncomment //throw new NotImplementedException(); } }