From 4b92c6d3883196d848d13066e200c89a9762897e Mon Sep 17 00:00:00 2001 From: lov3b Date: Tue, 22 Mar 2022 18:40:39 +0100 Subject: [PATCH] =?UTF-8?q?br=C3=B6t=20ut=20checkAttack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/Pawn.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 3747af8..3fc6d4f 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -32,21 +32,26 @@ public class Pawn extends Piece { } } - // Logik för att ta + // Kolla ifall vi kan ta någon for (int pawnX : new int[]{-1, 1}) { // Position vi kollar just nu, snett upp åt höger & vänster Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1)); - Piece p = pieces[pos.x][pos.y]; - - // Ifall det är en pjäs som står här och den inte är samma färg som oss, lägg till - if (p != null && p.isWhite != this.isWhite) { - movable.add(pos); - } + checkAttack(pos, movable, pieces); } return movable; } + // Känns som det här skulle kunnat vara i validMoves, men nu är det såhär + private void checkAttack(Point pos, LinkedHashSet movable, Piece[][] pieces) { + Piece p = pieces[pos.x][pos.y]; + + // Ifall det är en pjäs som står här och den inte är samma färg som oss, lägg till + if (p != null && p.isWhite != this.isWhite) { + movable.add(pos); + } + } + @Override protected boolean checkMove(Point pos, LinkedHashSet movable, Piece[][] pieces) { // Instead of checking index and null, try-catch