Nu funkar schack checken för bönder

This commit is contained in:
loveb 2022-04-21 09:40:25 +02:00
parent e7f5f3ca44
commit 0c2a7f9ed8
2 changed files with 30 additions and 46 deletions

View File

@ -33,7 +33,6 @@ public class Pawn extends PieceKnownIfMoved {
@Override @Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) { public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
// TODO: Lösa bugg där bunder kanterna inte kan röra sig
LinkedHashSet<Point> movable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
// 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
@ -41,8 +40,6 @@ public class Pawn extends PieceKnownIfMoved {
// 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++) {
System.out.println("this.position.x: " + this.position.x);
System.out.println("calced y: " + (this.position.y + (this.white ? -pawnDY : pawnDY)));
Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY)); Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY));
boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected);
if (shouldBreak) { if (shouldBreak) {
@ -68,7 +65,8 @@ public class Pawn extends PieceKnownIfMoved {
try { try {
Piece p = pieces[pos.x][pos.y]; Piece p = pieces[pos.x][pos.y];
if (p.white != this.white) { if (p.white != this.white) {
movable.add(pos); tryToMoveAndCheckIfCheck(pieces, movable, pos);
} }
} catch (Exception e) { } catch (Exception e) {
} }
@ -98,7 +96,7 @@ public class Pawn extends PieceKnownIfMoved {
} }
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
// This is an empty spot // This is an empty spot
movable.add(pos); tryToMoveAndCheckIfCheck(pieces, movable, pos);
} catch (IndexOutOfBoundsException ioobe) { } catch (IndexOutOfBoundsException ioobe) {
// This means that the player is at the edge // This means that the player is at the edge
System.out.println(pos); System.out.println(pos);

View File

@ -84,9 +84,32 @@ public abstract class Piece {
// Vi kan ta men inte längre. // Vi kan ta men inte längre.
if (!isSelected) { if (!isSelected) {
movable.add(pos); movable.add(pos);
} else {
tryToMoveAndCheckIfCheck(pieces, movable, pos);
}
return true; return true;
}
} catch (NullPointerException npe) {
// Detta är en tom plats, vi ska inte breaka
if (!isSelected) {
movable.add(pos);
return false;
} }
tryToMoveAndCheckIfCheck(pieces, movable, pos);
return false;
} catch (IndexOutOfBoundsException ioobe) {
// This means that the player is at the edge
} catch (Exception e) {
// For good meassure
}
return false;
}
void tryToMoveAndCheckIfCheck(Piece[][] pieces, LinkedHashSet movable, Point pos) {
// Kom ihåg vart vi var // Kom ihåg vart vi var
Point previousPosition = new Point(this.position); Point previousPosition = new Point(this.position);
@ -108,43 +131,6 @@ public abstract class Piece {
if (!inSchack) { if (!inSchack) {
movable.add(pos); movable.add(pos);
} }
return true;
}
} catch (NullPointerException npe) {
// Detta är en tom plats, vi ska inte breaka
if (!isSelected) {
movable.add(pos);
return false;
}
// Kom ihåg vart vi var
Point previousPosition = new Point(this.position);
// Testa att flytta
pieces[pos.x][pos.y] = this;
pieces[previousPosition.x][previousPosition.y] = null;
this.position = new Point(pos);
boolean inSchack = checkIfSchack(pos, pieces);
// Flytta tillbaka
pieces[previousPosition.x][previousPosition.y] = this;
pieces[pos.x][pos.y] = null;
this.position = new Point(previousPosition);
if (!inSchack) {
movable.add(pos);
}
return false;
} catch (IndexOutOfBoundsException ioobe) {
// This means that the player is at the edge
} catch (Exception e) {
// For good meassure
}
return false;
} }
boolean checkIfSchack(Point pos, Piece[][] pieces) { boolean checkIfSchack(Point pos, Piece[][] pieces) {