From e79d1222c20a2593502728bad21a8e30412aa9c7 Mon Sep 17 00:00:00 2001 From: lov3b Date: Wed, 2 Mar 2022 19:57:45 +0100 Subject: [PATCH] fixed king calculate legal --- src/schack/Board.java | 16 +++++++++------- src/schack/King.java | 13 +++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index b71ba19..b3b1ec5 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -15,7 +15,7 @@ import javax.swing.JPanel; public class Board extends JPanel { public static final int SIZE_OF_TILE = 100; - ArrayList pieces = new ArrayList<>(); + private ArrayList 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 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 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) { diff --git a/src/schack/King.java b/src/schack/King.java index edafa8a..c79bfac 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -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(