From c5d91e9aeb8f4928ce39c23c187c3c917317421d Mon Sep 17 00:00:00 2001 From: lov3b Date: Sun, 15 May 2022 19:08:47 +0200 Subject: [PATCH] finals --- src/schack/Board.java | 22 +++++++++++----------- src/schack/Horse.java | 8 ++++---- src/schack/King.java | 8 +++----- src/schack/LongWalkers.java | 4 ++-- src/schack/Pawn.java | 8 ++++---- src/schack/Piece.java | 35 +++++++++++++++++------------------ src/schack/Schack.java | 20 ++++++++++---------- 7 files changed, 51 insertions(+), 54 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index e8552a3..f2d7a69 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -103,22 +103,22 @@ public class Board extends JPanel implements MouseListener { @Override public void mousePressed(MouseEvent mouseEvent) { - int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE); - int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE); - Point clicked = new Point(mouseCoordinateX, mouseCoordinateY); + final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE); + final int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE); + final Point clickedCoordinate = new Point(mouseCoordinateX, mouseCoordinateY); // Ifall vi har tryckt på en pjäs och sedan ska gå dit - if (validMovesToDraw.contains(clicked)) { + if (validMovesToDraw.contains(clickedCoordinate)) { final Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y]; if (selectedPiece == null) { validMovesToDraw.clear(); return; } - selectedPiece.move(pieces, clicked); + selectedPiece.move(pieces, clickedCoordinate); turnCount++; whitesTurn = !whitesTurn; - ArrayList allValidMoves = new ArrayList<>(); + final ArrayList allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { if (piece == null || whitesTurn != piece.isWhite()) { @@ -132,7 +132,7 @@ public class Board extends JPanel implements MouseListener { ArrayList opposingAttacks = checkAttacks(!whitesTurn); - boolean weCanMove = !allValidMoves.isEmpty(); + final boolean weCanMove = !allValidMoves.isEmpty(); boolean inSchack = false; for (Point attack : opposingAttacks) { @@ -147,7 +147,7 @@ public class Board extends JPanel implements MouseListener { if (weCanMove) { JOptionPane.showMessageDialog(this, "Du står i schack"); } else { - int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?"); + final int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?"); if (choise == JOptionPane.YES_OPTION) { try { restartGame(); @@ -164,12 +164,12 @@ public class Board extends JPanel implements MouseListener { } } else { - previouslyClickedPoint = new Point(clicked); + previouslyClickedPoint = new Point(clickedCoordinate); validMovesToDraw.clear(); } // Om vi inte redan har valt en pjäs klickar vi på en pjäs - if (!validMovesToDraw.contains(clicked)) { + if (!validMovesToDraw.contains(clickedCoordinate)) { final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; @@ -186,7 +186,7 @@ public class Board extends JPanel implements MouseListener { } public ArrayList checkAttacks(boolean preferedColor) { - ArrayList attacks = new ArrayList(); + final ArrayList attacks = new ArrayList(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { // Ifall det är tomrum skippa diff --git a/src/schack/Horse.java b/src/schack/Horse.java index 81a120d..d7c5113 100644 --- a/src/schack/Horse.java +++ b/src/schack/Horse.java @@ -12,13 +12,13 @@ public class Horse extends Piece { @Override public ArrayList validMoves(Piece[][] pieces, boolean allowedToRecurse) { - ArrayList movable = new ArrayList<>(); + final ArrayList movable = new ArrayList<>(); for (int dx : new int[]{-2, -1, 1, 2}) { for (int direction : new int[]{-1, 1}) { - int stepLength = (3 - Math.abs(dx)); - int dy = direction * stepLength; - Point potentialMove = new Point(this.position.x + dx, this.position.y + dy); + final int stepLength = (3 - Math.abs(dx)), + dy = direction * stepLength; + final Point potentialMove = new Point(this.position.x + dx, this.position.y + dy); addMovesIfCan(potentialMove, movable, pieces, allowedToRecurse); } } diff --git a/src/schack/King.java b/src/schack/King.java index f42d40a..3baf0a3 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -3,8 +3,6 @@ package schack; import java.awt.Point; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; -import java.util.stream.Stream; public final class King extends PieceKnownIfMoved { @@ -20,7 +18,7 @@ public final class King extends PieceKnownIfMoved { * @return */ private ArrayList getCastlingIfPossible(Piece[][] pieces) { - ArrayList possibleCastling = new ArrayList<>(); + final ArrayList possibleCastling = new ArrayList<>(); if (this.isMoved()) { return possibleCastling; } @@ -85,8 +83,8 @@ 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; + final Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y]; + final Piece king = this; // Null där de stod pieces[king.position.x][king.position.y] = null; diff --git a/src/schack/LongWalkers.java b/src/schack/LongWalkers.java index 2142731..9f37be5 100644 --- a/src/schack/LongWalkers.java +++ b/src/schack/LongWalkers.java @@ -19,14 +19,14 @@ public abstract class LongWalkers extends PieceKnownIfMoved { * @return */ ArrayList getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) { - ArrayList movable = new ArrayList<>(); + final ArrayList movable = new ArrayList<>(); for (int[] xy : directions) { int loopX = this.position.x, loopY = this.position.y; 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, allowedToRecurse); + final 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 65daf4d..a3beb43 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -48,7 +48,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)); + final Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY)); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse); if (shouldBreak) { break; @@ -58,7 +58,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)); + final Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1)); movable.addAll(addAttackMovesIfCan(pos, pieces)); } return movable; @@ -80,9 +80,9 @@ public class Pawn extends PieceKnownIfMoved { return movable; } - Piece piece = pieces[pos.x][pos.y]; + final Piece potentialEnemy = pieces[pos.x][pos.y]; // Ifall det är tomt här, gör ingenting - if (piece != null && piece.isWhite() != this.isWhite()) { + if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) { if (!isInSchack(pieces, pos)) { movable.add(pos); } diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 8c72af1..d7dba03 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; } @@ -45,9 +45,9 @@ public abstract class Piece { * @throws IOException ifall det inte finns någon bild på pjäsen */ private void setPieceIcon() throws IOException { - String className = this.getClass().getSimpleName(); - String colorName = this.isWhite() ? "White" : "Black"; - String fileName = colorName + className + ".png"; + final String className = this.getClass().getSimpleName(); + final String colorName = this.isWhite() ? "White" : "Black"; + final String fileName = colorName + className + ".png"; InputStream is = getClass().getResourceAsStream("/img/" + fileName); icon = ImageIO.read(is); } @@ -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); @@ -118,8 +118,8 @@ public abstract class Piece { if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) { return false; } - - Piece pieceToCheck = pieces[pos.x][pos.y]; + + final Piece pieceToCheck = pieces[pos.x][pos.y]; // Detta är en tom plats if (pieceToCheck == null) { @@ -139,7 +139,7 @@ public abstract class Piece { movable.add(pos); } return true; - + } /** @@ -151,23 +151,23 @@ public abstract class Piece { */ protected boolean isInSchack(Piece[][] pieces, Point pos) { // Kom ihåg vart vi var - Point previousPosition = new Point(this.position); + final Point previousPosition = new Point(this.position); // Kom ihåg motståndarpjäs - Piece guyThatsAlreadyHere = pieces[pos.x][pos.y]; + final Piece guyThatsAlreadyHere = pieces[pos.x][pos.y]; // Testa att flytta pieces[pos.x][pos.y] = this; pieces[previousPosition.x][previousPosition.y] = null; this.position = pos; - - boolean inSchack = isInSchack(pieces); + + final boolean inSchack = isInSchack(pieces); // Flytta tillbaka pieces[previousPosition.x][previousPosition.y] = this; pieces[pos.x][pos.y] = guyThatsAlreadyHere; this.position = previousPosition; - + return inSchack; } @@ -192,18 +192,17 @@ public abstract class Piece { // Kollar ifall kungen står i schack just nu for (Point enemyAttack : enemyAttacks) { - Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y]; + final Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y]; if (attackedPiece != null && attackedPiece.supremeRuler) { return true; } } return false; } - + @Override public String toString() { return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}'; -// return "Piece{" + "position=" + position + ", isWhite=" + white + '}'; } /** @@ -222,5 +221,5 @@ public abstract class Piece { public boolean isMoved() { return false; } - + } diff --git a/src/schack/Schack.java b/src/schack/Schack.java index 673eda1..b2f980b 100644 --- a/src/schack/Schack.java +++ b/src/schack/Schack.java @@ -20,7 +20,7 @@ import javax.swing.UIManager; */ public class Schack { - JFrame frame; + final JFrame frame; public Schack() throws IOException { // Set theme @@ -42,18 +42,18 @@ public class Schack { frame.setResizable(false); // Might throw an IOException if the icon of the Pieces isn't embedded correctly - Board board = new Board(); + final Board board = new Board(); frame.setContentPane(board); frame.getContentPane().addMouseListener(board); // Create menu - JMenuBar menuBar = new JMenuBar(); - JMenu gameMenu = new JMenu("Game"); - JMenu connectMenu = new JMenu("Connect"); - JMenuItem connectToOpponent = new JMenuItem("Connect to opponent"); - JMenuItem showLocalIP = new JMenuItem("Show IP"); - JMenuItem askForRemi = new JMenuItem("Ask for remi"); - JMenuItem surrender = new JMenuItem("Surrender"); + final JMenuBar menuBar = new JMenuBar(); + final JMenu gameMenu = new JMenu("Game"); + final JMenu connectMenu = new JMenu("Connect"); + final JMenuItem connectToOpponent = new JMenuItem("Connect to opponent"); + final JMenuItem showLocalIP = new JMenuItem("Show IP"); + final JMenuItem askForRemi = new JMenuItem("Ask for remi"); + final JMenuItem surrender = new JMenuItem("Surrender"); // Actions connectToOpponent.addActionListener((ActionEvent ae) -> { @@ -83,7 +83,7 @@ public class Schack { }); surrender.addActionListener((ActionEvent ae) -> { String whosGivingUp = board.isWhitesTurn() ? "Vit" : "Svart"; - int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?"); + final int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?"); if (choice == JOptionPane.YES_OPTION) { try { board.restartGame();