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

View File

@ -30,15 +30,14 @@ public class Pawn extends Piece {
for (int pawnX : new int[] { -1, 1 }) { for (int pawnX : new int[] { -1, 1 }) {
// Position vi kollar just nu, snett upp åt höger & vänster // 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) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7)
continue; continue;
}
Piece piece = pieces[pos.x][pos.y]; Piece piece = pieces[pos.x][pos.y];
if (piece == null || piece.isWhite() != this.isWhite() if (piece == null || piece.isWhite() != this.isWhite()
|| (shouldNotCareIfAttackSpaceIsEmptyOrNot && piece.isWhite() != this.isWhite())) { || (shouldNotCareIfAttackSpaceIsEmptyOrNot && piece.isWhite() != this.isWhite()))
movable.add(pos); movable.add(pos);
} }
}
return movable; return movable;
} }
@ -54,10 +53,9 @@ public class Pawn extends Piece {
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
final Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY)); final Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY));
boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse);
if (shouldBreak) { if (shouldBreak)
break; break;
} }
}
// 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 }) {
@ -78,7 +76,7 @@ public class Pawn extends Piece {
* @param pieces * @param pieces
*/ */
private List<Point> addAttackMovesIfCan(Point pos, Piece[][] 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 // 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) { if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) {
return movable; return movable;
@ -94,16 +92,16 @@ public class Pawn extends Piece {
} }
@Override @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) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
return false; return false;
} }
Piece pieceToCheck = pieces[pos.x][pos.y]; Piece pieceToCheck = pieces[pos.x][pos.y];
if (pieceToCheck == null) { if (pieceToCheck == null) {
if (!isInSchack(pieces, pos)) { if (!isInSchack(pieces, pos))
movable.add(pos); movable.add(pos);
}
return false; return false;
} }
return true; return true;

View File

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