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