John Doe
John Doe

Reputation: 73

Using 2 input coordinates of the chess board, identify whether or not they crossed each other's path (using queen moves of the chess game)

class Board 
{
     public static void main(String args[]) 
    {

        int i, j;
        int x1 = 0, y1 = 0;
        int x2 = 0, y2 = 0;
        int[][] board = new int[8][8];

        x1 = Integer.parseInt(args[0]); 
        y1 = Integer.parseInt(args[1]); 
        x2 = Integer.parseInt(args[2]); 
        y2 = Integer.parseInt(args[3]); 


        // initialize the board to 0's
        for (i = 0; i < 8; i++)
            for (j = 0; j < 8; j++)
                board[i][j] = 0;

        board[x1][y1] = 1;      
        board[x2][y2] = 1;      

        for (i = 0; i < 8; i++) 
        {
            for (j = 0; j < 8; j++) 
            {
                System.out.print(board[i][j]+" ");
            }
            System.out.println();
        }   

    }
}

This is what I only managed to do which is to print the board with 0's and 1's

board:

0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1

My goal is to code and identify if the 2 queens(which is the two 1's) would cross each other.

I tried a lot of methods but some of them don't work. If you can help me I really appreciate it :)

P.S still learning to code :)

Upvotes: 2

Views: 4088

Answers (2)

Nikolas
Nikolas

Reputation: 44418

Welcome to StackOverflow :)

Here is what are you looking for:

public static boolean twoQueensSeeEachOther(int x1, int y1, int x2, int y2) {
    if (x1 == x2 && y1 == y2) {
        return true;                                // One has picked another
    }
    if (x1 == x2 || y1 == y2) {
        return true;                                // Row or column
    }
    if (Math.abs(x1 - x2) == Math.abs(y1 - y2)) {
        return true;                                // Diagonal
    }
    return false;
}

There are these conditions under two queens can see each other:

  • If they are both in the same place, one picked another

  • If they share the same axis (either x or y), they see each other since they can move as a rook. This condition is met if their x or y positions are the same.

  • If they share the same diagonal, they see each other since they can move as a bishop. This condition is met if the differences between axes are equal. Example:

    • Black queen on position [2,5] and white queen on position [4,3].
    • The difference between the x axes is xDiff = abs(2 - 4) = 2.
    • The difference between the y axes is yDiff = abs(5 - 3) = 2.
    • Both the differences are equal - they see each other in the diagonal.

Upvotes: 4

Saif Rahman
Saif Rahman

Reputation: 1

import java.util.Scanner;
class Main {

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
        int x1 = scanner.nextByte();
        int y1 = scanner.nextByte();
        int x2 = scanner.nextByte();
        int y2 = scanner.nextByte();
        boolean sameRow = y1 == y2;
        boolean sameColumn = x1 == x2;
        boolean canAttack;

        if (sameRow || sameColumn) {
            canAttack = true;
        } else {
            canAttack = Math.abs(x1 - x2) == Math.abs(y1 - y2);
        }
        System.out.println(canAttack ? "YES" : "NO");

}
}

Upvotes: 0

Related Questions