48 lines
1.2 KiB
Java
48 lines
1.2 KiB
Java
import java.awt.*;
|
|
import java.util.*;
|
|
|
|
public abstract class Piece {
|
|
Position pos;
|
|
Image sprite;
|
|
String color;
|
|
|
|
public Piece(int x, int y, String color, Image sprite) {
|
|
this.pos = new Position(x, y);
|
|
this.color = color;
|
|
this.sprite = sprite;
|
|
}
|
|
|
|
public void draw(Graphics g) {
|
|
g.drawImage(sprite, pos.x * 40, pos.y * 40, null);
|
|
}
|
|
|
|
public abstract ArrayList<Position> getLegalMoves(Board board);
|
|
|
|
public abstract ArrayList<Position> getPseudoLegalMoves(Board board);
|
|
|
|
public abstract Piece copy();
|
|
|
|
public boolean colorMatches(Piece p) {
|
|
return this.color.equals(p.color);
|
|
}
|
|
|
|
public ArrayList<Position> slide(Board board, int dx, int dy) {
|
|
ArrayList<Position> positions = new ArrayList<>();
|
|
int step = 1;
|
|
while (true) {
|
|
Position test = new Position(pos.x + step * dx, pos.y + step * dy);
|
|
if (!Board.inBounds(test)) break;
|
|
if (board.isOpen(test)) {
|
|
positions.add(test);
|
|
} else if (board.getPiece(test).colorMatches(this)) {
|
|
break;
|
|
} else {
|
|
positions.add(test);
|
|
break;
|
|
}
|
|
step++;
|
|
}
|
|
return positions;
|
|
}
|
|
}
|