Ta bort fel jag gjorde förut

This commit is contained in:
lov3b 2022-05-14 16:42:36 +02:00
parent e2d4037654
commit ddbae03150
2 changed files with 16 additions and 11 deletions

View File

@ -75,7 +75,9 @@ public class Pawn extends PieceKnownIfMoved {
Piece piece = pieces[pos.x][pos.y];
// Ifall det är tomt här, gör ingenting
if (piece != null && piece.isWhite() != this.isWhite()) {
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
if (!isInSchackHere(pieces, pos)) {
movable.add(pos);
}
}
return movable;
}
@ -88,7 +90,9 @@ public class Pawn extends PieceKnownIfMoved {
Piece pieceToCheck = pieces[pos.x][pos.y];
if (pieceToCheck == null) {
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
if (!isInSchackHere(pieces, pos)) {
movable.add(pos);
}
return false;
}
return true;

View File

@ -69,7 +69,8 @@ public abstract class Piece {
* Ger tillbaks alla ställen pjäsen kan attackera
*
* @param pieces
* @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man inte ska bry sig ifall
* @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man inte ska bry sig
* ifall
* @return
*/
public ArrayList<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
@ -131,7 +132,9 @@ public abstract class Piece {
if (!isSelected) {
movable.add(pos);
} else {
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
if (!isInSchackHere(pieces, pos)) {
movable.add(pos);
}
}
// Fortsätt att
return false;
@ -150,7 +153,9 @@ public abstract class Piece {
if (!isSelected) {
movable.add(pos);
} else {
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
if (!isInSchackHere(pieces, pos)) {
movable.add(pos);
}
}
}
return true;
@ -165,8 +170,7 @@ public abstract class Piece {
* @param pos
* @return
*/
ArrayList<Point> tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) {
ArrayList<Point> movable = new ArrayList<>();
boolean isInSchackHere(Piece[][] pieces, Point pos) {
// Kom ihåg vart vi var
Point previousPosition = new Point(this.position);
@ -185,10 +189,7 @@ public abstract class Piece {
pieces[pos.x][pos.y] = opponentPiece;
this.position = new Point(previousPosition);
if (!inSchack) {
movable.add(pos);
}
return movable;
return inSchack;
}
/**