Ta bort duplikationer ur koden, men gör den mer komplex

This commit is contained in:
lov3b 2022-05-15 18:48:20 +02:00
parent 0548a96061
commit 53cff837ea

View File

@ -3,6 +3,8 @@ package schack;
import java.awt.Point; import java.awt.Point;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Stream;
public final class King extends PieceKnownIfMoved { public final class King extends PieceKnownIfMoved {
@ -23,6 +25,26 @@ public final class King extends PieceKnownIfMoved {
return possibleCastling; return possibleCastling;
} }
boolean[] nothingInBetweenAndNotSchackOnTheWay = new boolean[2]; // Vänster, höger
final int LEFT_MODIFIER = -1, RIGHT_MODIFIER = 1;
for (final int modifier : new int[]{LEFT_MODIFIER, RIGHT_MODIFIER}) {
for (int loopX = this.position.x + modifier; loopX > 0 && loopX < 7; loopX += modifier) {
if (pieces[loopX][this.position.y] != null || isInSchack(pieces, new Point(loopX, this.position.y))) {
nothingInBetweenAndNotSchackOnTheWay[(modifier == RIGHT_MODIFIER) ? 0 : 1] = true;
break;
}
}
}
final int LEFT_DIRECTION = 0, RIGHT_DIRECTION = 1;
for (final int direction : new int[]{LEFT_DIRECTION, RIGHT_DIRECTION}) {
if (nothingInBetweenAndNotSchackOnTheWay[direction]) {
final Piece possibleRook = pieces[direction == LEFT_DIRECTION ? 0 : 7][this.position.y];
if (possibleRook != null && !possibleRook.isMoved()) {
possibleCastling.add(new Point(direction == LEFT_DIRECTION ? 2 : 6, this.position.y));
}
}
}
/*
// Vänster // Vänster
boolean nothingInBetweenAndNotSchackOnTheWay = true; boolean nothingInBetweenAndNotSchackOnTheWay = true;
for (int loopX = this.position.x - 1; loopX > 0; loopX--) { for (int loopX = this.position.x - 1; loopX > 0; loopX--) {
@ -52,9 +74,8 @@ public final class King extends PieceKnownIfMoved {
possibleCastling.add(new Point(6, this.position.y)); possibleCastling.add(new Point(6, this.position.y));
} }
} }
*/
return possibleCastling; return possibleCastling;
} }
/** /**