Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

2d array chess example

–1 vote
asked Sep 4, 2019 by basel 1 flag
how to make the able moves for queen in chess by java

1 Answer

–2 votes
answered Dec 26, 2019 by anonymous
import java.io.*;

import java.util.*;

public class Chess

{

public static void main (String[] args)

{

Scanner sc=new Scanner(System.in);

int n = 4;

int k = 0;

int x = 2;

int y = 2;

int obstPosx[] = { 4,3 };

int obstPosy[] = { 2,3 };

int d11, d12, d21, d22, r1, r2, c1, c2;

d11 = Math.min( x-1, y-1 );

d12 = Math.min( n-x, n-y );

d21 = Math.min( n-x, y-1 );

d22 = Math.min( x-1, n-y );

r1 = y-1;

r2 = n-y;

c1 = x-1;

c2 = n-x;

for (int i = 0; i < k; i++)

{

if ( x > obstPosx[i] && y > obstPosy[i] && x-obstPosx[i] == y-obstPosy[i] )

{

d11 = Math.min(d11, x-obstPosx[i]-1);

}

if ( obstPosx[i] > x && obstPosy[i] > y && obstPosx[i]-x == obstPosy[i]-y )

{

d12 = Math.min( d12, obstPosx[i]-x-1);

}

if ( obstPosx[i] > x && y > obstPosy[i] && obstPosx[i]-x == y-obstPosy[i] )

{

d21 = Math.min(d21, obstPosx[i]-x-1);

}

if ( x > obstPosx[i] && obstPosy[i] > y && x-obstPosx[i] == obstPosy[i]-y )

{

d22 = Math.min(d22, x-obstPosx[i]-1);

}

if ( x == obstPosx[i] && obstPosy[i] < y )

{

r1 = Math.min(r1, y-obstPosy[i]-1);

}

if ( x == obstPosx[i] && obstPosy[i] > y )

{

r2 = Math.min(r2, obstPosy[i]-y-1);

}

if ( y == obstPosy[i] && obstPosx[i] < x )

{

c1 = Math.min(c1, x-obstPosx[i]-1);

}

if ( y == obstPosy[i] && obstPosx[i] > x )

{

c2 = Math.min(c2, obstPosx[i]-x-1);

}

}

System.out.println(d11 + d12 + d21 + d22 + r1 + r2 + c1 + c2);

}

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...