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 possible positions which a white minister can move without getting killed by black minister and black horse. Assume that there is nothing else on the chess board apart from these three.
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 (White Minister Position, Black Minister Position, Black Horse Position) Output (Sorted List of Positions)
White Minister = C6, Black Minister = B4, Black Horse = A3 [A6, A8, C1, C7, C8, D5, D7, E6, E8, F3, F6, G2, G6, H1, H6]
White Minister = C3, Black Minister = G3, Black Horse = E5 [A1, A5, B2, B4, C1, C2, C5, C8, D2, D4, F6, H8]
White Minister = A5, Black Minister = D3, Black Horse = G6 [A1, A2, A4, A7, A8, B4, B6, C5, C7, E1, G5, H5]
White Minister = D1, Black Minister = A3, Black Horse =F4 [B1, C2, D2, D4, D7, D8, E1, F1, G1, G4, H1]

class FindWhiteMinisterPositionsWithBlackMinisterAndHorse

{    public static void main(String s[])
        ChessPosition whiteMinister = new ChessPosition('C', 6);
        ChessPosition blackMinister = new ChessPosition('B', 4);
        ChessPosition blackHorse = new ChessPosition('A', 3);
        System.out.println("The positions which the white minister can move with out getting killed : " + getPossiblePositions(whiteMinister, blackMinister, blackHorse));


public static List<ChessPosition> getPossiblePositions(ChessPosition whiteMinister, ChessPosition blackMinister, ChessPosition blackHorse) {
//Write code here to get the positions in which the white minister can move with out getting killed by black minister and black horse
class ChessPosition {

char column;
int row;

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

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



Topic: Java ArrayList

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App