From e7f5f3ca4486c624931a9b9eaeded490c4f524e2 Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 21 Apr 2022 09:26:34 +0200 Subject: [PATCH] =?UTF-8?q?Funkar=20f=C3=B6r=20allt=20utom=20b=C3=B6nder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/Piece.java | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 4242e6a..a8b7065 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -82,8 +82,34 @@ public abstract class Piece { } else { // Detta betyder att det är en med motsatts pjäs här // Vi kan ta men inte gå längre. - movable.add(pos); + if (!isSelected) { + movable.add(pos); + return true; + } + + // Kom ihåg vart vi var + Point previousPosition = new Point(this.position); + + // Kom ihåg motståndarpjäs + Piece opponentPiece = pieces[pos.x][pos.y]; + + // Testa att flytta + pieces[pos.x][pos.y] = this; + pieces[previousPosition.x][previousPosition.y] = null; + this.position = new Point(pos); + + boolean inSchack = checkIfSchack(pos, pieces); + + // Flytta tillbaka + pieces[previousPosition.x][previousPosition.y] = this; + pieces[pos.x][pos.y] = opponentPiece; + this.position = new Point(previousPosition); + + if (!inSchack) { + movable.add(pos); + } return true; + } } catch (NullPointerException npe) { // Detta är en tom plats, vi ska inte breaka