diff --git a/src/main/java/com/billenius/schack/Board.java b/src/main/java/com/billenius/schack/Board.java index 51a6b3e..701ffbd 100644 --- a/src/main/java/com/billenius/schack/Board.java +++ b/src/main/java/com/billenius/schack/Board.java @@ -83,26 +83,24 @@ public class Board extends JPanel implements MouseListener { } // Måla alla pjäser - for (Piece[] pieceArr : pieces) { + for (Piece[] pieceArr : pieces) for (Piece piece : pieceArr) { if (piece == null) { continue; } piece.draw(g2); } - } } private void drawSquares(Graphics2D g2) { g2.setBackground(Color.WHITE); g2.setColor(Color.DARK_GRAY); - for (int i = 0; i < 8; i += 2) { + for (int i = 0; i < 8; i += 2) for (int j = 0; j < 8; j += 2) { g2.fillRect(i * SIZE_OF_TILE, j * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE); g2.fillRect((i + 1) * SIZE_OF_TILE, (j + 1) * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE); } - } } @Override @@ -133,12 +131,12 @@ public class Board extends JPanel implements MouseListener { String msg = stateStr.charAt(0) + stateStr.substring(1, stateStr.length()).toLowerCase(); int choice = JOptionPane.showConfirmDialog(this, msg + "\nVill du starta om?"); - if (choice == JOptionPane.YES_OPTION) { + if (choice == JOptionPane.YES_OPTION) try { restartGame(); } catch (IOException ex) { } - } + break; default: } @@ -152,14 +150,13 @@ public class Board extends JPanel implements MouseListener { if (!validMovesToDraw.contains(clickedCoordinate)) { Piece selectedPiece = pieces[clickedCoordinate.x][clickedCoordinate.y]; - if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn) { + if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn) validMovesToDraw.addAll(selectedPiece.validMoves(pieces, true)); - } else { + else validMovesToDraw = new ArrayList<>(); // Snabbare än .clear - } - } else { + + } else validMovesToDraw = new ArrayList<>(); // Snabbare än .clear - } getParent().repaint(); } @@ -177,49 +174,45 @@ public class Board extends JPanel implements MouseListener { boolean inSchack = false; for (Point attack : opposingAttacks) { final Piece attacked = pieces[attack.x][attack.y]; - if (attacked == null) { + if (attacked == null) continue; - } + if (attacked.supremeRuler) { inSchack = true; validMovesToDraw = new ArrayList<>(); // Snabbare än .clear getParent().repaint(); - if (weCanMove) { + if (weCanMove) return SchackState.SCHACK; - } else { + else return SchackState.SCHACKMATT; - } } } - if (!inSchack && !weCanMove) { + if (!inSchack && !weCanMove) return SchackState.PATT; - } return SchackState.NORMAL; } private List getMoves(boolean whiteMovesAreWanted) { List allValidMoves = new ArrayList<>(); - for (Piece[] pieceArr : pieces) { + for (Piece[] pieceArr : pieces) for (Piece piece : pieceArr) { - if (piece == null || whiteMovesAreWanted != piece.isWhite()) { + if (piece == null || whiteMovesAreWanted != piece.isWhite()) continue; - } allValidMoves.addAll(piece.validMoves(pieces, true)); } - } + return allValidMoves; } public List getAttacks(boolean whiteAttacksAreWanted) { - List attacks = new ArrayList(); - for (Piece[] pieceArr : pieces) { + List attacks = new ArrayList<>(); + for (Piece[] pieceArr : pieces) for (Piece piece : pieceArr) { - if (piece == null || whiteAttacksAreWanted != piece.isWhite()) { + if (piece == null || whiteAttacksAreWanted != piece.isWhite()) continue; - } attacks.addAll(piece.validAttacks(pieces, true)); } - } + return attacks; } diff --git a/src/main/java/com/billenius/schack/Pawn.java b/src/main/java/com/billenius/schack/Pawn.java index 7d9286d..75bd5b4 100644 --- a/src/main/java/com/billenius/schack/Pawn.java +++ b/src/main/java/com/billenius/schack/Pawn.java @@ -30,14 +30,13 @@ public class Pawn extends Piece { 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)); - if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { + 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() != this.isWhite() - || (shouldNotCareIfAttackSpaceIsEmptyOrNot && piece.isWhite() != this.isWhite())) { + || (shouldNotCareIfAttackSpaceIsEmptyOrNot && piece.isWhite() != this.isWhite())) movable.add(pos); - } } return movable; @@ -54,9 +53,8 @@ public class Pawn extends Piece { for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { final Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY)); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse); - if (shouldBreak) { + if (shouldBreak) break; - } } // Kolla ifall vi kan ta någon @@ -78,7 +76,7 @@ public class Pawn extends Piece { * @param pieces */ private List addAttackMovesIfCan(Point pos, Piece[][] pieces) { - List movable = new ArrayList(); + List movable = new ArrayList<>(); // Se till att vi inte är utanför brädet if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) { return movable; @@ -94,16 +92,16 @@ public class Pawn extends Piece { } @Override - protected boolean addMovesIfCan(Point pos, List movable, Piece[][] pieces, boolean allowedToRecurse) { + protected boolean addMovesIfCan(Point pos, List movable, Piece[][] pieces, boolean allowedToRecurse) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { return false; } Piece pieceToCheck = pieces[pos.x][pos.y]; if (pieceToCheck == null) { - if (!isInSchack(pieces, pos)) { + if (!isInSchack(pieces, pos)) movable.add(pos); - } + return false; } return true; diff --git a/src/main/java/com/billenius/schack/Piece.java b/src/main/java/com/billenius/schack/Piece.java index a7fe100..a5d2fd7 100644 --- a/src/main/java/com/billenius/schack/Piece.java +++ b/src/main/java/com/billenius/schack/Piece.java @@ -125,17 +125,15 @@ public abstract class Piece { */ protected boolean addMovesIfCan(Point pos, List 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) { + 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 (!allowedToRecurse || !isInSchack(pieces, pos)) { + if (!allowedToRecurse || !isInSchack(pieces, pos)) movable.add(pos); - } return false; } @@ -145,9 +143,8 @@ public abstract class Piece { * lägga till den */ if ((pieceToCheck.isWhite() != this.isWhite()) - && ((allowedToRecurse && !isInSchack(pieces, pos)) || !allowedToRecurse)) { + && ((allowedToRecurse && !isInSchack(pieces, pos)) || !allowedToRecurse)) movable.add(pos); - } return true; } @@ -191,21 +188,18 @@ public abstract class Piece { List enemyAttacks = new ArrayList<>(); // Fråga alla pjäser vart de kan gå/ta - for (Piece[] pieceArr : pieces) { - for (Piece piece : pieceArr) { - if (piece != null && piece.isWhite != this.isWhite()) { + for (Piece[] pieceArr : pieces) + for (Piece piece : pieceArr) + if (piece != null && piece.isWhite != this.isWhite()) // Lägg till alla attacker för mostståndaren enemyAttacks.addAll(piece.validAttacks(pieces, false)); - } - } - } // Kollar ifall kungen står i schack just nu for (Point enemyAttack : enemyAttacks) { Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y]; - if (attackedPiece != null && attackedPiece.supremeRuler) { + if (attackedPiece != null && attackedPiece.supremeRuler) return true; - } + } return false; }