Menu
Topics Index
...
`

Find all the pawns which a camel can kill


If you need explanation Read this topic

If you need Answer Take test on this topic

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]


import java.util.*;
class FindKillablePawnsByCamel
{
    public static void main(String s[])
    {
        ChessPosition camel = new ChessPosition('C', 1);
        List<ChessPosition> pawnPositions = new ArrayList<ChessPosition>();
        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) {
    }

}
class ChessPosition {

char column;
int row;

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

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

@Override
public String toString() {
return column + "" + row;
}
}
Topic: Collection Framework In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

now firstly understand the concept of how camel moves on chess to kill a pawn.... columns are marked by alphabets and rows are marked with no.  ....so,  for a given cell1 on wich a camel is present already, it can furthr enter only in that cell2 which satisfy the followng cndtn ->> the ascii value dfrnc of columns of both cell(tkng  value) is eql to the dfrnc in row numbr of both cell.. for ex -> if camel is on cell.. a3... now to chek whetr it can go to cell b2 or not..... firsly find the ascii value difrnce of A-B =1 AND ALSO ROW NO. DFRNCE IS 1 ...HENCE CAMEL CAN ENTR INTO B2 ......so make a fnctn that works on this concept....... wen this function is called ....we will get the result as all the cells on wich camel can move..... now we have to arrange these resulting cells... firstly perform sorting on the basis of column alphabet..... den sort acc  to row number if two adjacent cells of sorted result hv same alphabets

Posted by Asma Mujtaba Khan    2015-01-30 10:45:51


first we need to know the how camel will move in the chess..it will move in the cross direction..
here we are receiving the instance of the ChessPosition class one represent row and another represents column of camel..here camel will kill pawn if(row of camel-row pwan)==col of camel-col of pawn) than it than camel can kill the pawn..here we need to take absolute value..to avoid the negative values..if the condition is true than add to the List..
Note: here we have pawn positions in the form of list so take for loop to travel.

Posted by Uday Kumar    2015-01-31 03:15:00


This dose is now closed and the winners are Asma Mujtaba Khan,  for 'First Correct Comment', Uday Kumar,  for 'Second Correct Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-02 07:00:25


© meritcampus 2019

All Rights Reserved.

Open In App