2022-03-01 18:28:25 +01:00
|
|
|
package schack;
|
|
|
|
|
2022-03-01 23:00:38 +01:00
|
|
|
import java.awt.Point;
|
|
|
|
import java.io.IOException;
|
2022-03-05 20:20:41 +01:00
|
|
|
import java.io.InputStream;
|
2022-03-01 23:00:38 +01:00
|
|
|
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-05 20:20:41 +01:00
|
|
|
boolean eglibleForCastling = true;
|
2022-03-01 23:00:38 +01:00
|
|
|
|
2022-03-05 20:20:41 +01:00
|
|
|
public King(boolean isWhite, Point startingPosition) throws IOException {
|
|
|
|
super(isWhite, startingPosition);
|
|
|
|
String colorName = isWhite ? "White" : "Black";
|
|
|
|
String fileName = colorName + "King.png";
|
|
|
|
InputStream is = King.class.getResourceAsStream("../img/" + fileName);
|
|
|
|
icon = ImageIO.read(is);
|
2022-03-02 19:40:53 +01:00
|
|
|
}
|
|
|
|
|
2022-03-05 20:20:41 +01:00
|
|
|
public King(boolean isWhite) throws IOException {
|
|
|
|
super(isWhite, isWhite ? new Point(4, 7) : new Point(4, 0));
|
|
|
|
setPieceIcon("King");
|
2022-03-01 23:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05 20:20:41 +01:00
|
|
|
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
|
|
|
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
2022-03-01 23:00:38 +01:00
|
|
|
|
2022-03-05 20:20:41 +01:00
|
|
|
for (int loopX = -1; loopX < 2; loopX++) {
|
|
|
|
for (int loopY = -1; loopY < 2; loopY++) {
|
|
|
|
if (loopY == 0 && loopX == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Point pos = new Point(this.position.x + loopX, this.position.y + loopY);
|
2022-03-01 23:00:38 +01:00
|
|
|
|
2022-03-05 20:20:41 +01:00
|
|
|
// Instead of checking index and null, try-catch
|
|
|
|
try {
|
|
|
|
Piece p = pieces[pos.x][pos.y];
|
|
|
|
System.out.println(p);
|
|
|
|
// If this piece is the same team as ours, skip
|
|
|
|
if (p.isWhite == this.isWhite) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
movable.add(pos);
|
2022-03-01 23:00:38 +01:00
|
|
|
|
2022-03-05 20:20:41 +01:00
|
|
|
} catch (NullPointerException npe) {
|
|
|
|
// This is an empty spot
|
|
|
|
movable.add(pos);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// This means that the player is at the edge
|
2022-03-01 23:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-05 20:20:41 +01:00
|
|
|
|
2022-03-01 23:00:38 +01:00
|
|
|
}
|
2022-03-05 20:20:41 +01:00
|
|
|
System.out.println("Len of movable: " + movable.size());
|
|
|
|
return movable;
|
|
|
|
|
|
|
|
}
|
2022-03-01 23:00:38 +01:00
|
|
|
|
2022-03-05 20:20:41 +01:00
|
|
|
@Override
|
|
|
|
public void move(Piece[][] pieces) {
|
|
|
|
eglibleForCastling = false;
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
2022-03-01 23:00:38 +01:00
|
|
|
}
|
2022-03-05 20:20:41 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Piece{" + "eglibleForCastling=" + eglibleForCastling + "position=" + position + ", isWhite=" + isWhite + '}';
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:28:25 +01:00
|
|
|
}
|