2022-03-01 18:28:25 +01:00
|
|
|
package schack;
|
|
|
|
|
2022-03-01 23:00:38 +01:00
|
|
|
import java.awt.Graphics2D;
|
|
|
|
import java.awt.Point;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
2022-03-01 18:39:24 +01:00
|
|
|
public final class King extends Piece {
|
2022-03-01 18:28:25 +01:00
|
|
|
|
2022-03-01 23:00:38 +01:00
|
|
|
public King(boolean white, Point startingPosition) throws IOException {
|
|
|
|
super(white, startingPosition);
|
|
|
|
String colorName = white ? "White" : "Black";
|
|
|
|
String fileName = "resized" + colorName + "King.png";
|
|
|
|
String path = Paths.get("icons", fileName).toString();
|
|
|
|
System.out.println(path);
|
|
|
|
icon = ImageIO.read(new File(fileName));
|
|
|
|
}
|
|
|
|
|
2022-03-02 19:40:53 +01:00
|
|
|
public King(boolean white) throws IOException {
|
2022-03-02 19:57:45 +01:00
|
|
|
super(white, white ? new Point(4, 0) : new Point(4, 7));
|
2022-03-02 19:40:53 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 23:00:38 +01:00
|
|
|
@Override
|
|
|
|
public void draw(Graphics2D g2) {
|
|
|
|
super.draw(g2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSeen(ArrayList<Piece> pieces) {
|
2022-03-01 18:39:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
2022-03-01 23:00:38 +01:00
|
|
|
|
|
|
|
@Override
|
2022-03-02 19:33:08 +01:00
|
|
|
public LinkedHashSet<Point> validMoves(ArrayList<Piece> pieces) {
|
2022-03-01 23:00:38 +01:00
|
|
|
LinkedHashSet<Point> unmovable = new LinkedHashSet<>();
|
|
|
|
LinkedHashSet<Point> perhapsMovable = new LinkedHashSet<>();
|
|
|
|
for (Piece piece : pieces) {
|
|
|
|
Point p = piece.position;
|
|
|
|
|
|
|
|
// Ifall en pjäs står runt omkring kungen går det inte att flytta dit
|
|
|
|
if (Math.abs(p.x - this.position.x) == 1
|
|
|
|
&& Math.abs(p.y - this.position.y) == 1) {
|
|
|
|
unmovable.add(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lägg till tiles kring kungen
|
|
|
|
for (int x = 0; x < 3; x++) {
|
|
|
|
for (int y = 0; y < 3; y++) {
|
|
|
|
if (y == 1 && x == 1) {
|
|
|
|
continue;
|
|
|
|
} // Ifall det är utanför planen, skippa tror inte det funkar
|
2022-03-02 19:57:45 +01:00
|
|
|
else if (x + this.position.x >= 8+1
|
|
|
|
|| x + this.position.x <= 0
|
|
|
|
|| y + this.position.y >= 8+1
|
|
|
|
|| y + this.position.y <= 0) {
|
2022-03-01 23:00:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
perhapsMovable.add(
|
|
|
|
new Point(this.position.x - 1 + x, this.position.y - 1 + y)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
perhapsMovable.removeAll(unmovable);
|
|
|
|
return perhapsMovable;
|
|
|
|
}
|
2022-03-01 18:28:25 +01:00
|
|
|
}
|