mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 04:40:11 +01:00
ta bort onödiga finals
This commit is contained in:
parent
5ace8e719b
commit
ea1523c3dc
@ -157,6 +157,7 @@ public class Board extends JPanel implements MouseListener {
|
||||
|
||||
/**
|
||||
* Få status över brädet
|
||||
*
|
||||
* @return SCHACK, SCHACKMATT, PATT, NORMAL
|
||||
*/
|
||||
private SchackState getSchackState() {
|
||||
|
@ -12,13 +12,13 @@ public class Horse extends Piece {
|
||||
|
||||
@Override
|
||||
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
final ArrayList<Point> movable = new ArrayList<>();
|
||||
ArrayList<Point> movable = new ArrayList<>();
|
||||
|
||||
for (int dx : new int[]{-2, -1, 1, 2}) {
|
||||
for (int direction : new int[]{-1, 1}) {
|
||||
final int stepLength = (3 - Math.abs(dx)),
|
||||
int stepLength = (3 - Math.abs(dx)),
|
||||
dy = direction * stepLength;
|
||||
final Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
|
||||
Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
|
||||
addMovesIfCan(potentialMove, movable, pieces, allowedToRecurse);
|
||||
}
|
||||
}
|
||||
|
@ -53,8 +53,8 @@ public final class King extends Piece {
|
||||
* @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll
|
||||
*/
|
||||
private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) {
|
||||
final Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y];
|
||||
final Piece king = this;
|
||||
Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y];
|
||||
Piece king = this;
|
||||
|
||||
// Null där de stod
|
||||
pieces[king.position.x][king.position.y] = null;
|
||||
@ -70,7 +70,7 @@ public final class King extends Piece {
|
||||
@Override
|
||||
public void move(Piece[][] pieces, Point toMove) {
|
||||
if (Math.abs(position.x - toMove.x) == 2) {
|
||||
final boolean goToLeftSide = toMove.x < 5;
|
||||
boolean goToLeftSide = toMove.x < 5;
|
||||
castle(pieces, goToLeftSide);
|
||||
} else {
|
||||
super.move(pieces, toMove);
|
||||
|
@ -22,14 +22,14 @@ public abstract class LongWalkers extends Piece {
|
||||
* @return
|
||||
*/
|
||||
ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) {
|
||||
final ArrayList<Point> movable = new ArrayList<>();
|
||||
ArrayList<Point> movable = new ArrayList<>();
|
||||
|
||||
for (int[] xy : directions) {
|
||||
int loopX = this.position.x, loopY = this.position.y;
|
||||
while (loopX + xy[0] >= 0 && loopX + xy[0] <= 7 && loopY + xy[1] >= 0 && loopY + xy[1] <= 7) {
|
||||
loopX += xy[0];
|
||||
loopY += xy[1];
|
||||
final boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, allowedToRecurse);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, allowedToRecurse);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
@ -50,9 +50,9 @@ public abstract class Piece {
|
||||
* @throws IOException ifall det inte finns någon bild på pjäsen
|
||||
*/
|
||||
private void setPieceIcon() throws IOException {
|
||||
final String className = this.getClass().getSimpleName();
|
||||
final String colorName = this.isWhite() ? "White" : "Black";
|
||||
final String fileName = colorName + className + ".png";
|
||||
String className = this.getClass().getSimpleName();
|
||||
String colorName = this.isWhite() ? "White" : "Black";
|
||||
String fileName = colorName + className + ".png";
|
||||
InputStream is = getClass().getResourceAsStream("/img/" + fileName);
|
||||
icon = ImageIO.read(is);
|
||||
}
|
||||
@ -127,7 +127,7 @@ public abstract class Piece {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Piece pieceToCheck = pieces[pos.x][pos.y];
|
||||
Piece pieceToCheck = pieces[pos.x][pos.y];
|
||||
|
||||
// Detta är en tom plats
|
||||
if (pieceToCheck == null) {
|
||||
@ -159,17 +159,17 @@ public abstract class Piece {
|
||||
*/
|
||||
protected boolean isInSchack(Piece[][] pieces, Point pos) {
|
||||
// Kom ihåg vart vi var
|
||||
final Point previousPosition = new Point(this.position);
|
||||
Point previousPosition = new Point(this.position);
|
||||
|
||||
// Kom ihåg motståndarpjäs
|
||||
final Piece guyThatsAlreadyHere = pieces[pos.x][pos.y];
|
||||
Piece guyThatsAlreadyHere = pieces[pos.x][pos.y];
|
||||
|
||||
// Testa att flytta
|
||||
pieces[pos.x][pos.y] = this;
|
||||
pieces[previousPosition.x][previousPosition.y] = null;
|
||||
this.position = pos;
|
||||
|
||||
final boolean inSchack = isInSchack(pieces);
|
||||
boolean inSchack = isInSchack(pieces);
|
||||
|
||||
// Flytta tillbaka
|
||||
pieces[previousPosition.x][previousPosition.y] = this;
|
||||
@ -200,7 +200,7 @@ public abstract class Piece {
|
||||
|
||||
// Kollar ifall kungen står i schack just nu
|
||||
for (Point enemyAttack : enemyAttacks) {
|
||||
final Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y];
|
||||
Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y];
|
||||
if (attackedPiece != null && attackedPiece.supremeRuler) {
|
||||
return true;
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ public class Schack {
|
||||
});
|
||||
surrender.addActionListener((ActionEvent ae) -> {
|
||||
String whosGivingUp = board.isWhitesTurn() ? "Vit" : "Svart";
|
||||
final int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?");
|
||||
int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?");
|
||||
if (choice == JOptionPane.YES_OPTION) {
|
||||
try {
|
||||
board.restartGame();
|
||||
|
@ -6,5 +6,4 @@ package schack;
|
||||
*/
|
||||
public enum SchackState {
|
||||
SCHACK, SCHACKMATT, PATT, NORMAL, REMI
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user