mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +01:00
Funkar nästan
inte löst att kolla ifall tornet har rört sig än
This commit is contained in:
parent
0a8f9c7996
commit
fbc44d29f3
@ -4,6 +4,7 @@ import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import javax.swing.text.Position;
|
||||
|
||||
public final class King extends PieceKnownIfMoved {
|
||||
|
||||
@ -25,20 +26,21 @@ public final class King extends PieceKnownIfMoved {
|
||||
if (hasMoved) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Vänster
|
||||
boolean nothingInBetween = true;
|
||||
for (int kingX = this.position.x - 1; kingX >= 0; kingX--) {
|
||||
if (kingX == 0) {
|
||||
try {
|
||||
|
||||
} catch (Exception e) {
|
||||
}
|
||||
// Kolla ifall vi kollar tornet och inget är emellan
|
||||
if (kingX == 0 && nothingInBetween) {
|
||||
movable.add(new Point(2, 7));
|
||||
}
|
||||
|
||||
boolean shouldBreak = addMovesIfCan(new Point(kingX, this.position.y), movable, pieces);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
// Kolla ifall det är tomt emellan kung och torn
|
||||
if (pieces[kingX][this.position.y] != null) {
|
||||
nothingInBetween = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Höger
|
||||
@ -51,6 +53,33 @@ public final class King extends PieceKnownIfMoved {
|
||||
|
||||
}
|
||||
|
||||
private void castle(Piece[][] pieces, boolean left) {
|
||||
|
||||
Piece rook = pieces[left ? 0 : 7][this.position.y];
|
||||
Piece king = this;
|
||||
|
||||
// Null där de stod
|
||||
pieces[king.position.x][king.position.y] = null;
|
||||
pieces[rook.position.x][rook.position.y] = null;
|
||||
// Uppdatera internt minne
|
||||
king.position.x = left ? 2 : 6;
|
||||
rook.position.x = left ? 3 : 5;
|
||||
// Uppdatera brädet
|
||||
pieces[king.position.x][king.position.y] = king;
|
||||
pieces[rook.position.x][rook.position.y] = rook;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(Piece[][] pieces, Point toMove, Point selected) {
|
||||
if (Math.abs(selected.x - toMove.x) == 2) {
|
||||
castle(pieces, isWhite);
|
||||
} else {
|
||||
super.move(pieces, toMove, selected);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
@ -81,6 +110,7 @@ public final class King extends PieceKnownIfMoved {
|
||||
}
|
||||
|
||||
}
|
||||
addCastlingIfCan(pieces, movable, position, position);
|
||||
return movable;
|
||||
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ public abstract class Piece {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) {
|
||||
// Instead of checking index and null, try-catch
|
||||
try {
|
||||
|
@ -22,4 +22,8 @@ public abstract class PieceKnownIfMoved extends Piece {
|
||||
hasMoved = true;
|
||||
}
|
||||
|
||||
public boolean hasMoved() {
|
||||
return hasMoved;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user