Nu funkar checken ifall man står i schak lite mer

This commit is contained in:
loveb 2022-04-21 09:13:06 +02:00
parent f4a2d3a37d
commit 175edea21c
9 changed files with 104 additions and 43 deletions

View File

@ -12,12 +12,12 @@ public class Bishop extends Piece {
} }
@Override @Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
LinkedHashSet<Point> movable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
// Upp vänster // Upp vänster
for (int loopX = this.position.x - 1, loopY = this.position.y - 1; loopX >= 0 && loopY >= 0; loopX--, loopY--) { 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) { if (shouldBreak) {
break; break;
} }
@ -26,7 +26,7 @@ public class Bishop extends Piece {
// Upp höger // Upp höger
for (int loopX = this.position.x + 1, loopY = this.position.y - 1; loopX <= 7 && loopY >= 0; loopX++, loopY--) { 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) { if (shouldBreak) {
break; break;
} }
@ -34,7 +34,7 @@ public class Bishop extends Piece {
} }
// Ner höger // Ner höger
for (int loopX = this.position.x + 1, loopY = this.position.y + 1; loopX <= 7 && loopY <= 7; loopX++, loopY++) { 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) { if (shouldBreak) {
break; break;
} }
@ -42,7 +42,7 @@ public class Bishop extends Piece {
} }
// Ner vänster // Ner vänster
for (int loopX = this.position.x - 1, loopY = this.position.y + 1; loopX >= 0 && loopY <= 7; loopX--, loopY++) { 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) { if (shouldBreak) {
break; break;
} }

View File

@ -20,7 +20,7 @@ public class Board extends JPanel implements MouseListener {
private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>(); private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>();
private Point selectedPiece = new Point(); private Point selectedPiece = new Point();
private Color moveableColor = new Color(255, 191, 0); private Color moveableColor = new Color(255, 191, 0);
private boolean turn = true; public static boolean turn = true;
public boolean developerMode = false; public boolean developerMode = false;
public Board() throws IOException { public Board() throws IOException {
@ -126,10 +126,10 @@ public class Board extends JPanel implements MouseListener {
if (!validMovesToDraw.contains(clicked)) { if (!validMovesToDraw.contains(clicked)) {
try { try {
Piece p = pieces[mouseCoordinateX][mouseCoordinateY]; Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
// Kolla endast ifall vi kan röra pjäsen om det är samma färg som den tur vi är // 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 (selectedPiece.isWhite() == turn || developerMode) {
LinkedHashSet<Point> blackAttacks = new LinkedHashSet<>(); LinkedHashSet<Point> blackAttacks = new LinkedHashSet<>();
LinkedHashSet<Point> whiteAttacks = new LinkedHashSet<>(); LinkedHashSet<Point> whiteAttacks = new LinkedHashSet<>();
@ -150,12 +150,13 @@ public class Board extends JPanel implements MouseListener {
} }
} }
LinkedHashSet validMoves = p.validMoves(pieces); LinkedHashSet<Point> validMoves = selectedPiece.validMoves(pieces, true);
// Funkar // Funkar
if (p.supremeRuler) { if (selectedPiece.supremeRuler) {
validMoves.removeAll(turn ? blackAttacks : whiteAttacks); validMoves.removeAll(turn ? blackAttacks : whiteAttacks);
} }
// Kollar ifall kungen står i schack just nu // 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]; Piece attacked = pieces[attack.x][attack.y];

View File

@ -12,7 +12,7 @@ public class Horse extends Piece {
} }
@Override @Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
LinkedHashSet<Point> movable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
// Postitioner att checka // Postitioner att checka
@ -37,7 +37,7 @@ public class Horse extends Piece {
for (Point pos : positions) { for (Point pos : positions) {
// Ifall en är blockerad ska vi inte sluta kolla // Ifall en är blockerad ska vi inte sluta kolla
addMovesIfCan(pos, movable, pieces); addMovesIfCan(pos, movable, pieces, isSelected);
} }
return movable; return movable;

View File

@ -96,7 +96,7 @@ public final class King extends PieceKnownIfMoved {
} }
@Override @Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
LinkedHashSet<Point> movable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
for (int loopX = -1; loopX < 2; loopX++) { for (int loopX = -1; loopX < 2; loopX++) {
@ -104,7 +104,7 @@ public final class King extends PieceKnownIfMoved {
if (loopY == 0 && loopX == 0) { if (loopY == 0 && loopX == 0) {
continue; 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);
} }
} }

View File

@ -32,7 +32,7 @@ public class Pawn extends PieceKnownIfMoved {
} }
@Override @Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
// TODO: Lösa bugg där bunder kanterna inte kan röra sig // TODO: Lösa bugg där bunder kanterna inte kan röra sig
LinkedHashSet<Point> movable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
@ -44,7 +44,7 @@ public class Pawn extends PieceKnownIfMoved {
System.out.println("this.position.x: " + this.position.x); System.out.println("this.position.x: " + this.position.x);
System.out.println("calced y: " + (this.position.y + (this.white ? -pawnDY : pawnDY))); 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)); 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) { if (shouldBreak) {
System.out.println("should brkje!"); System.out.println("should brkje!");
break; break;
@ -75,7 +75,7 @@ public class Pawn extends PieceKnownIfMoved {
} }
@Override @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) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
return false; return false;
} }

View File

