Schack/src/schack/Piece.java

94 lines
2.5 KiB
Java
Raw Normal View History

2022-03-01 18:28:25 +01:00
package schack;
import java.awt.Component;
import java.awt.Graphics2D;
2022-03-01 18:28:25 +01:00
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet;
import javax.imageio.ImageIO;
2022-03-01 18:28:25 +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;
public boolean isWhite;
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 {
this.isWhite = isWhite;
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;
}
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";
InputStream is = getClass().getResourceAsStream("../img/" + fileName);
icon = ImageIO.read(is);
}
public abstract LinkedHashSet<Point> validMoves(Piece[][] pieces);
public void draw(Graphics2D g2) {
2022-03-02 19:40:53 +01:00
g2.drawImage(
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-08 16:19:36 +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;
}
@Override
public String toString() {
return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}';
}
2022-03-01 18:28:25 +01:00
}