diff --git a/src/schack/Bishop.java b/src/schack/Bishop.java index 5f60220..e7357aa 100644 --- a/src/schack/Bishop.java +++ b/src/schack/Bishop.java @@ -2,7 +2,7 @@ package schack; import java.awt.Point; import java.io.IOException; -import java.util.ArrayList; +import java.util.List; public class Bishop extends LongWalkers { @@ -11,7 +11,7 @@ public class Bishop extends LongWalkers { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { + public List validMoves(Piece[][] pieces, boolean allowedToRecurse) { return getMoves( new int[][]{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}}, pieces, diff --git a/src/schack/Board.java b/src/schack/Board.java index 0b57165..b98e406 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -9,6 +9,7 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import javax.swing.JOptionPane; import javax.swing.JPanel; @@ -16,7 +17,7 @@ public class Board extends JPanel implements MouseListener { public static final int SIZE_OF_TILE = 100; private Piece[][] pieces = new Piece[8][8]; - private ArrayList validMovesToDraw = new ArrayList<>(); + private List validMovesToDraw = new ArrayList<>(); private Point previouslyClickedPoint = new Point(); private final Color moveableColor = new Color(255, 191, 0); short turnCount = 0; @@ -161,8 +162,8 @@ public class Board extends JPanel implements MouseListener { * @return SCHACK, SCHACKMATT, PATT, NORMAL */ private SchackState getSchackState() { - ArrayList allValidMoves = getMoves(whitesTurn); - ArrayList opposingAttacks = getAttacks(!whitesTurn); + List allValidMoves = getMoves(whitesTurn); + List opposingAttacks = getAttacks(!whitesTurn); boolean weCanMove = !allValidMoves.isEmpty(); boolean inSchack = false; @@ -188,8 +189,8 @@ public class Board extends JPanel implements MouseListener { return SchackState.NORMAL; } - private ArrayList getMoves(boolean whiteMovesAreWanted) { - ArrayList allValidMoves = new ArrayList<>(); + private List getMoves(boolean whiteMovesAreWanted) { + List allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { if (piece == null || whiteMovesAreWanted != piece.isWhite()) { @@ -201,8 +202,8 @@ public class Board extends JPanel implements MouseListener { return allValidMoves; } - public ArrayList getAttacks(boolean whiteAttacksAreWanted) { - ArrayList attacks = new ArrayList(); + public List getAttacks(boolean whiteAttacksAreWanted) { + List attacks = new ArrayList(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { if (piece == null || whiteAttacksAreWanted != piece.isWhite()) { diff --git a/src/schack/Horse.java b/src/schack/Horse.java index ca9996f..ba53c6e 100644 --- a/src/schack/Horse.java +++ b/src/schack/Horse.java @@ -3,6 +3,7 @@ package schack; import java.awt.Point; import java.io.IOException; import java.util.ArrayList; +import java.util.List; public class Horse extends Piece { @@ -11,8 +12,8 @@ public class Horse extends Piece { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { - ArrayList movable = new ArrayList<>(); + public List validMoves(Piece[][] pieces, boolean allowedToRecurse) { + List movable = new ArrayList<>(); for (int dx : new int[]{-2, -1, 1, 2}) { for (int direction : new int[]{-1, 1}) { diff --git a/src/schack/King.java b/src/schack/King.java index 7cdad5d..988fd1d 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -3,6 +3,7 @@ package schack; import java.awt.Point; import java.io.IOException; import java.util.ArrayList; +import java.util.List; public final class King extends Piece { @@ -17,8 +18,8 @@ public final class King extends Piece { * @param pieces * @return */ - private ArrayList getCastlingIfPossible(Piece[][] pieces) { - ArrayList possibleCastling = new ArrayList<>(); + private List getCastlingIfPossible(Piece[][] pieces) { + List possibleCastling = new ArrayList<>(); if (this.isMoved()) { return possibleCastling; } @@ -78,8 +79,8 @@ public final class King extends Piece { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { - ArrayList movable = new ArrayList<>(); + public List validMoves(Piece[][] pieces, boolean allowedToRecurse) { + List movable = new ArrayList<>(); for (int loopX = -1; loopX < 2; loopX++) { for (int loopY = -1; loopY < 2; loopY++) { diff --git a/src/schack/LongWalkers.java b/src/schack/LongWalkers.java index 9ec5824..d025af4 100644 --- a/src/schack/LongWalkers.java +++ b/src/schack/LongWalkers.java @@ -3,6 +3,7 @@ package schack; import java.awt.Point; import java.io.IOException; import java.util.ArrayList; +import java.util.List; public abstract class LongWalkers extends Piece { @@ -21,8 +22,8 @@ public abstract class LongWalkers extends Piece { * @param allowedToRecurse * @return */ - ArrayList getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) { - ArrayList movable = new ArrayList<>(); + List getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) { + List movable = new ArrayList<>(); for (int[] xy : directions) { int loopX = this.position.x, loopY = this.position.y; diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index dea912c..14df50d 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -4,6 +4,7 @@ import java.awt.HeadlessException; import java.awt.Point; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import javax.swing.JOptionPane; public class Pawn extends Piece { @@ -21,8 +22,8 @@ public class Pawn extends Piece { * @return Alla lämpliga attackMoves */ @Override - public ArrayList validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) { - ArrayList movable = new ArrayList<>(); + public List validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) { + List movable = new ArrayList<>(); // Kolla ifall vi kan ta någon for (int pawnX : new int[]{-1, 1}) { @@ -42,8 +43,8 @@ public class Pawn extends Piece { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { - ArrayList movable = new ArrayList<>(); + public List validMoves(Piece[][] pieces, boolean allowedToRecurse) { + List movable = new ArrayList<>(); // Om bonden har gått en gång, får gå 1 steg, annars 2 final int upTo = this.isMoved() ? 1 : 2; @@ -75,8 +76,8 @@ public class Pawn extends Piece { * @param movable * @param pieces */ - private ArrayList addAttackMovesIfCan(Point pos, Piece[][] pieces) { - ArrayList movable = new ArrayList(); + private List addAttackMovesIfCan(Point pos, Piece[][] pieces) { + 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; @@ -92,7 +93,7 @@ public class Pawn extends Piece { } @Override - protected boolean addMovesIfCan(Point pos, ArrayList 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; } diff --git a/src/schack/Piece.java b/src/schack/Piece.java index e988b58..15e853a 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -6,6 +6,7 @@ import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.List; import javax.imageio.ImageIO; public abstract class Piece { @@ -18,7 +19,7 @@ public abstract class Piece { /** * Sant ifall pjäsens färg är vit, falskt ifall den är svart */ - private boolean isWhite; + private final boolean isWhite; /** * SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:) */ @@ -64,7 +65,7 @@ public abstract class Piece { * @param allowedToRecurse * @return */ - public abstract ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse); + public abstract List validMoves(Piece[][] pieces, boolean allowedToRecurse); /** * Ger tillbaks alla ställen pjäsen kan attackera @@ -74,7 +75,7 @@ public abstract class Piece { * kolla ifall det är något i möjliga attackrutor ifall * @return Alla lämpliga attackMoves */ - public ArrayList validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) { + public List validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) { return validMoves(pieces, false); } @@ -121,7 +122,7 @@ public abstract class Piece { * * @return true ifall man inte kan gå längre i denna riktning */ - protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean allowedToRecurse) { + 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) { return false; @@ -186,7 +187,7 @@ public abstract class Piece { * @return true ifall det är schack */ protected boolean isInSchack(Piece[][] pieces) { - ArrayList enemyAttacks = new ArrayList<>(); + List enemyAttacks = new ArrayList<>(); // Fråga alla pjäser vart de kan gå/ta for (Piece[] pieceArr : pieces) { diff --git a/src/schack/Queen.java b/src/schack/Queen.java index 458928a..d4138b7 100644 --- a/src/schack/Queen.java +++ b/src/schack/Queen.java @@ -2,7 +2,7 @@ package schack; import java.awt.Point; import java.io.IOException; -import java.util.ArrayList; +import java.util.List; public class Queen extends LongWalkers { @@ -11,7 +11,7 @@ public class Queen extends LongWalkers { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { + public List 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, diff --git a/src/schack/Rook.java b/src/schack/Rook.java index d4b5f5d..f08503b 100644 --- a/src/schack/Rook.java +++ b/src/schack/Rook.java @@ -2,7 +2,7 @@ package schack; import java.awt.Point; import java.io.IOException; -import java.util.ArrayList; +import java.util.List; public class Rook extends LongWalkers { @@ -11,7 +11,7 @@ public class Rook extends LongWalkers { } @Override - public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { + public List validMoves(Piece[][] pieces, boolean allowedToRecurse) { return getMoves( new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, pieces, diff --git a/src/schack/Schack.java b/src/schack/Schack.java index 34b2d6d..0ef6fb0 100644 --- a/src/schack/Schack.java +++ b/src/schack/Schack.java @@ -1,9 +1,11 @@ package schack; +import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.io.IOException; import java.net.Inet4Address; import java.net.NetworkInterface; +import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; @@ -74,7 +76,7 @@ public class Schack { String ip = Inet4Address.getLocalHost().toString(); JOptionPane.showMessageDialog(null, "IP: " + ip); - } catch (Exception e) { + } catch (HeadlessException | UnknownHostException e) { } }); connectToOpponent.addActionListener((ActionEvent ae) -> {