shcakc no werk men man man får inte gå in i schackl

This commit is contained in:
loveb 2022-04-05 15:58:29 +02:00
parent c3f85f0128
commit 29df86030c
3 changed files with 22 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Board extends JPanel implements MouseListener {
@ -128,7 +129,7 @@ public class Board extends JPanel implements MouseListener {
Piece p = pieces[mouseCoordinateX][mouseCoordinateY];
// Kolla endast ifall vi kan röra pjäsen om det är samma färg som den tur vi är
if (p.isWhite() == turn||developerMode) {
if (p.isWhite() == turn || developerMode) {
LinkedHashSet<Point> blackAttacks = new LinkedHashSet<>();
LinkedHashSet<Point> whiteAttacks = new LinkedHashSet<>();
@ -150,8 +151,24 @@ public class Board extends JPanel implements MouseListener {
}
LinkedHashSet validMoves = p.validMoves(pieces);
// Funkar
if (p.supremeRuler) {
validMoves.removeAll(turn ? blackAttacks : whiteAttacks);
}
// Funkar inte
for (Point attack : turn ? whiteAttacks : blackAttacks) {
Piece attacked = pieces[attack.x][attack.x];
if (attacked == null) {
continue;
}
if (attacked.supremeRuler) {
JOptionPane.showMessageDialog(this, "Du står i schack");
}
}
validMovesToDraw.addAll(validMoves);
validMovesToDraw.addAll(blackAttacks);
}
} catch (Exception e) {

View File

@ -10,6 +10,7 @@ public final class King extends PieceKnownIfMoved {
public King(boolean isWhite, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
setPieceIcon("King");
supremeRuler= true;
}
public boolean isSeen(ArrayList<Piece> pieces) {

View File

@ -12,6 +12,8 @@ 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;
protected BufferedImage icon;
public Piece(boolean white, Point startingPosition) throws IOException {