This commit is contained in:
loveb 2022-03-31 08:37:47 +02:00
parent 6710645543
commit 4f77dc0b33

View File

@ -19,6 +19,7 @@ public class Board extends JPanel implements MouseListener {
private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>();
private Point selectedPiece = new Point();
private Color moveableColor = new Color(255, 191, 0);
private boolean turn = true;
public Board() throws IOException {
@ -108,6 +109,7 @@ public class Board extends JPanel implements MouseListener {
try {
Piece p = pieces[selectedPiece.x][selectedPiece.y];
p.move(pieces, clicked, selectedPiece);
turn = !turn;
} catch (Exception e) {
validMovesToDraw.clear();
@ -118,13 +120,16 @@ public class Board extends JPanel implements MouseListener {
validMovesToDraw.clear();
}
// Om vi inte redan har valt en pjäs klickar vi en pjäs
if (!validMovesToDraw.contains(clicked)) {
try {
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) {
LinkedHashSet validMoves = p.validMoves(pieces);
validMovesToDraw.addAll(validMoves);
}
} catch (Exception e) {
validMovesToDraw.clear();
}