diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index e62756c..69ab778 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -75,7 +75,10 @@ public class Pawn extends PieceKnownIfMoved { Piece piece = pieces[pos.x][pos.y]; // Ifall det är tomt här, gör ingenting if (piece != null && piece.isWhite() != this.isWhite()) { - movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); + boolean isSchack = isInSchack(pos, pieces); + if (isSchack) { + movable.add(pos); + } } return movable; } @@ -88,7 +91,10 @@ public class Pawn extends PieceKnownIfMoved { Piece pieceToCheck = pieces[pos.x][pos.y]; if (pieceToCheck == null) { - movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); + boolean isSchack = isInSchack(pos, pieces); + if (isSchack) { + movable.add(pos); + } return false; } return true; diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 342cf40..9f7680a 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -69,7 +69,8 @@ public abstract class Piece { * Ger tillbaks alla ställen pjäsen kan attackera * * @param pieces - * @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man inte ska bry sig ifall + * @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man inte ska bry sig + * ifall * @return */ public ArrayList validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) { @@ -131,7 +132,11 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); + boolean isSchack = isInSchack(pos, pieces); + if (isSchack) { + movable.add(pos); + } + } // Fortsätt att gå return false; @@ -150,7 +155,10 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); + boolean isSchack = isInSchack(pos, pieces); + if (isSchack) { + movable.add(pos); + } } } return true; @@ -165,8 +173,7 @@ public abstract class Piece { * @param pos * @return */ - ArrayList tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) { - ArrayList movable = new ArrayList<>(); + boolean isSchackHere(Piece[][] pieces, Point pos) { // Kom ihåg vart vi var Point previousPosition = new Point(this.position); @@ -184,11 +191,8 @@ public abstract class Piece { pieces[previousPosition.x][previousPosition.y] = this; pieces[pos.x][pos.y] = opponentPiece; this.position = new Point(previousPosition); + return inSchack; - if (!inSchack) { - movable.add(pos); - } - return movable; } /**