Fixa så att varje pjäs endast roppar på en schackfunktion

This commit is contained in:
lov3b 2022-05-15 14:12:44 +02:00
parent ddbae03150
commit 6eea10491b
2 changed files with 25 additions and 18 deletions

View File

@ -10,6 +10,14 @@ public class Pawn extends PieceKnownIfMoved {
super(isWhite, startingPosition);
}
/**
* 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
* @return Alla lämpliga attackMoves
*/
@Override
public ArrayList<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
ArrayList<Point> movable = new ArrayList<>();
@ -75,7 +83,7 @@ public class Pawn extends PieceKnownIfMoved {
Piece piece = pieces[pos.x][pos.y];
// Ifall det är tomt här, gör ingenting
if (piece != null && piece.isWhite() != this.isWhite()) {
if (!isInSchackHere(pieces, pos)) {
if (!isInSchack(pieces, pos)) {
movable.add(pos);
}
}
@ -90,7 +98,7 @@ public class Pawn extends PieceKnownIfMoved {
Piece pieceToCheck = pieces[pos.x][pos.y];
if (pieceToCheck == null) {
if (!isInSchackHere(pieces, pos)) {
if (!isInSchack(pieces, pos)) {
movable.add(pos);
}
return false;

View File

@ -38,10 +38,6 @@ public abstract class Piece {
this.isWhite = white;
}
public void setPosition(Point p) {
this.position = p;
}
/**
* Ladda in pjäsbild från paketet img
*
@ -69,9 +65,9 @@ public abstract class Piece {
* Ger tillbaks alla ställen pjäsen kan attackera
*
* @param pieces
* @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man inte ska bry sig
* @param shouldNotCareIfAttackSpaceIsEmptyOrNot För bönder ifall den ska kolla ifall det är något i möjliga attackrutor
* ifall
* @return
* @return Alla lämpliga attackMoves
*/
public ArrayList<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
return validMoves(pieces, false);
@ -83,7 +79,6 @@ public abstract class Piece {
* @param g2
*/
public void draw(Graphics2D g2) {
g2.drawImage(
icon,
position.x * Board.SIZE_OF_TILE,
@ -99,7 +94,6 @@ public abstract class Piece {
* @param toMove
*/
public void move(Piece[][] pieces, Point toMove) {
// Gör ingenting ifall vi är utanför brädet
if (toMove.x >= pieces.length || toMove.y < 0 || position.x >= pieces[0].length || position.y < 0) {
return;
@ -114,7 +108,7 @@ public abstract class Piece {
* Lägg till move ifall det går, alltså inte är schack där
*
* @param pos drag att lägga till ifall det går
* @param movable lägger till drag i denna ArrayList<Point>
* @param movable lägger till drag i denna ArrayList
* @param pieces Piece[][] över brädet
* @param isSelected
* @return true ifall man inte kan längre i denna riktning
@ -132,7 +126,7 @@ public abstract class Piece {
if (!isSelected) {
movable.add(pos);
} else {
if (!isInSchackHere(pieces, pos)) {
if (!isInSchack(pieces, pos)) {
movable.add(pos);
}
}
@ -153,7 +147,7 @@ public abstract class Piece {
if (!isSelected) {
movable.add(pos);
} else {
if (!isInSchackHere(pieces, pos)) {
if (!isInSchack(pieces, pos)) {
movable.add(pos);
}
}
@ -170,7 +164,7 @@ public abstract class Piece {
* @param pos
* @return
*/
boolean isInSchackHere(Piece[][] pieces, Point pos) {
private boolean moveAndTestSchackHere(Piece[][] pieces, Point pos) {
// Kom ihåg vart vi var
Point previousPosition = new Point(this.position);
@ -182,7 +176,7 @@ public abstract class Piece {
pieces[previousPosition.x][previousPosition.y] = null;
this.position = new Point(pos);
boolean inSchack = isInSchack(pos, pieces);
boolean inSchack = isInSchack(pieces, null);
// Flytta tillbaka
pieces[previousPosition.x][previousPosition.y] = this;
@ -195,11 +189,16 @@ public abstract class Piece {
/**
* Kolla ifall det är schack vid den här positionen
*
* @param pos
* @param pieces
* @param pieces Piece[][] över hela brädet
* @param pos null ifall man endast ska kolla ifall det är schack just nu,
* alltså ifall pos inte är null testar vi att flytta oss dit och sedan
* kollar ifall det är schack där
* @return true ifall det är schack
*/
boolean isInSchack(Point pos, Piece[][] pieces) {
boolean isInSchack(Piece[][] pieces, Point pos) {
if (pos != null) {
return moveAndTestSchackHere(pieces, pos);
}
ArrayList<Point> enemyAttacks = new ArrayList<>();
// Fråga alla pjäser vart de kan /ta