mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-04 15:10:21 +01:00 
			
		
		
		
	fixed king calculate legal
This commit is contained in:
		@@ -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) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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(
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user