Fix le rockad

Nu går det inte längre att göra rockad till ett ställe där man står i schack, eller till ett ställe där det hade varit schack på vägen. isSelected har även blivit bytt till det mer beskrivande namnet allowedToRecurse
This commit is contained in:
lov3b 2022-05-15 17:44:33 +02:00
parent e9a5416d1f
commit 0548a96061
8 changed files with 64 additions and 70 deletions

View File

@ -11,11 +11,11 @@ public class Bishop extends LongWalkers {
} }
@Override @Override
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) { public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
return getMoves( return getMoves(
new int[][]{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}}, new int[][]{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}},
pieces, pieces,
isSelected allowedToRecurse
); );
} }

View File

@ -11,7 +11,7 @@ public class Horse extends Piece {
} }
@Override @Override
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) { public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
ArrayList<Point> movable = new ArrayList<>(); ArrayList<Point> movable = new ArrayList<>();
for (int dx : new int[]{-2, -1, 1, 2}) { for (int dx : new int[]{-2, -1, 1, 2}) {
@ -19,7 +19,7 @@ public class Horse extends Piece {
int stepLength = (3 - Math.abs(dx)); int stepLength = (3 - Math.abs(dx));
int dy = direction * stepLength; int dy = direction * stepLength;
Point potentialMove = new Point(this.position.x + dx, this.position.y + dy); Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
addMovesIfCan(potentialMove, movable, pieces, isSelected); addMovesIfCan(potentialMove, movable, pieces, allowedToRecurse);
} }
} }
return movable; return movable;

View File

@ -24,43 +24,35 @@ public final class King extends PieceKnownIfMoved {
} }
// Vänster // Vänster
boolean nothingInBetween = true; boolean nothingInBetweenAndNotSchackOnTheWay = true;
for (int loopX = this.position.x - 1; loopX >= 0; loopX--) { for (int loopX = this.position.x - 1; loopX > 0; loopX--) {
if (pieces[loopX][this.position.y] != null || isInSchack(pieces, new Point(loopX, this.position.y))) {
// Kolla ifall vi kollar tornet och inget är emellan nothingInBetweenAndNotSchackOnTheWay = false;
if (loopX == 0 && nothingInBetween) { break;
}
// Check att man bara kan göra rockad ifall tornet inte rört sig }
Piece possibleRook = pieces[loopX][this.position.y]; if (nothingInBetweenAndNotSchackOnTheWay) {
Piece possibleRook = pieces[0][this.position.y];
if (possibleRook != null && !possibleRook.isMoved()) { if (possibleRook != null && !possibleRook.isMoved()) {
possibleCastling.add(new Point(2, this.position.y)); possibleCastling.add(new Point(2, this.position.y));
} }
} }
// Kolla ifall det är tomt emellan kung och torn
if (pieces[loopX][this.position.y] != null) {
nothingInBetween = false;
}
}
// Höger // Höger
nothingInBetween = true; nothingInBetweenAndNotSchackOnTheWay = true;
for (int loopX = this.position.x + 1; loopX <= 7; loopX++) { for (int loopX = this.position.x + 1; loopX < 7; loopX++) {
if (pieces[loopX][this.position.y] != null || isInSchack(pieces, new Point(loopX, this.position.y))) {
// Kolla ifall vi kollar tornet och inget är emellan nothingInBetweenAndNotSchackOnTheWay = false;
if (loopX == 7 && nothingInBetween) { break;
// Check att man bara kan göra rockad ifall tornet inte rört sig }
Piece possibleRook = pieces[loopX][this.position.y]; }
if (nothingInBetweenAndNotSchackOnTheWay) {
Piece possibleRook = pieces[7][this.position.y];
if (possibleRook != null && !possibleRook.isMoved()) { if (possibleRook != null && !possibleRook.isMoved()) {
possibleCastling.add(new Point(6, this.position.y)); possibleCastling.add(new Point(6, this.position.y));
} }
} }
// Kolla ifall det är tomt emellan kung och torn
if (pieces[loopX][this.position.y] != null) {
nothingInBetween = false;
}
}
return possibleCastling; return possibleCastling;
} }
@ -72,7 +64,6 @@ public final class King extends PieceKnownIfMoved {
* @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll * @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll
*/ */
private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) { private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) {
Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y]; Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y];
Piece king = this; Piece king = this;
@ -99,7 +90,7 @@ public final class King extends PieceKnownIfMoved {
} }
@Override @Override
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) { public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
ArrayList<Point> movable = new ArrayList<>(); ArrayList<Point> movable = new ArrayList<>();
for (int loopX = -1; loopX < 2; loopX++) { for (int loopX = -1; loopX < 2; loopX++) {
@ -107,11 +98,13 @@ public final class King extends PieceKnownIfMoved {
if (loopY == 0 && loopX == 0) { if (loopY == 0 && loopX == 0) {
continue; continue;
} }
addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces, isSelected); addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces, allowedToRecurse);
} }
} }
if (allowedToRecurse && !isInSchack(pieces)) {
movable.addAll(getCastlingIfPossible(pieces)); movable.addAll(getCastlingIfPossible(pieces));
}
return movable; return movable;
} }

View File

