diff --git a/src/schack/Board.java b/src/schack/Board.java index c62eefc..5182e01 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -178,7 +178,7 @@ public class Board extends JPanel implements MouseListener { if (!validMovesToDraw.contains(clicked)) { try { - Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; + final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; // Kolla endast ifall vi kan röra på pjäsen om det är samma färg som den tur vi är på if (selectedPiece.isWhite() == whitesTurn || developerMode) { @@ -191,7 +191,7 @@ public class Board extends JPanel implements MouseListener { ArrayList allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { - if (piece == null || whitesTurn != piece.isWhite) { + if (piece == null || whitesTurn != piece.isWhite()) { continue; } // Kolla ifall vi är samma färg som får röra sig @@ -224,7 +224,7 @@ public class Board extends JPanel implements MouseListener { for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { // Ifall det är tomrum skippa - if (piece == null || preferedColor != piece.isWhite) { + if (piece == null || preferedColor != piece.isWhite()) { continue; } // Lägg till alla attacker för respektive färg diff --git a/src/schack/King.java b/src/schack/King.java index cdc844c..5cf7804 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -11,6 +11,12 @@ public final class King extends PieceKnownIfMoved { supremeRuler = true; } + /** + * Få en ArrayList med möjliga rockadMoves + * + * @param pieces + * @return + */ private ArrayList getCastlingIfPossible(Piece[][] pieces) { ArrayList possibleCastling = new ArrayList<>(); if (this.isMoved()) { @@ -59,6 +65,12 @@ public final class King extends PieceKnownIfMoved { } + /** + * Gör en rockad + * + * @param pieces + * @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]; diff --git a/src/schack/LongWalkers.java b/src/schack/LongWalkers.java index c50aa58..419d89b 100644 --- a/src/schack/LongWalkers.java +++ b/src/schack/LongWalkers.java @@ -10,6 +10,13 @@ public abstract class LongWalkers extends PieceKnownIfMoved { super(isWhite, startingPosition); } + /** + * Generell metod för att generera möjliga drag för LongWalkers + * @param directions + * @param pieces + * @param isSelected + * @return + */ ArrayList getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) { ArrayList movable = new ArrayList<>(); @@ -19,7 +26,6 @@ public abstract class LongWalkers extends PieceKnownIfMoved { loopX += xy[0]; loopY += xy[1]; boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); - if (shouldBreak) { break; } diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index e82588f..e9198b2 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -17,12 +17,12 @@ public class Pawn extends PieceKnownIfMoved { // 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)); + Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1)); if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { continue; } Piece piece = pieces[pos.x][pos.y]; - if (piece == null || piece.isWhite != piece.isWhite) { + if (piece == null || piece.isWhite() != piece.isWhite()) { movable.add(pos); } } @@ -39,7 +39,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)); + Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY)); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); if (shouldBreak) { break; @@ -49,7 +49,7 @@ public class Pawn extends PieceKnownIfMoved { // 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)); + Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1)); addAttackMovesIfCan(pos, movable, pieces); } return movable; diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 2308356..d1d8dab 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -6,7 +6,6 @@ import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.Arrays; import javax.imageio.ImageIO; public abstract class Piece { @@ -19,7 +18,7 @@ public abstract class Piece { /** * Sant ifall pjäsens färg är vit, falskt ifall den är svart */ - public boolean isWhite; + private boolean isWhite; /** * SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:) */ @@ -51,7 +50,7 @@ public abstract class Piece { */ private void setPieceIcon() throws IOException { String className = this.getClass().getSimpleName(); - String colorName = isWhite ? "White" : "Black"; + String colorName = this.isWhite() ? "White" : "Black"; String fileName = colorName + className + ".png"; InputStream is = getClass().getResourceAsStream("/img/" + fileName); icon = ImageIO.read(is); @@ -109,6 +108,14 @@ public abstract class Piece { this.position = new Point(toMove); } + /** + * Lägg till move ifall det går, alltså inte är schack där + * @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 + * @return true ifall man inte kan gå längre i denna riktning + */ protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { // Ifall vi är utanför brädet ge tillbaka false if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) { @@ -133,7 +140,7 @@ public abstract class Piece { * längre Ifall det är samma färg som oss betyder det att vi inte kan * lägga till den */ - if (pieceToCheck.isWhite() != this.isWhite) { + if (pieceToCheck.isWhite() != this.isWhite()) { /** * Detta betyder att det är en motsatts pjäs här, vi kan ta men inte * gå längre @@ -218,6 +225,10 @@ public abstract class Piece { // return "Piece{" + "position=" + position + ", isWhite=" + white + '}'; } + /** + * + * @return true ifall pjäsen är vit + */ public boolean isWhite() { return isWhite; }