Lös problem där bönders attack inte registereas av kung

This commit is contained in:
loveb 2022-05-12 09:32:42 +02:00
parent 81983ddbd7
commit c0e9a30918
3 changed files with 9 additions and 6 deletions

View File

@ -228,7 +228,7 @@ public class Board extends JPanel implements MouseListener {
continue;
}
// Lägg till alla attacker för respektive färg
attacks.addAll(piece.validAttacks(pieces));
attacks.addAll(piece.validAttacks(pieces, true));
}
}

View File

@ -11,7 +11,7 @@ public class Pawn extends PieceKnownIfMoved {
}
@Override
public ArrayList<Point> validAttacks(Piece[][] pieces) {
public ArrayList<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
ArrayList<Point> movable = new ArrayList<>();
// Kolla ifall vi kan ta någon
@ -22,7 +22,8 @@ public class Pawn extends PieceKnownIfMoved {
continue;
}
Piece piece = pieces[pos.x][pos.y];
if (piece == null || piece.isWhite() != piece.isWhite()) {
if (piece == null || piece.isWhite() != this.isWhite()
|| (shouldNotCareIfAttackSpaceIsEmptyOrNot && piece.isWhite() != this.isWhite())) {
movable.add(pos);
}
}

View File

@ -69,9 +69,10 @@ public abstract class Piece {
* Ger tillbaks alla ställen pjäsen kan attackera
*
* @param pieces
* @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man inte ska bry sig ifall
* @return
*/
public ArrayList<Point> validAttacks(Piece[][] pieces) {
public ArrayList<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
return validMoves(pieces, false);
}
@ -110,6 +111,7 @@ public abstract class Piece {
/**
* Lägg till move ifall det går, alltså inte är schack där
*
* @param pos drag att lägga till ifall det går
* @param movable lägger till drag i denna ArrayList<Point>
* @param pieces Piece[][] över brädet
@ -204,7 +206,7 @@ public abstract class Piece {
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));
enemyAttacks.addAll(piece.validAttacks(pieces, false));
}
}
}
@ -226,7 +228,7 @@ public abstract class Piece {
}
/**
*
*
* @return true ifall pjäsen är vit
*/
public boolean isWhite() {