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
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
return getMoves(
new int[][]{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}},
pieces,
isSelected
allowedToRecurse
);
}

View File

@ -11,7 +11,7 @@ public class Horse extends Piece {
}
@Override
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
ArrayList<Point> movable = new ArrayList<>();
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 dy = direction * stepLength;
Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
addMovesIfCan(potentialMove, movable, pieces, isSelected);
addMovesIfCan(potentialMove, movable, pieces, allowedToRecurse);
}
}
return movable;

View File

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

View File

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

View File

@ -27,13 +27,13 @@ public abstract class Piece {
* Bild av pjäsen som ritas ut bärdet
*/
protected BufferedImage icon;
public Piece(boolean white, Point startingPosition) throws IOException {
this.isWhite = white;
this.position = startingPosition;
setPieceIcon();
}
public Piece(boolean white) {
this.isWhite = white;
}
@ -56,10 +56,10 @@ public abstract class Piece {
* Ger tillbaks alla ställen pjäsen kan till
*
* @param pieces
* @param isSelected
* @param allowedToRecurse
* @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
@ -98,7 +98,7 @@ public abstract class Piece {
if (toMove.x >= pieces.length || toMove.y < 0 || position.x >= pieces[0].length || position.y < 0) {
return;
}
pieces[toMove.x][toMove.y] = this;
pieces[position.x][position.y] = null;
this.position = new Point(toMove);
@ -110,20 +110,20 @@ public abstract class Piece {
* @param pos drag att lägga till ifall det går
* @param movable lägger till drag i denna ArrayList
* @param pieces Piece[][] över brädet
* @param isSelected
* @param allowedToRecurse
* @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
if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
return false;
}
Piece pieceToCheck = pieces[pos.x][pos.y];
// Detta är en tom plats
if (pieceToCheck == null) {
if (!isSelected || !isInSchack(pieces, pos)) {
if (!allowedToRecurse || !isInSchack(pieces, pos)) {
movable.add(pos);
}
return false;
@ -135,11 +135,11 @@ public abstract class Piece {
* lägga till den
*/
if ((pieceToCheck.isWhite() != this.isWhite())
&& ((isSelected && !isInSchack(pieces, pos)) || !isSelected)) {
&& ((allowedToRecurse && !isInSchack(pieces, pos)) || !allowedToRecurse)) {
movable.add(pos);
}
return true;
}
/**
@ -149,7 +149,7 @@ public abstract class Piece {
* @param pos Kollar ifall det är schack om denna Piece flyttar hit
* @return true ifall det är schack
*/
protected boolean isInSchack(Piece[][] pieces, Point pos) {
protected boolean isInSchack(Piece[][] pieces, Point pos) {
// Kom ihåg vart vi var
Point previousPosition = new Point(this.position);
@ -160,14 +160,14 @@ public abstract class Piece {
pieces[pos.x][pos.y] = this;
pieces[previousPosition.x][previousPosition.y] = null;
this.position = pos;
boolean inSchack = isInSchack(pieces);
// Flytta tillbaka
pieces[previousPosition.x][previousPosition.y] = this;
pieces[pos.x][pos.y] = guyThatsAlreadyHere;
this.position = previousPosition;
return inSchack;
}
@ -177,7 +177,7 @@ public abstract class Piece {
* @param pieces Piece[][] över hela brädet
* @return true ifall det är schack
*/
private boolean isInSchack(Piece[][] pieces) {
protected boolean isInSchack(Piece[][] pieces) {
ArrayList<Point> enemyAttacks = new ArrayList<>();
// Fråga alla pjäser vart de kan /ta
@ -199,7 +199,7 @@ public abstract class Piece {
}
return false;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}';
@ -222,5 +222,5 @@ public abstract class Piece {
public boolean isMoved() {
return false;
}
}

View File

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

View File

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