diff --git a/src/schack/Bishop.java b/src/schack/Bishop.java index 4e8a586..5f60220 100644 --- a/src/schack/Bishop.java +++ b/src/schack/Bishop.java @@ -11,11 +11,11 @@ public class Bishop extends LongWalkers { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean isSelected) { + public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { return getMoves( new int[][]{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}}, pieces, - isSelected + allowedToRecurse ); } diff --git a/src/schack/Horse.java b/src/schack/Horse.java index a4245fd..81a120d 100644 --- a/src/schack/Horse.java +++ b/src/schack/Horse.java @@ -11,7 +11,7 @@ public class Horse extends Piece { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean isSelected) { + public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { ArrayList movable = new ArrayList<>(); for (int dx : new int[]{-2, -1, 1, 2}) { @@ -19,7 +19,7 @@ public class Horse extends Piece { int stepLength = (3 - Math.abs(dx)); int dy = direction * stepLength; Point potentialMove = new Point(this.position.x + dx, this.position.y + dy); - addMovesIfCan(potentialMove, movable, pieces, isSelected); + addMovesIfCan(potentialMove, movable, pieces, allowedToRecurse); } } return movable; diff --git a/src/schack/King.java b/src/schack/King.java index b6e6e11..70fe6e1 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -24,43 +24,35 @@ public final class King extends PieceKnownIfMoved { } // Vänster - boolean nothingInBetween = true; - for (int loopX = this.position.x - 1; loopX >= 0; loopX--) { - - // Kolla ifall vi kollar tornet och inget är emellan - if (loopX == 0 && nothingInBetween) { - - // Check så att man bara kan göra rockad ifall tornet inte rört sig - Piece possibleRook = pieces[loopX][this.position.y]; - if (possibleRook != null && !possibleRook.isMoved()) { - possibleCastling.add(new Point(2, this.position.y)); - } + boolean nothingInBetweenAndNotSchackOnTheWay = true; + for (int loopX = this.position.x - 1; loopX > 0; loopX--) { + if (pieces[loopX][this.position.y] != null || isInSchack(pieces, new Point(loopX, this.position.y))) { + nothingInBetweenAndNotSchackOnTheWay = false; + break; } - - // Kolla ifall det är tomt emellan kung och torn - if (pieces[loopX][this.position.y] != null) { - nothingInBetween = false; + } + if (nothingInBetweenAndNotSchackOnTheWay) { + Piece possibleRook = pieces[0][this.position.y]; + if (possibleRook != null && !possibleRook.isMoved()) { + possibleCastling.add(new Point(2, this.position.y)); } } // Höger - nothingInBetween = true; - for (int loopX = this.position.x + 1; loopX <= 7; loopX++) { - - // Kolla ifall vi kollar tornet och inget är emellan - if (loopX == 7 && nothingInBetween) { - // Check så att man bara kan göra rockad ifall tornet inte rört sig - Piece possibleRook = pieces[loopX][this.position.y]; - if (possibleRook != null && !possibleRook.isMoved()) { - possibleCastling.add(new Point(6, this.position.y)); - } - } - - // Kolla ifall det är tomt emellan kung och torn - if (pieces[loopX][this.position.y] != null) { - nothingInBetween = false; + nothingInBetweenAndNotSchackOnTheWay = true; + for (int loopX = this.position.x + 1; loopX < 7; loopX++) { + if (pieces[loopX][this.position.y] != null || isInSchack(pieces, new Point(loopX, this.position.y))) { + nothingInBetweenAndNotSchackOnTheWay = false; + break; } } + if (nothingInBetweenAndNotSchackOnTheWay) { + Piece possibleRook = pieces[7][this.position.y]; + if (possibleRook != null && !possibleRook.isMoved()) { + possibleCastling.add(new Point(6, this.position.y)); + } + } + return possibleCastling; } @@ -72,7 +64,6 @@ public final class King extends PieceKnownIfMoved { * @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll */ private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) { - Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y]; Piece king = this; @@ -99,7 +90,7 @@ public final class King extends PieceKnownIfMoved { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean isSelected) { + public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { ArrayList movable = new ArrayList<>(); for (int loopX = -1; loopX < 2; loopX++) { @@ -107,11 +98,13 @@ public final class King extends PieceKnownIfMoved { if (loopY == 0 && loopX == 0) { continue; } - addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces, isSelected); + addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces, allowedToRecurse); } } - movable.addAll(getCastlingIfPossible(pieces)); + if (allowedToRecurse && !isInSchack(pieces)) { + movable.addAll(getCastlingIfPossible(pieces)); + } return movable; } diff --git a/src/schack/LongWalkers.java b/src/schack/LongWalkers.java index 419d89b..2142731 100644 --- a/src/schack/LongWalkers.java +++ b/src/schack/LongWalkers.java @@ -12,12 +12,13 @@ public abstract class LongWalkers extends PieceKnownIfMoved { /** * Generell metod för att generera möjliga drag för LongWalkers + * * @param directions * @param pieces - * @param isSelected - * @return + * @param allowedToRecurse + * @return */ - ArrayList getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) { + ArrayList getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) { ArrayList movable = new ArrayList<>(); for (int[] xy : directions) { @@ -25,7 +26,7 @@ public abstract class LongWalkers extends PieceKnownIfMoved { while (loopX + xy[0] >= 0 && loopX + xy[0] <= 7 && loopY + xy[1] >= 0 && loopY + xy[1] <= 7) { loopX += xy[0]; loopY += xy[1]; - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, allowedToRecurse); if (shouldBreak) { break; } diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 36be572..65daf4d 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -14,8 +14,8 @@ public class Pawn extends PieceKnownIfMoved { * Ger tillbaks alla ställen pjäsen kan attackera * * @param pieces - * @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man ska kolla ifall det är något i möjliga attackrutor - * ifall + * @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man ska kolla ifall + * det är något i möjliga attackrutor ifall * @return Alla lämpliga attackMoves */ @Override @@ -40,7 +40,7 @@ public class Pawn extends PieceKnownIfMoved { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean isSelected) { + public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { ArrayList movable = new ArrayList<>(); // Om bonden har gått en gång, får gå 1 steg, annars 2 @@ -49,7 +49,7 @@ public class Pawn extends PieceKnownIfMoved { // Kolla om man kan gå rakt frak for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY)); - boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); + boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse); if (shouldBreak) { break; } @@ -91,7 +91,7 @@ public class Pawn extends PieceKnownIfMoved { } @Override - protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { + protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean allowedToRecurse) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { return false; } diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 15c54e8..8c72af1 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -27,13 +27,13 @@ public abstract class Piece { * Bild av pjäsen som ritas ut på bärdet */ protected BufferedImage icon; - + public Piece(boolean white, Point startingPosition) throws IOException { this.isWhite = white; this.position = startingPosition; setPieceIcon(); } - + public Piece(boolean white) { this.isWhite = white; } @@ -56,10 +56,10 @@ public abstract class Piece { * Ger tillbaks alla ställen pjäsen kan gå till * * @param pieces - * @param isSelected + * @param allowedToRecurse * @return */ - public abstract ArrayList validMoves(Piece[][] pieces, boolean isSelected); + public abstract ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse); /** * Ger tillbaks alla ställen pjäsen kan attackera @@ -98,7 +98,7 @@ public abstract class Piece { if (toMove.x >= pieces.length || toMove.y < 0 || position.x >= pieces[0].length || position.y < 0) { return; } - + pieces[toMove.x][toMove.y] = this; pieces[position.x][position.y] = null; this.position = new Point(toMove); @@ -110,20 +110,20 @@ public abstract class Piece { * @param pos drag att lägga till ifall det går * @param movable lägger till drag i denna ArrayList * @param pieces Piece[][] över brädet - * @param isSelected + * @param allowedToRecurse * @return true ifall man inte kan gå längre i denna riktning */ - protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { + protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean allowedToRecurse) { // Ifall vi är utanför brädet ge tillbaka false if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) { return false; } - + Piece pieceToCheck = pieces[pos.x][pos.y]; // Detta är en tom plats if (pieceToCheck == null) { - if (!isSelected || !isInSchack(pieces, pos)) { + if (!allowedToRecurse || !isInSchack(pieces, pos)) { movable.add(pos); } return false; @@ -135,11 +135,11 @@ public abstract class Piece { * lägga till den */ if ((pieceToCheck.isWhite() != this.isWhite()) - && ((isSelected && !isInSchack(pieces, pos)) || !isSelected)) { + && ((allowedToRecurse && !isInSchack(pieces, pos)) || !allowedToRecurse)) { movable.add(pos); } return true; - + } /** @@ -149,7 +149,7 @@ public abstract class Piece { * @param pos Kollar ifall det är schack om denna Piece flyttar hit * @return true ifall det är schack */ - protected boolean isInSchack(Piece[][] pieces, Point pos) { + protected boolean isInSchack(Piece[][] pieces, Point pos) { // Kom ihåg vart vi var Point previousPosition = new Point(this.position); @@ -160,14 +160,14 @@ public abstract class Piece { pieces[pos.x][pos.y] = this; pieces[previousPosition.x][previousPosition.y] = null; this.position = pos; - + boolean inSchack = isInSchack(pieces); // Flytta tillbaka pieces[previousPosition.x][previousPosition.y] = this; pieces[pos.x][pos.y] = guyThatsAlreadyHere; this.position = previousPosition; - + return inSchack; } @@ -177,7 +177,7 @@ public abstract class Piece { * @param pieces Piece[][] över hela brädet * @return true ifall det är schack */ - private boolean isInSchack(Piece[][] pieces) { + protected boolean isInSchack(Piece[][] pieces) { ArrayList enemyAttacks = new ArrayList<>(); // Fråga alla pjäser vart de kan gå/ta @@ -199,7 +199,7 @@ public abstract class Piece { } return false; } - + @Override public String toString() { return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}'; @@ -222,5 +222,5 @@ public abstract class Piece { public boolean isMoved() { return false; } - + } diff --git a/src/schack/Queen.java b/src/schack/Queen.java index 5d8a500..458928a 100644 --- a/src/schack/Queen.java +++ b/src/schack/Queen.java @@ -11,11 +11,11 @@ public class Queen extends LongWalkers { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean isSelected) { + public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { return getMoves( new int[][]{{1, 0}, {-1, 0}, {0, 1}, {-1, -1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}}, pieces, - isSelected + allowedToRecurse ); } } diff --git a/src/schack/Rook.java b/src/schack/Rook.java index 8bc4a5f..f348810 100644 --- a/src/schack/Rook.java +++ b/src/schack/Rook.java @@ -11,11 +11,11 @@ public class Rook extends LongWalkers { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean isSelected) { + public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { return getMoves( - new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, - pieces, - isSelected + new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, + pieces, + allowedToRecurse ); }