mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 21:00:11 +01:00
isInSchack improved
This commit is contained in:
parent
76be123a54
commit
e8c18b4a52
@ -49,7 +49,7 @@ public abstract class Piece {
|
|||||||
* @param className
|
* @param className
|
||||||
* @throws IOException ifall det inte finns någon bild på pjäsen
|
* @throws IOException ifall det inte finns någon bild på pjäsen
|
||||||
*/
|
*/
|
||||||
protected void setPieceIcon() throws IOException {
|
private void setPieceIcon() throws IOException {
|
||||||
String className = this.getClass().getSimpleName();
|
String className = this.getClass().getSimpleName();
|
||||||
String colorName = isWhite ? "White" : "Black";
|
String colorName = isWhite ? "White" : "Black";
|
||||||
String fileName = colorName + className + ".png";
|
String fileName = colorName + className + ".png";
|
||||||
@ -96,7 +96,6 @@ public abstract class Piece {
|
|||||||
*
|
*
|
||||||
* @param pieces
|
* @param pieces
|
||||||
* @param toMove
|
* @param toMove
|
||||||
* @param selected
|
|
||||||
*/
|
*/
|
||||||
public void move(Piece[][] pieces, Point toMove) {
|
public void move(Piece[][] pieces, Point toMove) {
|
||||||
|
|
||||||
@ -149,6 +148,14 @@ public abstract class Piece {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa att flytta pjäs till pos och ifall det inte är schack så lägg ge
|
||||||
|
* tillbaka möjligt drag i en ArrayList
|
||||||
|
*
|
||||||
|
* @param pieces
|
||||||
|
* @param pos
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
ArrayList<Point> tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) {
|
ArrayList<Point> tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) {
|
||||||
ArrayList<Point> movable = new ArrayList<>();
|
ArrayList<Point> movable = new ArrayList<>();
|
||||||
// Kom ihåg vart vi var
|
// Kom ihåg vart vi var
|
||||||
@ -162,7 +169,7 @@ public abstract class Piece {
|
|||||||
pieces[previousPosition.x][previousPosition.y] = null;
|
pieces[previousPosition.x][previousPosition.y] = null;
|
||||||
this.position = new Point(pos);
|
this.position = new Point(pos);
|
||||||
|
|
||||||
boolean inSchack = checkIfSchack(pos, pieces);
|
boolean inSchack = isInSchack(pos, pieces);
|
||||||
|
|
||||||
// Flytta tillbaka
|
// Flytta tillbaka
|
||||||
pieces[previousPosition.x][previousPosition.y] = this;
|
pieces[previousPosition.x][previousPosition.y] = this;
|
||||||
@ -175,38 +182,30 @@ public abstract class Piece {
|
|||||||
return movable;
|
return movable;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean checkIfSchack(Point pos, Piece[][] pieces) {
|
/**
|
||||||
boolean ourColor = this.isWhite();
|
* Kolla ifall det är schack vid den här positionen
|
||||||
Piece selectedPiece = this;
|
*
|
||||||
ArrayList<Point> attacks = new ArrayList<>();
|
* @param pos
|
||||||
|
* @param pieces
|
||||||
|
* @return true ifall det är schack
|
||||||
|
*/
|
||||||
|
boolean isInSchack(Point pos, Piece[][] pieces) {
|
||||||
|
ArrayList<Point> enemyAttacks = new ArrayList<>();
|
||||||
|
|
||||||
// Fråga alla pjäser vart de kan gå/ta
|
// Fråga alla pjäser vart de kan gå/ta
|
||||||
Arrays.stream(pieces).forEach(array -> {
|
for (Piece[] pieceArr : pieces) {
|
||||||
Arrays.stream(array).filter(piece -> piece != null && piece.isWhite() != this.isWhite()).forEach(piece -> {
|
|
||||||
attacks.addAll(piece.validAttacks(pieces));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
/* for (Piece[] pieceArr : pieces) {
|
|
||||||
for (Piece piece : pieceArr) {
|
for (Piece piece : pieceArr) {
|
||||||
// Ifall det är tomrum skippa
|
if (piece != null && piece.isWhite != this.isWhite()) {
|
||||||
if (piece == null) {
|
// Lägg till alla attacker för mostståndaren
|
||||||
continue;
|
enemyAttacks.addAll(piece.validAttacks(pieces));
|
||||||
} else if (piece.isWhite() == ourColor) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Lägg till alla attacker för mostståndaren
|
|
||||||
attacks.addAll(piece.validAttacks(pieces));
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
// Kollar ifall kungen står i schack just nu
|
// Kollar ifall kungen står i schack just nu
|
||||||
for (Point attack : attacks) {
|
for (Point enemyAttack : enemyAttacks) {
|
||||||
Piece attacked = pieces[attack.x][attack.y];
|
Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y];
|
||||||
if (attacked == null) {
|
if (attackedPiece != null && attackedPiece.supremeRuler) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (attacked.supremeRuler) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user