diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index c1705b8..0373615 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -33,7 +33,6 @@ public class Pawn extends PieceKnownIfMoved { @Override public LinkedHashSet validMoves(Piece[][] pieces, boolean isSelected) { - // TODO: Lösa bugg där bunder på kanterna inte kan röra sig LinkedHashSet movable = new LinkedHashSet<>(); // Om bonden har gått en gång, får gå 1 steg, annars 2 @@ -41,8 +40,6 @@ public class Pawn extends PieceKnownIfMoved { // Kolla om man kan gå rakt frak for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { - System.out.println("this.position.x: " + this.position.x); - System.out.println("calced y: " + (this.position.y + (this.white ? -pawnDY : pawnDY))); Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY)); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); if (shouldBreak) { @@ -68,7 +65,8 @@ public class Pawn extends PieceKnownIfMoved { try { Piece p = pieces[pos.x][pos.y]; if (p.white != this.white) { - movable.add(pos); + tryToMoveAndCheckIfCheck(pieces, movable, pos); + } } catch (Exception e) { } @@ -98,7 +96,7 @@ public class Pawn extends PieceKnownIfMoved { } } catch (NullPointerException npe) { // This is an empty spot - movable.add(pos); + tryToMoveAndCheckIfCheck(pieces, movable, 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 a8b7065..0e93f03 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -84,29 +84,8 @@ public abstract class Piece { // Vi kan ta men inte gå längre. 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); + } else { + tryToMoveAndCheckIfCheck(pieces, movable, pos); } return true; @@ -118,24 +97,7 @@ public abstract class Piece { return false; } - // Kom ihåg vart vi var - Point previousPosition = new Point(this.position); - - // 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] = null; - this.position = new Point(previousPosition); - - if (!inSchack) { - movable.add(pos); - } + tryToMoveAndCheckIfCheck(pieces, movable, pos); return false; } catch (IndexOutOfBoundsException ioobe) { @@ -147,6 +109,30 @@ public abstract class Piece { } + void tryToMoveAndCheckIfCheck(Piece[][] pieces, LinkedHashSet movable, Point pos) { + // 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); + } + } + boolean checkIfSchack(Point pos, Piece[][] pieces) { boolean ourColor = this.isWhite(); Piece selectedPiece = this;