Sluta använda råa listor samt ta bort onödiga måsvingar

This commit is contained in:
Love 2022-12-04 16:23:53 +01:00
parent d934ba100a
commit 1d38695acc
No known key found for this signature in database
GPG Key ID: A3C10DC241C8FA9F
3 changed files with 36 additions and 51 deletions

View File

@ -83,7 +83,7 @@ public class Board extends JPanel implements MouseListener {
}
// Måla alla pjäser
for (Piece[] pieceArr : pieces) {
for (Piece[] pieceArr : pieces)
for (Piece piece : pieceArr) {
if (piece == null) {
continue;
@ -91,19 +91,17 @@ public class Board extends JPanel implements MouseListener {
piece.draw(g2);
}
}
}
private void drawSquares(Graphics2D g2) {
g2.setBackground(Color.WHITE);
g2.setColor(Color.DARK_GRAY);
for (int i = 0; i < 8; i += 2) {
for (int i = 0; i < 8; i += 2)
for (int j = 0; j < 8; j += 2) {
g2.fillRect(i * SIZE_OF_TILE, j * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE);
g2.fillRect((i + 1) * SIZE_OF_TILE, (j + 1) * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE);
}
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
@ -133,12 +131,12 @@ public class Board extends JPanel implements MouseListener {
String msg = stateStr.charAt(0) + stateStr.substring(1, stateStr.length()).toLowerCase();
int choice = JOptionPane.showConfirmDialog(this, msg + "\nVill du starta om?");
if (choice == JOptionPane.YES_OPTION) {
if (choice == JOptionPane.YES_OPTION)
try {
restartGame();
} catch (IOException ex) {
}
}
break;
default:
}
@ -152,14 +150,13 @@ public class Board extends JPanel implements MouseListener {
if (!validMovesToDraw.contains(clickedCoordinate)) {
Piece selectedPiece = pieces[clickedCoordinate.x][clickedCoordinate.y];
if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn) {
if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn)
validMovesToDraw.addAll(selectedPiece.validMoves(pieces, true));
} else {
else
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
}
} else {
} else
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
}
getParent().repaint();
}
@ -177,49 +174,45 @@ public class Board extends JPanel implements MouseListener {
boolean inSchack = false;
for (Point attack : opposingAttacks) {
final Piece attacked = pieces[attack.x][attack.y];
if (attacked == null) {
if (attacked == null)
continue;
}
if (attacked.supremeRuler) {
inSchack = true;
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
getParent().repaint();
if (weCanMove) {
if (weCanMove)
return SchackState.SCHACK;
} else {
else
return SchackState.SCHACKMATT;
}
}
}
if (!inSchack && !weCanMove) {
if (!inSchack && !weCanMove)
return SchackState.PATT;
}
return SchackState.NORMAL;
}
private List<Point> getMoves(boolean whiteMovesAreWanted) {
List<Point> allValidMoves = new ArrayList<>();
for (Piece[] pieceArr : pieces) {
for (Piece[] pieceArr : pieces)
for (Piece piece : pieceArr) {
if (piece == null || whiteMovesAreWanted != piece.isWhite()) {
if (piece == null || whiteMovesAreWanted != piece.isWhite())
continue;
}
allValidMoves.addAll(piece.validMoves(pieces, true));
}
}
return allValidMoves;
}
public List<Point> getAttacks(boolean whiteAttacksAreWanted) {
List attacks = new ArrayList();
for (Piece[] pieceArr : pieces) {
List<Point> attacks = new ArrayList<>();
for (Piece[] pieceArr : pieces)
for (Piece piece : pieceArr) {
if (piece == null || whiteAttacksAreWanted != piece.isWhite()) {
if (piece == null || whiteAttacksAreWanted != piece.isWhite())
continue;
}
attacks.addAll(piece.validAttacks(pieces, true));
}
}
return attacks;
}

View File

@ -30,15 +30,14 @@ public class Pawn extends Piece {
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));
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)
continue;
}
Piece piece = pieces[pos.x][pos.y];
if (piece == null || piece.isWhite() != this.isWhite()
|| (shouldNotCareIfAttackSpaceIsEmptyOrNot && piece.isWhite() != this.isWhite())) {
|| (shouldNotCareIfAttackSpaceIsEmptyOrNot && piece.isWhite() != this.isWhite()))
movable.add(pos);
}
}
return movable;
}
@ -54,10 +53,9 @@ public class Pawn extends Piece {
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
final Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY));
boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse);
if (shouldBreak) {
if (shouldBreak)
break;
}
}
// Kolla ifall vi kan ta någon
for (int pawnX : new int[] { -1, 1 }) {
@ -78,7 +76,7 @@ public class Pawn extends Piece {
* @param pieces
*/
private List<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) {
List<Point> movable = new ArrayList();
List<Point> movable = new ArrayList<>();
// Se till att vi inte är utanför brädet
if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) {
return movable;
@ -94,16 +92,16 @@ public class Pawn extends Piece {
}
@Override
protected boolean addMovesIfCan(Point pos, List movable, Piece[][] pieces, boolean allowedToRecurse) {
protected boolean addMovesIfCan(Point pos, List<Point> movable, Piece[][] pieces, boolean allowedToRecurse) {
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
return false;
}
Piece pieceToCheck = pieces[pos.x][pos.y];
if (pieceToCheck == null) {
if (!isInSchack(pieces, pos)) {
if (!isInSchack(pieces, pos))
movable.add(pos);
}
return false;
}
return true;

View File

@ -125,17 +125,15 @@ public abstract class Piece {
*/
protected boolean addMovesIfCan(Point pos, List<Point> movable, Piece[][] pieces, boolean allowedToRecurse) {
// Ifall vi är utanför brädet ge tillbaka false
if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0)
return false;
}
Piece pieceToCheck = pieces[pos.x][pos.y];
// Detta är en tom plats
if (pieceToCheck == null) {
if (!allowedToRecurse || !isInSchack(pieces, pos)) {
if (!allowedToRecurse || !isInSchack(pieces, pos))
movable.add(pos);
}
return false;
}
@ -145,9 +143,8 @@ public abstract class Piece {
* lägga till den
*/
if ((pieceToCheck.isWhite() != this.isWhite())
&& ((allowedToRecurse && !isInSchack(pieces, pos)) || !allowedToRecurse)) {
&& ((allowedToRecurse && !isInSchack(pieces, pos)) || !allowedToRecurse))
movable.add(pos);
}
return true;
}
@ -191,21 +188,18 @@ public abstract class Piece {
List<Point> enemyAttacks = new ArrayList<>();
// Fråga alla pjäser vart de kan /ta
for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) {
if (piece != null && piece.isWhite != this.isWhite()) {
for (Piece[] pieceArr : pieces)
for (Piece piece : pieceArr)
if (piece != null && piece.isWhite != this.isWhite())
// Lägg till alla attacker för mostståndaren
enemyAttacks.addAll(piece.validAttacks(pieces, false));
}
}
}
// Kollar ifall kungen står i schack just nu
for (Point enemyAttack : enemyAttacks) {
Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y];
if (attackedPiece != null && attackedPiece.supremeRuler) {
if (attackedPiece != null && attackedPiece.supremeRuler)
return true;
}
}
return false;
}