Update Pawn.java

This commit is contained in:
loveb 2022-05-12 09:51:56 +02:00
parent e70546c85f
commit 3a9d90ed0d

View File

@ -36,7 +36,7 @@ public class Pawn extends PieceKnownIfMoved {
ArrayList<Point> movable = new ArrayList<>(); ArrayList<Point> movable = new ArrayList<>();
// Om bonden har gått en gång, får 1 steg, annars 2 // Om bonden har gått en gång, får 1 steg, annars 2
final int upTo = moved ? 1 : 2; final int upTo = this.isMoved() ? 1 : 2;
// Kolla om man kan rakt frak // Kolla om man kan rakt frak
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
@ -51,7 +51,7 @@ public class Pawn extends PieceKnownIfMoved {
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));
addAttackMovesIfCan(pos, movable, pieces); movable.addAll(addAttackMovesIfCan(pos, pieces));
} }
return movable; return movable;
} }
@ -65,21 +65,19 @@ public class Pawn extends PieceKnownIfMoved {
* @param movable * @param movable
* @param pieces * @param pieces
*/ */
private void addAttackMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces) { private ArrayList<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) {
ArrayList movable = new ArrayList<Point>();
// 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; return movable;
} }
Piece piece = pieces[pos.x][pos.y]; Piece piece = pieces[pos.x][pos.y];
// Ifall det är tomt här, gör ingenting // Ifall det är tomt här, gör ingenting
if (piece == null) { if (piece != null && piece.isWhite() != this.isWhite()) {
return;
} else if (piece.isWhite() != this.isWhite()) {
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
} }
return movable;
} }
@Override @Override
@ -96,5 +94,4 @@ public class Pawn extends PieceKnownIfMoved {
return true; return true;
} }
} }