From afb9dba8d50bc8e0c84aa1c05e80ac87a19fe277 Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 5 May 2022 08:56:19 +0200 Subject: [PATCH] =?UTF-8?q?=C3=84ndringar=20nicklas=20snyggt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/Pawn.java | 4 ++-- src/schack/Piece.java | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 2abbd8d..be69717 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -78,7 +78,7 @@ public class Pawn extends PieceKnownIfMoved { if (piece == null) { return; } else if (piece.isWhite() != this.isWhite()) { - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } } @@ -107,7 +107,7 @@ public class Pawn extends PieceKnownIfMoved { } } catch (NullPointerException npe) { // This is an empty spot - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } catch (IndexOutOfBoundsException ioobe) { // This means that the player is at the edge System.out.println(pos); diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 07874d8..4f8f7d0 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -123,7 +123,7 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } // Fortsätt att gå return false; @@ -142,14 +142,15 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } } return true; } - void tryToMoveAndCheckIfCheck(Piece[][] pieces, ArrayList movable, Point pos) { + ArrayList tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) { + ArrayList movable = new ArrayList<>(); // Kom ihåg vart vi var Point previousPosition = new Point(this.position); @@ -171,6 +172,7 @@ public abstract class Piece { if (!inSchack) { movable.add(pos); } + return movable; } boolean checkIfSchack(Point pos, Piece[][] pieces) { @@ -198,7 +200,6 @@ public abstract class Piece { attacks.addAll(piece.validAttacks(pieces)); } }*/ - // Kollar ifall kungen står i schack just nu for (Point attack : attacks) { Piece attacked = pieces[attack.x][attack.y];