@ -7,12 +7,16 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import static schack.Board.turn;
public abstract class Piece { public abstract class Piece {
public Point position; public Point position;
public boolean white; public boolean white;
/** SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)*/ /**
* SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
*/
public boolean supremeRuler = false; public boolean supremeRuler = false;
protected BufferedImage icon; protected BufferedImage icon;
@ -36,10 +40,10 @@ public abstract class Piece {
icon = ImageIO.read(is); icon = ImageIO.read(is);
} }
public abstract LinkedHashSet<Point> validMoves(Piece[][] pieces); public abstract LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected);
public LinkedHashSet<Point> validAttacks(Piece[][] pieces) { public LinkedHashSet<Point> validAttacks(Piece[][] pieces) {
return validMoves(pieces); return validMoves(pieces, false);
} }
public void draw(Graphics2D g2) { 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 // Instead of checking index and null, try-catch
try { try {
// Ifall vi kollar utanför brädet kommer detta att faila // Ifall vi kollar utanför brädet kommer detta att faila
@ -82,8 +86,32 @@ public abstract class Piece {
return true; return true;
} }
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
// This is an empty spot // Detta är en tom plats, vi ska inte breaka
if (!isSelected) {
movable.add(pos); 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) { } catch (IndexOutOfBoundsException ioobe) {
// This means that the player is at the edge // This means that the player is at the edge
} catch (Exception e) { } 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<Point> attacks = new LinkedHashSet<>();
// Fråga alla pjäser vart de kan /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 @Override
public String toString() { public String toString() {
return "Piece{" + "position=" + position + ", isWhite=" + white + '}'; return "Piece{" + "position=" + position + ", isWhite=" + white + '}';

View File

@ -12,12 +12,12 @@ public class Queen extends Piece {
} }
@Override @Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
LinkedHashSet<Point> movable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
// Vänster // Vänster
for (int loopX = this.position.x - 1; loopX >= 0; loopX--) { 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) { if (shouldBreak) {
break; break;
@ -26,7 +26,7 @@ public class Queen extends Piece {
// Höger // Höger
for (int loopX = this.position.x + 1; loopX <= 7; loopX++) { 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) { if (shouldBreak) {
break; break;
} }
@ -34,7 +34,7 @@ public class Queen extends Piece {
// Ner // Ner
for (int loopY = this.position.y + 1; loopY <= 7; loopY++) { 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) { if (shouldBreak) {
break; break;
} }
@ -42,14 +42,14 @@ public class Queen extends Piece {
// Upp // Upp
for (int loopY = this.position.y - 1; loopY >= 0; loopY--) { 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) { if (shouldBreak) {
break; break;
} }
} }
// Upp vänster // Upp vänster
for (int loopX = this.position.x - 1, loopY = this.position.y - 1; loopX >= 0 && loopY >= 0; loopX--, loopY--) { 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) { if (shouldBreak) {
break; break;
} }
@ -58,7 +58,7 @@ public class Queen extends Piece {
// Upp höger // Upp höger
for (int loopX = this.position.x + 1, loopY = this.position.y - 1; loopX <= 7 && loopY >= 0; loopX++, loopY--) { 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) { if (shouldBreak) {
break; break;
} }
@ -66,7 +66,7 @@ public class Queen extends Piece {
} }
// Ner höger // Ner höger
for (int loopX = this.position.x + 1, loopY = this.position.y + 1; loopX <= 7 && loopY <= 7; loopX++, loopY++) { 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) { if (shouldBreak) {
break; break;
} }
@ -74,7 +74,7 @@ public class Queen extends Piece {
} }
// Ner vänster // Ner vänster
for (int loopX = this.position.x - 1, loopY = this.position.y + 1; loopX >= 0 && loopY <= 7; loopX--, loopY++) { 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) { if (shouldBreak) {
break; break;
} }

View File

@ -12,7 +12,7 @@ public class Rook extends PieceKnownIfMoved {
} }
@Override @Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
LinkedHashSet<Point> movable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
//Behöver skriva att om rookX = this.position.x ska vi istället loopa igenom //Behöver skriva att om rookX = this.position.x ska vi istället loopa igenom
//int rookY = 0-this.position.y; rookY < 8-this.position.Y; rookY++ //int rookY = 0-this.position.y; rookY < 8-this.position.Y; rookY++
@ -20,7 +20,7 @@ public class Rook extends PieceKnownIfMoved {
// Vänster // Vänster
for (int loopX = this.position.x - 1; loopX >= 0; loopX--) { 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) { if (shouldBreak) {
break; break;
} }
@ -28,7 +28,7 @@ public class Rook extends PieceKnownIfMoved {
// Höger // Höger
for (int loopX = this.position.x + 1; loopX <= 7; loopX++) { 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) { if (shouldBreak) {
break; break;
} }
@ -36,7 +36,7 @@ public class Rook extends PieceKnownIfMoved {
// Ner // Ner
for (int loopY = this.position.y + 1; loopY <= 7; loopY++) { 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) { if (shouldBreak) {
break; break;
} }
@ -44,7 +44,7 @@ public class Rook extends PieceKnownIfMoved {
// Upp // Upp
for (int loopY = this.position.y - 1; loopY >= 0; loopY--) { 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) { if (shouldBreak) {
break; break;
} }

View File

@ -1,6 +1,5 @@
package schack; package schack;
import com.formdev.flatlaf.FlatLightLaf;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
@ -24,8 +23,8 @@ public class Schack {
// Set theme // Set theme
try { try {
// FlatSolarizedLightIJTheme.setup(); // FlatSolarizedLightIJTheme.setup();
FlatLightLaf.setup(); //FlatLightLaf.setup();
embedMenuBarIfSupported(); //embedMenuBarIfSupported();
} catch (Exception cantThemeWithFlatLaf) { } catch (Exception cantThemeWithFlatLaf) {
try { try {