fixed king calculate legal

This commit is contained in:
lov3b 2022-03-02 19:57:45 +01:00
parent 1bad5933b0
commit e79d1222c2
2 changed files with 14 additions and 15 deletions

View File

@ -15,7 +15,7 @@ import javax.swing.JPanel;
public class Board extends JPanel {
public static final int SIZE_OF_TILE = 100;
ArrayList<Piece> pieces = new ArrayList<>();
private ArrayList<Piece> pieces = new ArrayList<>();
public Board() throws IOException {
@ -49,12 +49,14 @@ public class Board extends JPanel {
pieces.forEach(p -> p.draw(g2));
Piece p = pieces.get(1);
LinkedHashSet<Point> legal = p.validMoves(pieces);
g2.setColor(Color.yellow);
legal.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
System.out.println(legal.size());
// Check valid moves method
pieces.forEach(p -> {
LinkedHashSet<Point> validMoves = p.validMoves(pieces);
Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
g2.setColor(c);
validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
System.out.println("x:" + p.position.x + ", y:" + p.position.y + ": " + validMoves.size());
});
}
private void drawSquares(Graphics2D g2) {

View File

@ -21,10 +21,7 @@ public final class King extends Piece {
}
public King(boolean white) throws IOException {
// Point p = new Point();
// p.x = 5;
// p.y = white ? 0 : 7;
super(white, white ? new Point(5, 0) : new Point(5, 7));
super(white, white ? new Point(4, 0) : new Point(4, 7));
}
@Override
@ -57,10 +54,10 @@ public final class King extends Piece {
if (y == 1 && x == 1) {
continue;
} // Ifall det är utanför planen, skippa tror inte det funkar
else if (x + this.position.x > 8
|| x + this.position.x < 0
|| y + this.position.y > 8
|| y + this.position.y < 0) {
else if (x + this.position.x >= 8+1
|| x + this.position.x <= 0
|| y + this.position.y >= 8+1
|| y + this.position.y <= 0) {
continue;
}
perhapsMovable.add(