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,29 +84,8 @@ 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);
return true; } else {
} tryToMoveAndCheckIfCheck(pieces, movable, pos);
// Kom ihåg vart vi var
Point previousPosition = new Point(this.position);
// Kom ihåg motståndarpjäs
Piece opponentPiece = pieces[pos.x][pos.y];
// 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] = opponentPiece;
this.position = new Point(previousPosition);
if (!inSchack) {
movable.add(pos);
} }
return true; return true;
@ -118,24 +97,7 @@ public abstract class Piece {
return false; return false;
} }
// Kom ihåg vart vi var tryToMoveAndCheckIfCheck(pieces, movable, pos);
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; return false;
} catch (IndexOutOfBoundsException ioobe) { } catch (IndexOutOfBoundsException ioobe) {
@ -147,6 +109,30 @@ public abstract class Piece {
} }
void tryToMoveAndCheckIfCheck(Piece[][] pieces, LinkedHashSet movable, Point pos) {
// Kom ihåg vart vi var
Point previousPosition = new Point(this.position);
// Kom ihåg motståndarpjäs
Piece opponentPiece = pieces[pos.x][pos.y];
// 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] = opponentPiece;
this.position = new Point(previousPosition);
if (!inSchack) {
movable.add(pos);
}
}
boolean checkIfSchack(Point pos, Piece[][] pieces) { boolean checkIfSchack(Point pos, Piece[][] pieces) {
boolean ourColor = this.isWhite(); boolean ourColor = this.isWhite();
Piece selectedPiece = this; Piece selectedPiece = this;