From 53cff837ea731740be1c88c4e3235371a51c4bde Mon Sep 17 00:00:00 2001 From: lov3b Date: Sun, 15 May 2022 18:48:20 +0200 Subject: [PATCH] =?UTF-8?q?Ta=20bort=20duplikationer=20ur=20koden,=20men?= =?UTF-8?q?=20g=C3=B6r=20den=20mer=20komplex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/King.java | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) 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; - } /**