2022-03-01 18:28:25 +01:00
|
|
|
package schack;
|
|
|
|
|
2022-03-01 23:00:38 +01:00
|
|
|
import java.awt.Component;
|
|
|
|
import java.awt.Graphics2D;
|
2022-03-01 18:28:25 +01:00
|
|
|
import java.awt.Point;
|
2022-03-01 23:00:38 +01:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.awt.image.ImageObserver;
|
2022-03-05 20:20:41 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2022-03-01 23:00:38 +01:00
|
|
|
import java.util.LinkedHashSet;
|
2022-03-05 20:20:41 +01:00
|
|
|
import javax.imageio.ImageIO;
|
2022-03-01 18:28:25 +01:00
|
|
|
|
2022-03-01 23:00:38 +01:00
|
|
|
public abstract class Piece extends Component {
|
2022-03-01 18:39:24 +01:00
|
|
|
|
2022-03-01 18:28:25 +01:00
|
|
|
public Point position;
|
2022-03-05 20:20:41 +01:00
|
|
|
public boolean isWhite;
|
2022-03-01 23:00:38 +01:00
|
|
|
protected BufferedImage icon;
|
2022-03-01 18:28:25 +01:00
|
|
|
|
2022-03-08 16:19:36 +01:00
|
|
|
public Piece(boolean isWhite, Point startingPosition) throws IOException {
|
2022-03-05 20:20:41 +01:00
|
|
|
this.isWhite = isWhite;
|
2022-03-01 23:00:38 +01:00
|
|
|
this.position = startingPosition;
|
2022-03-01 18:39:24 +01:00
|
|
|
}
|
|
|
|
|
2022-03-06 11:10:59 +01:00
|
|
|
public Piece(boolean isWhite) {
|
|
|
|
this.isWhite = isWhite;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPosition(Point p) {
|
|
|
|
this.position = p;
|
|
|
|
}
|
|
|
|
|
2022-03-05 20:20:41 +01:00
|
|
|
protected void setPieceIcon(String className) throws IOException {
|
|
|
|
String colorName = isWhite ? "White" : "Black";
|
2022-03-06 11:10:59 +01:00
|
|
|
String fileName = colorName + className + ".png";
|
2022-03-05 20:20:41 +01:00
|
|
|
InputStream is = getClass().getResourceAsStream("../img/" + fileName);
|
|
|
|
icon = ImageIO.read(is);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract LinkedHashSet<Point> validMoves(Piece[][] pieces);
|
2022-03-01 23:00:38 +01:00
|
|
|
|
|
|
|
public void draw(Graphics2D g2) {
|
|
|
|
|
2022-03-02 19:40:53 +01:00
|
|
|
g2.drawImage(
|
2022-03-05 20:20:41 +01:00
|
|
|
icon,
|
|
|
|
position.x * Board.SIZE_OF_TILE,
|
|
|
|
position.y * Board.SIZE_OF_TILE,
|
2022-03-02 19:40:53 +01:00
|
|
|
(ImageObserver) this
|
|
|
|
);
|
2022-03-01 18:28:25 +01:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:19:36 +01:00
|
|
|
public void move(Piece[][] pieces, Point toMove, Point selected) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove));
|
|
|
|
pieces[selected.x][selected.y] = null;
|
|
|
|
this.position = new Point(toMove);
|
|
|
|
Board.printPieces(pieces);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
2022-03-17 09:30:18 +01:00
|
|
|
|
2022-03-08 16:19:36 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-05 20:20:41 +01:00
|
|
|
|
2022-03-17 09:30:18 +01:00
|
|
|
protected boolean checkMove(int x, int y, LinkedHashSet movable, Piece[][] pieces) {
|
|
|
|
Point pos = new Point(x, y);
|
|
|
|
|
|
|
|
// Instead of checking index and null, try-catch
|
|
|
|
try {
|
|
|
|
Piece p = pieces[pos.x][pos.y];
|
|
|
|
// If this piece is the same team as ours, skip
|
|
|
|
if (p.isWhite == this.isWhite) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
movable.add(pos);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} catch (NullPointerException npe) {
|
|
|
|
// This is an empty spot
|
|
|
|
movable.add(pos);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// This means that the player is at the edge
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-05 20:20:41 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}';
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:28:25 +01:00
|
|
|
}
|