mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-04 07:00:21 +01:00 
			
		
		
		
	Ändra från LinkedHashSet till ArrayList och ta bort oanvända funktioner
This commit is contained in:
		@@ -2,7 +2,7 @@ package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public class Bishop extends LongWalkers {
 | 
			
		||||
 | 
			
		||||
@@ -12,7 +12,7 @@ public class Bishop extends LongWalkers {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
    public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        return getMoves(
 | 
			
		||||
                new int[][]{{-1,-1},{1, 1}, {-1, 1}, {1, -1}},
 | 
			
		||||
                pieces,
 | 
			
		||||
 
 | 
			
		||||
@@ -8,10 +8,8 @@ import java.awt.Point;
 | 
			
		||||
import java.awt.event.MouseEvent;
 | 
			
		||||
import java.awt.event.MouseListener;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import javax.swing.JOptionPane;
 | 
			
		||||
import javax.swing.JPanel;
 | 
			
		||||
 | 
			
		||||
@@ -19,8 +17,8 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
 | 
			
		||||
    public static final int SIZE_OF_TILE = 100;
 | 
			
		||||
    private Piece[][] pieces = new Piece[8][8];
 | 
			
		||||
    private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>();
 | 
			
		||||
    private LinkedHashSet<Point> validDebugMovesToDraw = new LinkedHashSet<>();
 | 
			
		||||
    private ArrayList<Point> validMovesToDraw = new ArrayList<>();
 | 
			
		||||
    private ArrayList<Point> validDebugMovesToDraw = new ArrayList<>();
 | 
			
		||||
    private Point selectedPiece = new Point();
 | 
			
		||||
    private Color moveableColor = new Color(255, 191, 0);
 | 
			
		||||
    public static boolean turn = true;
 | 
			
		||||
@@ -142,12 +140,12 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
                if (selectedPiece.isWhite() == turn || developerMode) {
 | 
			
		||||
                    ArrayList<Point> attacks = checkAttacks(turn);
 | 
			
		||||
 | 
			
		||||
                    LinkedHashSet<Point> validMoves = selectedPiece.validMoves(pieces, true);
 | 
			
		||||
                    ArrayList<Point> validMoves = selectedPiece.validMoves(pieces, true);
 | 
			
		||||
                    // Kolla ifall vi kan röra oss
 | 
			
		||||
                    // Loopa igenom allt
 | 
			
		||||
                    System.out.println("\n\n\n\n\n\n");
 | 
			
		||||
 | 
			
		||||
                    LinkedHashSet<Point> allValidMoves = new LinkedHashSet<>();
 | 
			
		||||
                    ArrayList<Point> allValidMoves = new ArrayList<>();
 | 
			
		||||
                    for (Piece[] pieceArr : pieces) {
 | 
			
		||||
                        for (Piece piece : pieceArr) {
 | 
			
		||||
                            if (piece == null || turn != piece.white) {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@ package schack;
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import static java.lang.Math.abs;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public class Horse extends Piece {
 | 
			
		||||
 | 
			
		||||
@@ -13,8 +13,8 @@ public class Horse extends Piece {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        LinkedHashSet<Point> movable = new LinkedHashSet<>();
 | 
			
		||||
    public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        // TODO: Integrera
 | 
			
		||||
        /*
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,6 @@ package schack;
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
 | 
			
		||||
public final class King extends PieceKnownIfMoved {
 | 
			
		||||
 | 
			
		||||
@@ -13,11 +12,7 @@ public final class King extends PieceKnownIfMoved {
 | 
			
		||||
        supremeRuler = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isSeen(ArrayList<Piece> pieces) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void addCastlingIfCan(Piece[][] pieces, LinkedHashSet<Point> movable, Point toMove, Point selected) {
 | 
			
		||||
    private void addCastlingIfCan(Piece[][] pieces, ArrayList<Point> movable, Point toMove, Point selected) {
 | 
			
		||||
        if (moved) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -96,8 +91,8 @@ public final class King extends PieceKnownIfMoved {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        LinkedHashSet<Point> movable = new LinkedHashSet<>();
 | 
			
		||||
    public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        for (int loopX = -1; loopX < 2; loopX++) {
 | 
			
		||||
            for (int loopY = -1; loopY < 2; loopY++) {
 | 
			
		||||
@@ -112,11 +107,6 @@ public final class King extends PieceKnownIfMoved {
 | 
			
		||||
        return movable;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean chechCheck(Piece[][] pieces, Point point) {
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return "Piece{" + "hasMoved=" + moved + "position=" + position + ", isWhite=" + white + '}';
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public abstract class LongWalkers extends PieceKnownIfMoved {
 | 
			
		||||
 | 
			
		||||
@@ -10,8 +10,8 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
 | 
			
		||||
        super(isWhite, startingPosition);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    LinkedHashSet<Point> getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        LinkedHashSet<Point> movable = new LinkedHashSet<>();
 | 
			
		||||
    ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        for (int[] xy : directions) {
 | 
			
		||||
            int loopX = this.position.x, loopY = this.position.y;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
 | 
			
		||||
@@ -12,8 +12,8 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validAttacks(Piece[][] pieces) {
 | 
			
		||||
        LinkedHashSet<Point> movable = new LinkedHashSet<>();
 | 
			
		||||
    public ArrayList<Point> validAttacks(Piece[][] pieces) {
 | 
			
		||||
        ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        // Kolla ifall vi kan ta någon
 | 
			
		||||
        for (int pawnX : new int[]{-1, 1}) {
 | 
			
		||||
@@ -32,8 +32,8 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        LinkedHashSet<Point> movable = new LinkedHashSet<>();
 | 
			
		||||
    public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        ArrayList<Point> movable = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        // Om bonden har gått en gång, får gå 1 steg, annars 2
 | 
			
		||||
        final int upTo = moved ? 1 : 2;
 | 
			
		||||
@@ -59,7 +59,7 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Känns som det här skulle kunnat vara i validMoves, men nu är det såhär
 | 
			
		||||
    private void addAttackMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) {
 | 
			
		||||
    private void addAttackMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces) {
 | 
			
		||||
 | 
			
		||||
        // Ifall det är en pjäs som står här och den inte är samma färg som oss, lägg till
 | 
			
		||||
        try {
 | 
			
		||||
@@ -73,7 +73,7 @@ public class Pawn extends PieceKnownIfMoved {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
    protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -5,10 +5,8 @@ import java.awt.Point;
 | 
			
		||||
import java.awt.image.BufferedImage;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import javax.imageio.ImageIO;
 | 
			
		||||
import javax.swing.JOptionPane;
 | 
			
		||||
import static schack.Board.turn;
 | 
			
		||||
 | 
			
		||||
public abstract class Piece {
 | 
			
		||||
 | 
			
		||||
@@ -40,9 +38,9 @@ public abstract class Piece {
 | 
			
		||||
        icon = ImageIO.read(is);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public abstract LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected);
 | 
			
		||||
    public abstract ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected);
 | 
			
		||||
 | 
			
		||||
    public LinkedHashSet<Point> validAttacks(Piece[][] pieces) {
 | 
			
		||||
    public ArrayList<Point> validAttacks(Piece[][] pieces) {
 | 
			
		||||
        return validMoves(pieces, false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -68,7 +66,7 @@ public abstract class Piece {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
    protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        // Instead of checking index and null, try-catch
 | 
			
		||||
        try {
 | 
			
		||||
            // Ifall vi kollar utanför brädet kommer detta att faila
 | 
			
		||||
@@ -109,7 +107,7 @@ public abstract class Piece {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void tryToMoveAndCheckIfCheck(Piece[][] pieces, LinkedHashSet movable, Point pos) {
 | 
			
		||||
    void tryToMoveAndCheckIfCheck(Piece[][] pieces, ArrayList movable, Point pos) {
 | 
			
		||||
        // Kom ihåg vart vi var
 | 
			
		||||
        Point previousPosition = new Point(this.position);
 | 
			
		||||
 | 
			
		||||
@@ -136,7 +134,7 @@ public abstract class Piece {
 | 
			
		||||
    boolean checkIfSchack(Point pos, Piece[][] pieces) {
 | 
			
		||||
        boolean ourColor = this.isWhite();
 | 
			
		||||
        Piece selectedPiece = this;
 | 
			
		||||
        LinkedHashSet<Point> attacks = new LinkedHashSet<>();
 | 
			
		||||
        ArrayList<Point> attacks = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        // Fråga alla pjäser vart de kan gå/ta
 | 
			
		||||
        for (Piece[] pieceArr : pieces) {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public class Queen extends LongWalkers {
 | 
			
		||||
 | 
			
		||||
@@ -12,7 +12,7 @@ public class Queen extends LongWalkers {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
    public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        return getMoves(
 | 
			
		||||
                new int[][]{{1, 0}, {-1, 0}, {0, 1}, {-1, -1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}},
 | 
			
		||||
                pieces,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ package schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Point;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.LinkedHashSet;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public class Rook extends LongWalkers {
 | 
			
		||||
 | 
			
		||||
@@ -12,7 +12,7 @@ public class Rook extends LongWalkers {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
    public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
 | 
			
		||||
        return getMoves(
 | 
			
		||||
                new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, 
 | 
			
		||||
                pieces, 
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user