Små ändringar

This commit is contained in:
loveb 2022-05-19 15:26:16 +02:00
parent 1d86151951
commit 69f66e7542
4 changed files with 15 additions and 19 deletions

View File

@ -104,7 +104,7 @@ public class Board extends JPanel implements MouseListener {
// Ifall vi har tryckt en pjäs och sedan ska dit
if (validMovesToDraw.contains(clickedCoordinate)) {
final Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
if (selectedPiece == null) {
validMovesToDraw.clear();
return;
@ -141,7 +141,7 @@ public class Board extends JPanel implements MouseListener {
// Om vi inte redan har valt en pjäs klickar vi en pjäs
if (!validMovesToDraw.contains(clickedCoordinate)) {
final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
Piece selectedPiece = pieces[clickedCoordinate.x][clickedCoordinate.y];
if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn) {
validMovesToDraw.addAll(selectedPiece.validMoves(pieces, true));
@ -161,9 +161,9 @@ public class Board extends JPanel implements MouseListener {
* @return SCHACK, SCHACKMATT, PATT, NORMAL
*/
private SchackState getSchackState() {
final ArrayList<Point> allValidMoves = getMoves(whitesTurn);
final ArrayList<Point> opposingAttacks = getAttacks(!whitesTurn);
final boolean weCanMove = !allValidMoves.isEmpty();
ArrayList<Point> allValidMoves = getMoves(whitesTurn);
ArrayList<Point> opposingAttacks = getAttacks(!whitesTurn);
boolean weCanMove = !allValidMoves.isEmpty();
boolean inSchack = false;
for (Point attack : opposingAttacks) {
@ -189,7 +189,7 @@ public class Board extends JPanel implements MouseListener {
}
private ArrayList<Point> getMoves(boolean whiteMovesAreWanted) {
final ArrayList<Point> allValidMoves = new ArrayList<>();
ArrayList<Point> allValidMoves = new ArrayList<>();
for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) {
if (piece == null || whiteMovesAreWanted != piece.isWhite()) {
@ -202,7 +202,7 @@ public class Board extends JPanel implements MouseListener {
}
public ArrayList<Point> getAttacks(boolean whiteAttacksAreWanted) {
final ArrayList attacks = new ArrayList();
ArrayList attacks = new ArrayList();
for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) {
if (piece == null || whiteAttacksAreWanted != piece.isWhite()) {

View File

@ -24,22 +24,22 @@ public final class King extends Piece {
}
boolean[] somethingBetweenOrSchackOnTheWay = new boolean[2]; // Vänster, höger
int left_modifier = -1, right_modifier = 1;
for (int modifier : new int[]{left_modifier, right_modifier}) {
int leftModifier = -1, rightModifier = 1;
for (int modifier : new int[]{leftModifier, rightModifier}) {
for (int loopX = this.position.x + modifier; loopX > 0 && loopX < 7; loopX += modifier) {
if (pieces[loopX][this.position.y] != null || isInSchack(pieces, new Point(loopX, this.position.y))) {
somethingBetweenOrSchackOnTheWay[(modifier == left_modifier) ? 0 : 1] = true;
somethingBetweenOrSchackOnTheWay[(modifier == leftModifier) ? 0 : 1] = true;
break;
}
}
}
left_modifier = 0;
right_modifier = 1;
for (int direction : new int[]{left_modifier, right_modifier}) {
leftModifier = 0;
rightModifier = 1;
for (int direction : new int[]{leftModifier, rightModifier}) {
if (!somethingBetweenOrSchackOnTheWay[direction]) {
Piece possibleRook = pieces[direction == left_modifier ? 0 : 7][this.position.y];
Piece possibleRook = pieces[direction == leftModifier ? 0 : 7][this.position.y];
if (possibleRook != null && !possibleRook.isMoved()) {
possibleCastling.add(new Point(direction == left_modifier ? 2 : 6, this.position.y));
possibleCastling.add(new Point(direction == leftModifier ? 2 : 6, this.position.y));
}
}
}
@ -75,7 +75,6 @@ public final class King extends Piece {
} else {
super.move(pieces, toMove);
}
}
@Override

View File

@ -3,8 +3,6 @@ package schack;
import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class Pawn extends Piece {

View File

@ -17,7 +17,6 @@ public class Rook extends LongWalkers {
pieces,
allowedToRecurse
);
}
}