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