diff --git a/src/schack/Bishop.java b/src/schack/Bishop.java index 225f25b..38e6561 100644 --- a/src/schack/Bishop.java +++ b/src/schack/Bishop.java @@ -12,12 +12,12 @@ public class Bishop extends Piece { } @Override - public LinkedHashSet validMoves(Piece[][] pieces) { + public LinkedHashSet validMoves(Piece[][] pieces, boolean isSelected) { LinkedHashSet movable = new LinkedHashSet<>(); // Upp vänster for (int loopX = this.position.x - 1, loopY = this.position.y - 1; loopX >= 0 && loopY >= 0; loopX--, loopY--) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -26,7 +26,7 @@ public class Bishop extends Piece { // Upp höger for (int loopX = this.position.x + 1, loopY = this.position.y - 1; loopX <= 7 && loopY >= 0; loopX++, loopY--) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -34,7 +34,7 @@ public class Bishop extends Piece { } // Ner höger for (int loopX = this.position.x + 1, loopY = this.position.y + 1; loopX <= 7 && loopY <= 7; loopX++, loopY++) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -42,7 +42,7 @@ public class Bishop extends Piece { } // Ner vänster for (int loopX = this.position.x - 1, loopY = this.position.y + 1; loopX >= 0 && loopY <= 7; loopX--, loopY++) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } diff --git a/src/schack/Board.java b/src/schack/Board.java index 9abac4f..dcfcd3f 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -20,7 +20,7 @@ public class Board extends JPanel implements MouseListener { private LinkedHashSet validMovesToDraw = new LinkedHashSet<>(); private Point selectedPiece = new Point(); private Color moveableColor = new Color(255, 191, 0); - private boolean turn = true; + public static boolean turn = true; public boolean developerMode = false; public Board() throws IOException { @@ -126,10 +126,10 @@ public class Board extends JPanel implements MouseListener { if (!validMovesToDraw.contains(clicked)) { try { - Piece p = pieces[mouseCoordinateX][mouseCoordinateY]; + 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 (p.isWhite() == turn || developerMode) { + if (selectedPiece.isWhite() == turn || developerMode) { LinkedHashSet blackAttacks = new LinkedHashSet<>(); LinkedHashSet whiteAttacks = new LinkedHashSet<>(); @@ -150,14 +150,15 @@ public class Board extends JPanel implements MouseListener { } } - LinkedHashSet validMoves = p.validMoves(pieces); + LinkedHashSet validMoves = selectedPiece.validMoves(pieces, true); // Funkar - if (p.supremeRuler) { + if (selectedPiece.supremeRuler) { validMoves.removeAll(turn ? blackAttacks : whiteAttacks); } + // Kollar ifall kungen står i schack just nu - for (Point attack : turn ? blackAttacks : whiteAttacks) { + for (Point attack : turn ? blackAttacks : whiteAttacks) { Piece attacked = pieces[attack.x][attack.y]; if (attacked == null) { continue; diff --git a/src/schack/Horse.java b/src/schack/Horse.java index 77cef4a..580b919 100644 --- a/src/schack/Horse.java +++ b/src/schack/Horse.java @@ -12,7 +12,7 @@ public class Horse extends Piece { } @Override - public LinkedHashSet validMoves(Piece[][] pieces) { + public LinkedHashSet validMoves(Piece[][] pieces, boolean isSelected) { LinkedHashSet movable = new LinkedHashSet<>(); // Postitioner att checka @@ -37,7 +37,7 @@ public class Horse extends Piece { for (Point pos : positions) { // Ifall en är blockerad så ska vi inte sluta kolla - addMovesIfCan(pos, movable, pieces); + addMovesIfCan(pos, movable, pieces, isSelected); } return movable; diff --git a/src/schack/King.java b/src/schack/King.java index 3c814f0..d22d3e2 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -96,7 +96,7 @@ public final class King extends PieceKnownIfMoved { } @Override - public LinkedHashSet validMoves(Piece[][] pieces) { + public LinkedHashSet validMoves(Piece[][] pieces, boolean isSelected) { LinkedHashSet movable = new LinkedHashSet<>(); for (int loopX = -1; loopX < 2; loopX++) { @@ -104,7 +104,7 @@ 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); + addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces, isSelected); } } diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 0520509..c1705b8 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -32,7 +32,7 @@ public class Pawn extends PieceKnownIfMoved { } @Override - public LinkedHashSet validMoves(Piece[][] pieces) { + public LinkedHashSet validMoves(Piece[][] pieces, boolean isSelected) { // TODO: Lösa bugg där bunder på kanterna inte kan röra sig LinkedHashSet movable = new LinkedHashSet<>(); @@ -44,7 +44,7 @@ public class Pawn extends PieceKnownIfMoved { System.out.println("this.position.x: " + this.position.x); System.out.println("calced y: " + (this.position.y + (this.white ? -pawnDY : pawnDY))); Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY)); - boolean shouldBreak = addMovesIfCan(pos, movable, pieces); + boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); if (shouldBreak) { System.out.println("should brkje!"); break; @@ -75,7 +75,7 @@ public class Pawn extends PieceKnownIfMoved { } @Override - protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) { + protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces, boolean isSelected) { 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 998ff84..4242e6a 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -7,13 +7,17 @@ import java.io.IOException; import java.io.InputStream; import java.util.LinkedHashSet; import javax.imageio.ImageIO; +import javax.swing.JOptionPane; +import static schack.Board.turn; public abstract class Piece { public Point position; public boolean white; - /** SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)*/ - public boolean supremeRuler= false; + /** + * SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:) + */ + public boolean supremeRuler = false; protected BufferedImage icon; public Piece(boolean white, Point startingPosition) throws IOException { @@ -36,10 +40,10 @@ public abstract class Piece { icon = ImageIO.read(is); } - public abstract LinkedHashSet validMoves(Piece[][] pieces); + public abstract LinkedHashSet validMoves(Piece[][] pieces, boolean isSelected); public LinkedHashSet validAttacks(Piece[][] pieces) { - return validMoves(pieces); + return validMoves(pieces, false); } public void draw(Graphics2D g2) { @@ -64,7 +68,7 @@ public abstract class Piece { } } - protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) { + protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces, boolean isSelected) { // Instead of checking index and null, try-catch try { // Ifall vi kollar utanför brädet kommer detta att faila @@ -82,8 +86,32 @@ public abstract class Piece { return true; } } catch (NullPointerException npe) { - // This is an empty spot - movable.add(pos); + // Detta är en tom plats, vi ska inte breaka + if (!isSelected) { + movable.add(pos); + return false; + } + + // Kom ihåg vart vi var + Point previousPosition = new Point(this.position); + + // Testa att flytta + pieces[pos.x][pos.y] = this; + pieces[previousPosition.x][previousPosition.y] = null; + this.position = new Point(pos); + + boolean inSchack = checkIfSchack(pos, pieces); + + // Flytta tillbaka + pieces[previousPosition.x][previousPosition.y] = this; + pieces[pos.x][pos.y] = null; + this.position = new Point(previousPosition); + + if (!inSchack) { + movable.add(pos); + } + return false; + } catch (IndexOutOfBoundsException ioobe) { // This means that the player is at the edge } catch (Exception e) { @@ -93,6 +121,39 @@ public abstract class Piece { } + boolean checkIfSchack(Point pos, Piece[][] pieces) { + boolean ourColor = this.isWhite(); + Piece selectedPiece = this; + LinkedHashSet attacks = new LinkedHashSet<>(); + + // Fråga alla pjäser vart de kan gå/ta + for (Piece[] pieceArr : pieces) { + for (Piece piece : pieceArr) { + // Ifall det är tomrum skippa + if (piece == null) { + continue; + } else if (piece.isWhite() == ourColor) { + continue; + } + + // Lägg till alla attacker för mostståndaren + attacks.addAll(piece.validAttacks(pieces)); + } + } + + // Kollar ifall kungen står i schack just nu + for (Point attack : attacks) { + Piece attacked = pieces[attack.x][attack.y]; + if (attacked == null) { + continue; + } + if (attacked.supremeRuler) { + return true; + } + } + return false; + } + @Override public String toString() { return "Piece{" + "position=" + position + ", isWhite=" + white + '}'; diff --git a/src/schack/Queen.java b/src/schack/Queen.java index c1a617d..00c5fa4 100644 --- a/src/schack/Queen.java +++ b/src/schack/Queen.java @@ -12,13 +12,13 @@ public class Queen extends Piece { } @Override - public LinkedHashSet validMoves(Piece[][] pieces) { + public LinkedHashSet validMoves(Piece[][] pieces, boolean isSelected) { LinkedHashSet movable = new LinkedHashSet<>(); // Vänster for (int loopX = this.position.x - 1; loopX >= 0; loopX--) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces); - + boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected); + if (shouldBreak) { break; } @@ -26,7 +26,7 @@ public class Queen extends Piece { // Höger for (int loopX = this.position.x + 1; loopX <= 7; loopX++) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -34,7 +34,7 @@ public class Queen extends Piece { // Ner for (int loopY = this.position.y + 1; loopY <= 7; loopY++) { - boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -42,14 +42,14 @@ public class Queen extends Piece { // Upp for (int loopY = this.position.y - 1; loopY >= 0; loopY--) { - boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } } // Upp vänster for (int loopX = this.position.x - 1, loopY = this.position.y - 1; loopX >= 0 && loopY >= 0; loopX--, loopY--) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -58,7 +58,7 @@ public class Queen extends Piece { // Upp höger for (int loopX = this.position.x + 1, loopY = this.position.y - 1; loopX <= 7 && loopY >= 0; loopX++, loopY--) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -66,7 +66,7 @@ public class Queen extends Piece { } // Ner höger for (int loopX = this.position.x + 1, loopY = this.position.y + 1; loopX <= 7 && loopY <= 7; loopX++, loopY++) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -74,7 +74,7 @@ public class Queen extends Piece { } // Ner vänster for (int loopX = this.position.x - 1, loopY = this.position.y + 1; loopX >= 0 && loopY <= 7; loopX--, loopY++) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } diff --git a/src/schack/Rook.java b/src/schack/Rook.java index 75455c3..14e6b7e 100644 --- a/src/schack/Rook.java +++ b/src/schack/Rook.java @@ -12,7 +12,7 @@ public class Rook extends PieceKnownIfMoved { } @Override - public LinkedHashSet validMoves(Piece[][] pieces) { + public LinkedHashSet validMoves(Piece[][] pieces, boolean isSelected) { LinkedHashSet movable = new LinkedHashSet<>(); //Behöver skriva att om rookX = this.position.x så ska vi istället loopa igenom //int rookY = 0-this.position.y; rookY < 8-this.position.Y; rookY++ @@ -20,7 +20,7 @@ public class Rook extends PieceKnownIfMoved { // Vänster for (int loopX = this.position.x - 1; loopX >= 0; loopX--) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -28,7 +28,7 @@ public class Rook extends PieceKnownIfMoved { // Höger for (int loopX = this.position.x + 1; loopX <= 7; loopX++) { - boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -36,7 +36,7 @@ public class Rook extends PieceKnownIfMoved { // Ner for (int loopY = this.position.y + 1; loopY <= 7; loopY++) { - boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } @@ -44,7 +44,7 @@ public class Rook extends PieceKnownIfMoved { // Upp for (int loopY = this.position.y - 1; loopY >= 0; loopY--) { - boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces); + boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected); if (shouldBreak) { break; } diff --git a/src/schack/Schack.java b/src/schack/Schack.java index 8d26178..cc4ff53 100644 --- a/src/schack/Schack.java +++ b/src/schack/Schack.java @@ -1,6 +1,5 @@ package schack; -import com.formdev.flatlaf.FlatLightLaf; import java.awt.event.ActionEvent; import java.io.IOException; import java.net.InetAddress; @@ -24,8 +23,8 @@ public class Schack { // Set theme try { // FlatSolarizedLightIJTheme.setup(); - FlatLightLaf.setup(); - embedMenuBarIfSupported(); + //FlatLightLaf.setup(); + //embedMenuBarIfSupported(); } catch (Exception cantThemeWithFlatLaf) { try {