diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 69ab778..e62756c 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -75,10 +75,7 @@ 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()) { - boolean isSchack = isInSchack(pos, pieces); - if (isSchack) { - movable.add(pos); - } + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } return movable; } @@ -91,10 +88,7 @@ public class Pawn extends PieceKnownIfMoved { Piece pieceToCheck = pieces[pos.x][pos.y]; if (pieceToCheck == null) { - boolean isSchack = isInSchack(pos, pieces); - if (isSchack) { - movable.add(pos); - } + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); return false; } return true; diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 9f7680a..342cf40 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -69,8 +69,7 @@ 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) { @@ -132,11 +131,7 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - boolean isSchack = isInSchack(pos, pieces); - if (isSchack) { - movable.add(pos); - } - + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } // Fortsätt att gå return false; @@ -155,10 +150,7 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - boolean isSchack = isInSchack(pos, pieces); - if (isSchack) { - movable.add(pos); - } + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } } return true; @@ -173,7 +165,8 @@ public abstract class Piece { * @param pos * @return */ - boolean isSchackHere(Piece[][] pieces, Point pos) { + ArrayList tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) { + ArrayList movable = new ArrayList<>(); // Kom ihåg vart vi var Point previousPosition = new Point(this.position); @@ -191,8 +184,11 @@ 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; } /**