@ -12,12 +12,13 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
/** /**
* Generell metod för att generera möjliga drag för LongWalkers * Generell metod för att generera möjliga drag för LongWalkers
*
* @param directions * @param directions
* @param pieces * @param pieces
* @param isSelected * @param allowedToRecurse
* @return * @return
*/ */
ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) { ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) {
ArrayList<Point> movable = new ArrayList<>(); ArrayList<Point> movable = new ArrayList<>();
for (int[] xy : directions) { for (int[] xy : directions) {
@ -25,7 +26,7 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
while (loopX + xy[0] >= 0 && loopX + xy[0] <= 7 && loopY + xy[1] >= 0 && loopY + xy[1] <= 7) { while (loopX + xy[0] >= 0 && loopX + xy[0] <= 7 && loopY + xy[1] >= 0 && loopY + xy[1] <= 7) {
loopX += xy[0]; loopX += xy[0];
loopY += xy[1]; loopY += xy[1];
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, allowedToRecurse);
if (shouldBreak) { if (shouldBreak) {
break; break;
} }

View File

@ -14,8 +14,8 @@ public class Pawn extends PieceKnownIfMoved {
* Ger tillbaks alla ställen pjäsen kan attackera * Ger tillbaks alla ställen pjäsen kan attackera
* *
* @param pieces * @param pieces
* @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man ska kolla ifall det är något i möjliga attackrutor * @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man ska kolla ifall
* ifall * det är något i möjliga attackrutor ifall
* @return Alla lämpliga attackMoves * @return Alla lämpliga attackMoves
*/ */
@Override @Override
@ -40,7 +40,7 @@ public class Pawn extends PieceKnownIfMoved {
} }
@Override @Override
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) { public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
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
@ -49,7 +49,7 @@ 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++) {
Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY)); Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY));
boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse);
if (shouldBreak) { if (shouldBreak) {
break; break;
} }
@ -91,7 +91,7 @@ public class Pawn extends PieceKnownIfMoved {
} }
@Override @Override
protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean allowedToRecurse) {
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
return false; return false;
} }

View File

@ -56,10 +56,10 @@ public abstract class Piece {
* Ger tillbaks alla ställen pjäsen kan till * Ger tillbaks alla ställen pjäsen kan till
* *
* @param pieces * @param pieces
* @param isSelected * @param allowedToRecurse
* @return * @return
*/ */
public abstract ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected); public abstract ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse);
/** /**
* Ger tillbaks alla ställen pjäsen kan attackera * Ger tillbaks alla ställen pjäsen kan attackera
@ -110,10 +110,10 @@ public abstract class Piece {
* @param pos drag att lägga till ifall det går * @param pos drag att lägga till ifall det går
* @param movable lägger till drag i denna ArrayList * @param movable lägger till drag i denna ArrayList
* @param pieces Piece[][] över brädet * @param pieces Piece[][] över brädet
* @param isSelected * @param allowedToRecurse
* @return true ifall man inte kan längre i denna riktning * @return true ifall man inte kan längre i denna riktning
*/ */
protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean isSelected) { protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean allowedToRecurse) {
// Ifall vi är utanför brädet ge tillbaka false // Ifall vi är utanför brädet ge tillbaka false
if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) { if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
return false; return false;
@ -123,7 +123,7 @@ public abstract class Piece {
// Detta är en tom plats // Detta är en tom plats
if (pieceToCheck == null) { if (pieceToCheck == null) {
if (!isSelected || !isInSchack(pieces, pos)) { if (!allowedToRecurse || !isInSchack(pieces, pos)) {
movable.add(pos); movable.add(pos);
} }
return false; return false;
@ -135,7 +135,7 @@ public abstract class Piece {
* lägga till den * lägga till den
*/ */
if ((pieceToCheck.isWhite() != this.isWhite()) if ((pieceToCheck.isWhite() != this.isWhite())
&& ((isSelected && !isInSchack(pieces, pos)) || !isSelected)) { && ((allowedToRecurse && !isInSchack(pieces, pos)) || !allowedToRecurse)) {
movable.add(pos); movable.add(pos);
} }
return true; return true;
@ -177,7 +177,7 @@ public abstract class Piece {
* @param pieces Piece[][] över hela brädet * @param pieces Piece[][] över hela brädet
* @return true ifall det är schack * @return true ifall det är schack
*/ */
private boolean isInSchack(Piece[][] pieces) { protected boolean isInSchack(Piece[][] pieces) {
ArrayList<Point> enemyAttacks = new ArrayList<>(); ArrayList<Point> enemyAttacks = new ArrayList<>();
// Fråga alla pjäser vart de kan /ta // Fråga alla pjäser vart de kan /ta

View File

@ -11,11 +11,11 @@ public class Queen extends LongWalkers {
} }
@Override @Override
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) { public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
return getMoves( return getMoves(
new int[][]{{1, 0}, {-1, 0}, {0, 1}, {-1, -1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}}, new int[][]{{1, 0}, {-1, 0}, {0, 1}, {-1, -1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}},
pieces, pieces,
isSelected allowedToRecurse
); );
} }
} }

View File

@ -11,11 +11,11 @@ public class Rook extends LongWalkers {
} }
@Override @Override
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) { public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
return getMoves( return getMoves(
new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}},
pieces, pieces,
isSelected allowedToRecurse
); );
} }