diff --git a/src/schack/King.java b/src/schack/King.java index 70fe6e1..f42d40a 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -3,6 +3,8 @@ package schack; import java.awt.Point; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.stream.Stream; public final class King extends PieceKnownIfMoved { @@ -23,6 +25,26 @@ public final class King extends PieceKnownIfMoved { 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 boolean nothingInBetweenAndNotSchackOnTheWay = true; 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)); } } - + */ return possibleCastling; - } /**