mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +01:00
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:
parent
e9a5416d1f
commit
0548a96061
@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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 så att man bara kan göra rockad ifall tornet inte rört sig
|
||||
Piece possibleRook = pieces[loopX][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;
|
||||
}
|
||||
}
|
||||
if (nothingInBetweenAndNotSchackOnTheWay) {
|
||||
Piece possibleRook = pieces[0][this.position.y];
|
||||
if (possibleRook != null && !possibleRook.isMoved()) {
|
||||
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
|
||||
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 så att man bara kan göra rockad ifall tornet inte rört sig
|
||||
Piece possibleRook = pieces[loopX][this.position.y];
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
// Kolla ifall det är tomt emellan kung och torn
|
||||
if (pieces[loopX][this.position.y] != null) {
|
||||
nothingInBetween = false;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
if (allowedToRecurse && !isInSchack(pieces)) {
|
||||
movable.addAll(getCastlingIfPossible(pieces));
|
||||
}
|
||||
return movable;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
* @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;
|
||||
}
|
||||
|
@ -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 gå 1 steg, annars 2
|
||||
@ -49,7 +49,7 @@ public class Pawn extends PieceKnownIfMoved {
|
||||
// Kolla om man kan gå 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;
|
||||
}
|
||||
|
@ -56,10 +56,10 @@ public abstract class Piece {
|
||||
* Ger tillbaks alla ställen pjäsen kan gå 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
|
||||
@ -110,10 +110,10 @@ 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 gå 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;
|
||||
@ -123,7 +123,7 @@ public abstract class Piece {
|
||||
|
||||
// Detta är en tom plats
|
||||
if (pieceToCheck == null) {
|
||||
if (!isSelected || !isInSchack(pieces, pos)) {
|
||||
if (!allowedToRecurse || !isInSchack(pieces, pos)) {
|
||||
movable.add(pos);
|
||||
}
|
||||
return false;
|
||||
@ -135,7 +135,7 @@ 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;
|
||||
@ -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 gå/ta
|
||||
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
allowedToRecurse
|
||||
);
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user