From 9332de1a2fa42c99b1cfdc83fb808b5b4e7be839 Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 29 Mar 2022 15:50:09 +0200 Subject: [PATCH] Update King.java --- src/schack/King.java | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/schack/King.java b/src/schack/King.java index 02f4c07..f436afe 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -29,25 +29,31 @@ public final class King extends PieceKnownIfMoved { // Vänster boolean nothingInBetween = true; - for (int kingX = this.position.x - 1; kingX >= 0; kingX--) { + for (int loopX = this.position.x - 1; loopX >= 0; loopX--) { // Kolla ifall vi kollar tornet och inget är emellan - if (kingX == 0 && nothingInBetween) { - movable.add(new Point(2, 7)); + if (loopX == 0 && nothingInBetween) { + movable.add(new Point(2, this.position.y)); } // Kolla ifall det är tomt emellan kung och torn - if (pieces[kingX][this.position.y] != null) { + if (pieces[loopX][this.position.y] != null) { nothingInBetween = false; } } // Höger - for (int kingX = this.position.x + 1; kingX <= 7; kingX++) { - boolean shouldBreak = addMovesIfCan(new Point(kingX, this.position.y), movable, pieces); - if (shouldBreak) { - break; + for (int loopX = this.position.x + 1; loopX <= 7; loopX++) { + + // Kolla ifall vi kollar tornet och inget är emellan + if (loopX == 7 && nothingInBetween) { + movable.add(new Point(6, this.position.y)); + } + + // Kolla ifall det är tomt emellan kung och torn + if (pieces[loopX][this.position.y] != null) { + nothingInBetween = false; } } @@ -67,7 +73,6 @@ public final class King extends PieceKnownIfMoved { // Uppdatera brädet pieces[king.position.x][king.position.y] = king; pieces[rook.position.x][rook.position.y] = rook; - } @Override