Menu
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 positions to which the elephant can move to. Assume that there is nothing else 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) Output (Sorted List of Positions)
D4 [A4, B4, C4, D1, D2, D3, D5, D6, D7, D8, E4, F4, G4, H4]
A1 [A2, A3, A4, A5, A6, A7, A8, B1, C1, D1, E1, F1, G1, H1]
F8 [A8, B8, C8, D8, E8, F1, F2, F3, F4, F5, F6, F7, G8, H8]
H6 [A6, B6, C6, D6, E6, F6, G6, H1, H2, H3, H4, H5, H7, H8]

class FindElephantPositions

{    public static void main(String s[])
    {
        ChessPosition elephant = new ChessPosition('D', 4);
        System.out.println("The positions which elephant can move to: " + getPossiblePositions(elephant));

    }


public static List<ChessPosition> getPossiblePositions(ChessPosition position) {
//Write a code here to get possible positions to which a elephant can move.
}
}
class ChessPosition {

char column;
int row;

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

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

Doubts

Problems

Topic: Collection Framework In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App