Question Index

In chess, assuming that the columns are numbered A to H and the rows are numbered 1 to 8. Write a program to find all the pawns which the camel can kill. Assume that there are only the given pawns on the chess board.
NOTE: The list returned should be first sorted by columns and then by rows, so A4 comes before B1 and C7 comes before D5 etc.,

Input (Position, List Of Positions) Output (Sorted List of Positions)
Camel = C1 pawns = [A3, B2, B1, H6, D3, E3, G3, E2] [A3, B2, E3, H6]
Camel = G1 pawns = [H1, C5, A8, D4, F1, E1, H2] [C5, D4, F1, H2]
Camel = D5 pawns = [A5, B7, C8, C6, A2, B3, H1, G4, F7, D6] [A2, B3, B7, C6, F7, H1]
Camel = B3 pawns = [H7, D5, A2, C2, D3, F7, D7, G8, A6] [A2, C2, D5, F7, G8]

class FindKillablePawnsByCamel

{    public static void main(String s[])
        ChessPosition camel = new ChessPosition('C', 1);
        List pawnPositions = new ArrayList();
        pawnPositions.add(new ChessPosition('A', 3));
        pawnPositions.add(new ChessPosition('B', 2));
        pawnPositions.add(new ChessPosition('B', 1));
        pawnPositions.add(new ChessPosition('H', 6));
        pawnPositions.add(new ChessPosition('D', 3));
        pawnPositions.add(new ChessPosition('E', 3));
        pawnPositions.add(new ChessPosition('G', 3));
        pawnPositions.add(new ChessPosition('E', 2));
        System.out.println("The pawn positions which camel can kill are: " + getPossiblePawnPositions(camel, pawnPositions));


public static List<ChessPosition> getPossiblePawnPositions(ChessPosition camelPosition, List<ChessPosition> pawnPositions) {
//Write code here to get possible pawn positions which a camel can kill.
class ChessPosition {

char column;
int row;

public ChessPosition(char column, int row) {
this.column = column;
this.row = row;

public boolean equals(Object obj) {
ChessPosition position = (ChessPosition) obj;
return column == position.column && row == position.row;

public String toString() {
return column + "" + row;



Topic: Collection Framework In Java

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App