import java.util.Scanner;
import java.util.Arrays;

class Main {
public static void main(String[] args) {
System.out.println("The marking for the black chess are the negative numbers and it's the opposite for the white. The field consists of 8 rows numbered from 0 to seven on the x-and y-coordinates. To make your move, choose the coordinate of the figure you want to move and the coordinate of the desired spot to move it to");

Scanner scanner = new Scanner(System.in);
int[][] array = {
{0,1,2,3,4,5,6,7,8},
{1,-4,-2,-3,-5,-6,-3,-2,-4},
{2,-1,-1,-1,-1,-1,-1,-1,-1},
{3,0,0,0,0,0,0,0,0},
{4,0,0,0,0,0,0,0,0},
{5,0,0,0,0,0,0,0,0},
{6,0,0 ,0,0,0,0,0,0},
{7,1,1,1,1,1,1,1,1},
{8,4,2,3,6,5,3,2,4}
};

boolean white = true;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(array[i][j] + " ");
}
System.out.print("\n");
}
int KingB = 0;
int KingW = 0;
int d = 0;

while (d < 8) {
int e = 0;
while (e < 8) {
if (array[d][e] == 6) {
KingW = 1;
e += 1;
}
if (array[d][e] == -6) {
KingB = 1;
e += 1;
} else {
e += 1;
}
}
d += 1;
}
d = 0;
if (KingB == 0) {
System.out.println("White has won.");
}
if (KingW == 0) {
System.out.println("Black has won.");
} else {
while (white == true) {
System.out.println("It's white's turn.");

System.out.println("Input the y-coordinate of the piece you want to move");
int x = scanner.nextInt();

System.out.println("Input the x-coordinate of the piece you want to move");
int y = scanner.nextInt();

if (x > 8) {
System.out.println("invalid move");
} else if (y > 8) {
System.out.println("invalid move");
} else if (array[x][y] == 0) {
System.out.println("invalid move");
} else if (array[x][y] < 0) {
System.out.println("invalid move");
} else {
int start = array[x][y];

System.out.println("Input the y-coordinate of the field the piece should go on");
int a = scanner.nextInt();

System.out.println("Input the x-coordinate of the field the piece should go on");
int b = scanner.nextInt();
if (array[a][b] <= 0) {
if (array[x][y] == 1) { // Pawn
if (a != x - 1 || b != y) {
if (a == x - 1) {
if (y == b + 1 || y == b - 1) {
if (array[a][b] < 0) {
array[a][b] = array[x][y];
array[x][y] = 0;
white = false;
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
} else {
array[a][b] = array[x][y];
array[x][y] = 0;
white = false;
}
}

if (array[x][y] == 6) { // King
if (a != x - 1 && a != x + 1 && b != y + 1 && b != y - 1) {
System.out.println("invalid move");
} else {
array[a][b] = array[x][y];
array[x][y] = 0;
white = false;
}
}

if (array[x][y] == 2 || array[x][y] == -2) { // Knight (Horse)
if ((Math.abs(x - a) == 2 && Math.abs(y - b) == 1) || (Math.abs(x - a) == 1 && Math.abs(y - b) == 2)) {
array[a][b] = array[x][y];
array[x][y] = 0;
white = false;
} else {
System.out.println("invalid move");
}
}

if (array[x][y] == 3 || array[x][y] == -3) { // Bishop
if (Math.abs(x - a) == Math.abs(y - b)) {
// Check for path clearance
int dx = (a > x) ? 1 : -1;
int dy = (b > y) ? 1 : -1;
int i = x + dx;
int j = y + dy;
boolean pathClear = true;
while (i != a && j != b) {
if (array[i][j] != 0) {
pathClear = false;
break;
}
i += dx;
j += dy;
}
if (pathClear) {
array[a][b] = array[x][y];
array[x][y] = 0;
white = false;
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
}

if (array[x][y] == 4 || array[x][y] == -4) { // Rook (Tower)
if (a == x || b == y) {
// Check for path clearance
int dx = (a == x) ? 0 : (a > x ? 1 : -1);
int dy = (b == y) ? 0 : (b > y ? 1 : -1);
int i = x + dx;
int j = y + dy;
boolean pathClear = true;
while (i != a || j != b) {
if (array[i][j] != 0) {
pathClear = false;
break;
}
i += dx;
j += dy;
}
if (pathClear) {
array[a][b] = array[x][y];
array[x][y] = 0;
white = false;
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
}

if (array[x][y] == 5 || array[x][y] == -5) { // Queen
if (Math.abs(x - a) == Math.abs(y - b) || x == a || y == b) {
// Check for path clearance
int dx = (a > x) ? 1 : (a < x) ? -1 : 0;
int dy = (b > y) ? 1 : (b < y) ? -1 : 0;
int i = x + dx;
int j = y + dy;
boolean pathClear = true;
while (i != a || j != b) {
if (array[i][j] != 0) {
pathClear = false;
break;
}
i += dx;
j += dy;
}
if (pathClear) {
array[a][b] = array[x][y];
array[x][y] = 0;
white = false;
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
}

for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(array[i][j] + " ");
}
System.out.print("\n");
}
} else {
System.out.println("invalid move");
}
}
}

while (white == false) {
System.out.println("It's black's turn.");

System.out.println("Input the y-coordinate of the piece you want to move");
int g = scanner.nextInt();

System.out.println("Input the x-coordinate of the piece you want to move");
int h = scanner.nextInt();

if (g > 8) {
System.out.println("invalid move");
} else if (h > 8) {
System.out.println("invalid move");
} else if (array[g][h] == 0) {
System.out.println("invalid move");
} else if (array[g][h] > 0) {
System.out.println("invalid move");
} else {
System.out.println("Input the y-coordinate of the field the piece should go on");
int k = scanner.nextInt();

System.out.println("Input the x-coordinate of the field the piece should go on");
int l = scanner.nextInt();
if (array[k][l] >= 0) {
if (array[g][h] == -1) { // Pawn
if (k != g + 1 || l != h) {
if (k == g + 1) {
if (h == k - 1 || h == l + 1) {
if (array[k][l] > 0) {
array[k][l] = array[g][h];
array[g][h] = 0;
white = true;
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
} else {
array[k][l] = array[g][h];
array[g][h] = 0;
white = true;
}
}

if (array[g][h] == -6) { // King
if (k != g - 1 && k != g + 1 && l != h + 1 && l != h - 1) {
System.out.println("invalid move");
} else {
array[k][l] = array[g][h];
array[g][h] = 0;
white = true;
}
}

if (array[g][h] == -2 || array[g][h] == 2) { // Knight (Horse)
if ((Math.abs(g - k) == 2 && Math.abs(h - l) == 1) || (Math.abs(g - k) == 1 && Math.abs(h - l) == 2)) {
array[k][l] = array[g][h];
array[g][h] = 0;
white = true;
} else {
System.out.println("invalid move");
}
}

if (array[g][h] == -3 || array[g][h] == 3) { // Bishop
if (Math.abs(g - k) == Math.abs(h - l)) {
int dx = (k > g) ? 1 : -1;
int dy = (l > h) ? 1 : -1;
int i = g + dx;
int j = h + dy;
boolean pathClear = true;
while (i != k && j != l) {
if (array[i][j] != 0) {
pathClear = false;
break;
}
i += dx;
j += dy;
}
if (pathClear) {
array[k][l] = array[g][h];
array[g][h] = 0;
white = true;
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
}

if (array[g][h] == -4 || array[g][h] == 4) { // Rook (Tower)
if (k == g || l == h) {
int dx = (k == g) ? 0 : (k > g ? 1 : -1);
int dy = (l == h) ? 0 : (l > h ? 1 : -1);
int i = g + dx;
int j = h + dy;
boolean pathClear = true;
while (i != k || j != l) {
if (array[i][j] != 0) {
pathClear = false;
break;
}
i += dx;
j += dy;
}
if (pathClear) {
array[k][l] = array[g][h];
array[g][h] = 0;
white = true;
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
}

if (array[g][h] == -5 || array[g][h] == 5) { // Queen
if (Math.abs(g - k) == Math.abs(h - l) || g == k || h == l) {
int dx = (k > g) ? 1 : (k < g) ? -1 : 0;
int dy = (l > h) ? 1 : (l < h) ? -1 : 0;
int i = g + dx;
int j = h + dy;
boolean pathClear = true;
while (i != k || j != l) {
if (array[i][j] != 0) {
pathClear = false;
break;
}
i += dx;
j += dy;
}
if (pathClear) {
array[k][l] = array[g][h];
array[g][h] = 0;
white = true;
} else {
System.out.println("invalid move");
}
} else {
System.out.println("invalid move");
}
}

for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(array[i][j] + " ");
}
System.out.print("\n");
}
} else {
System.out.println("invalid move");
}
}
}
}
}
}
