mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-03 22:50:24 +01:00 
			
		
		
		
	kommentarer
This commit is contained in:
		@@ -178,7 +178,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
        if (!validMovesToDraw.contains(clicked)) {
 | 
			
		||||
 | 
			
		||||
            try {
 | 
			
		||||
                Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
 | 
			
		||||
                final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
 | 
			
		||||
 | 
			
		||||
                // Kolla endast ifall vi kan röra på pjäsen om det är samma färg som den tur vi är på
 | 
			
		||||
                if (selectedPiece.isWhite() == whitesTurn || developerMode) {
 | 
			
		||||
@@ -191,7 +191,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
                    ArrayList<Point> allValidMoves = new ArrayList<>();
 | 
			
		||||
                    for (Piece[] pieceArr : pieces) {
 | 
			
		||||
                        for (Piece piece : pieceArr) {
 | 
			
		||||
                            if (piece == null || whitesTurn != piece.isWhite) {
 | 
			
		||||
                            if (piece == null || whitesTurn != piece.isWhite()) {
 | 
			
		||||
                                continue;
 | 
			
		||||
                            }
 | 
			
		||||
                            // Kolla ifall vi är samma färg som får röra sig
 | 
			
		||||
@@ -224,7 +224,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
        for (Piece[] pieceArr : pieces) {
 | 
			
		||||
            for (Piece piece : pieceArr) {
 | 
			
		||||
                // Ifall det är tomrum skippa
 | 
			
		||||
                if (piece == null || preferedColor != piece.isWhite) {
 | 
			
		||||
                if (piece == null || preferedColor != piece.isWhite()) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                // Lägg till alla attacker för respektive färg
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,12 @@ public final class King extends PieceKnownIfMoved {
 | 
			
		||||
        supremeRuler = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Få en ArrayList<Point> med möjliga rockadMoves
 | 
			
		||||
     *
 | 
			
		||||
     * @param pieces
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    private ArrayList<Point> getCastlingIfPossible(Piece[][] pieces) {
 | 
			
		||||
        ArrayList<Point> possibleCastling = new ArrayList<>();
 | 
			
		||||
        if (this.isMoved()) {
 | 
			
		||||
@@ -59,6 +65,12 @@ public final class King extends PieceKnownIfMoved {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gör en rockad
 | 
			
		||||
     *
 | 
			
		||||
     * @param pieces
 | 
			
		||||
     * @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll
 | 
			
		||||
     */
 | 
			
		||||
    private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) {
 | 
			
		||||
 | 
			
		||||
        Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y];
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,13 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
 | 
			
		||||
        super(isWhite, startingPosition);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Generell metod för att generera möjliga drag för LongWalkers
 | 
			
		||||
     * @param directions
 | 
			
		||||
     * @param pieces
 | 
			
		||||
     * @param isSelected
 | 
			
		||||
     * @return 
 | 
			
		||||
     */
 | 
			
		||||
    ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
@@ -19,7 +26,6 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
 | 
			
		||||
                loopX += xy[0];
 | 
			
		||||
                loopY += xy[1];
 | 
			
		||||
                boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
 | 
			
		||||
 | 
			
		||||
                if (shouldBreak) {
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
@@ -17,12 +17,12 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
        // Kolla ifall vi kan ta någon
 | 
			
		||||
        for (int pawnX : new int[]{-1, 1}) {
 | 
			
		||||
            // Position vi kollar just nu, snett upp åt höger & vänster
 | 
			
		||||
            Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1));
 | 
			
		||||
            Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1));
 | 
			
		||||
            if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            Piece piece = pieces[pos.x][pos.y];
 | 
			
		||||
            if (piece == null || piece.isWhite != piece.isWhite) {
 | 
			
		||||
            if (piece == null || piece.isWhite() != piece.isWhite()) {
 | 
			
		||||
                movable.add(pos);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -39,7 +39,7 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
 | 
			
		||||
        // Kolla om man kan gå rakt frak
 | 
			
		||||
        for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
 | 
			
		||||
            Point pos = new Point(this.position.x, this.position.y + (this.isWhite ? -pawnDY : pawnDY));
 | 
			
		||||
            Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY));
 | 
			
		||||
            boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected);
 | 
			
		||||
            if (shouldBreak) {
 | 
			
		||||
                break;
 | 
			
		||||
@@ -49,7 +49,7 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
        // Kolla ifall vi kan ta någon
 | 
			
		||||
        for (int pawnX : new int[]{-1, 1}) {
 | 
			
		||||
            // Position vi kollar just nu, snett upp åt höger & vänster
 | 
			
		||||
            Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1));
 | 
			
		||||
            Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1));
 | 
			
		||||
            addAttackMovesIfCan(pos, movable, pieces);
 | 
			
		||||
        }
 | 
			
		||||
        return movable;
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,6 @@ import java.awt.image.BufferedImage;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import javax.imageio.ImageIO;
 | 
			
		||||
 | 
			
		||||
public abstract class Piece {
 | 
			
		||||
@@ -19,7 +18,7 @@ public abstract class Piece {
 | 
			
		||||
    /**
 | 
			
		||||
     * Sant ifall pjäsens färg är vit, falskt ifall den är svart
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isWhite;
 | 
			
		||||
    private boolean isWhite;
 | 
			
		||||
    /**
 | 
			
		||||
     * SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
 | 
			
		||||
     */
 | 
			
		||||
@@ -51,7 +50,7 @@ public abstract class Piece {
 | 
			
		||||
     */
 | 
			
		||||
    private void setPieceIcon() throws IOException {
 | 
			
		||||
        String className = this.getClass().getSimpleName();
 | 
			
		||||
        String colorName = isWhite ? "White" : "Black";
 | 
			
		||||
        String colorName = this.isWhite() ? "White" : "Black";
 | 
			
		||||
        String fileName = colorName + className + ".png";
 | 
			
		||||
        InputStream is = getClass().getResourceAsStream("/img/" + fileName);
 | 
			
		||||
        icon = ImageIO.read(is);
 | 
			
		||||
@@ -109,6 +108,14 @@ public abstract class Piece {
 | 
			
		||||
        this.position = new Point(toMove);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Lägg till move ifall det går, alltså inte är schack där
 | 
			
		||||
     * @param pos drag att lägga till ifall det går
 | 
			
		||||
     * @param movable lägger till drag i denna ArrayList<Point>
 | 
			
		||||
     * @param pieces Piece[][] över brädet
 | 
			
		||||
     * @param isSelected
 | 
			
		||||
     * @return true ifall man inte kan gå längre i denna riktning
 | 
			
		||||
     */
 | 
			
		||||
    protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        // Ifall vi är utanför brädet ge tillbaka false
 | 
			
		||||
        if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
 | 
			
		||||
@@ -133,7 +140,7 @@ public abstract class Piece {
 | 
			
		||||
         * längre Ifall det är samma färg som oss betyder det att vi inte kan
 | 
			
		||||
         * lägga till den
 | 
			
		||||
         */
 | 
			
		||||
        if (pieceToCheck.isWhite() != this.isWhite) {
 | 
			
		||||
        if (pieceToCheck.isWhite() != this.isWhite()) {
 | 
			
		||||
            /**
 | 
			
		||||
             * Detta betyder att det är en motsatts pjäs här, vi kan ta men inte
 | 
			
		||||
             * gå längre
 | 
			
		||||
@@ -218,6 +225,10 @@ public abstract class Piece {
 | 
			
		||||
//        return "Piece{" + "position=" + position + ", isWhite=" + white + '}';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @return true ifall pjäsen är vit
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isWhite() {
 | 
			
		||||
        return isWhite;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user