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

View File

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