mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 21:00:11 +01:00
Små ändringar
This commit is contained in:
parent
1d86151951
commit
69f66e7542
@ -104,7 +104,7 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
|
|
||||||
// Ifall vi har tryckt på en pjäs och sedan ska gå dit
|
// Ifall vi har tryckt på en pjäs och sedan ska gå dit
|
||||||
if (validMovesToDraw.contains(clickedCoordinate)) {
|
if (validMovesToDraw.contains(clickedCoordinate)) {
|
||||||
final Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
|
Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
|
||||||
if (selectedPiece == null) {
|
if (selectedPiece == null) {
|
||||||
validMovesToDraw.clear();
|
validMovesToDraw.clear();
|
||||||
return;
|
return;
|
||||||
@ -141,7 +141,7 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
|
|
||||||
// Om vi inte redan har valt en pjäs klickar vi på en pjäs
|
// Om vi inte redan har valt en pjäs klickar vi på en pjäs
|
||||||
if (!validMovesToDraw.contains(clickedCoordinate)) {
|
if (!validMovesToDraw.contains(clickedCoordinate)) {
|
||||||
final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
|
Piece selectedPiece = pieces[clickedCoordinate.x][clickedCoordinate.y];
|
||||||
|
|
||||||
if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn) {
|
if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn) {
|
||||||
validMovesToDraw.addAll(selectedPiece.validMoves(pieces, true));
|
validMovesToDraw.addAll(selectedPiece.validMoves(pieces, true));
|
||||||
@ -161,9 +161,9 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
* @return SCHACK, SCHACKMATT, PATT, NORMAL
|
* @return SCHACK, SCHACKMATT, PATT, NORMAL
|
||||||
*/
|
*/
|
||||||
private SchackState getSchackState() {
|
private SchackState getSchackState() {
|
||||||
final ArrayList<Point> allValidMoves = getMoves(whitesTurn);
|
ArrayList<Point> allValidMoves = getMoves(whitesTurn);
|
||||||
final ArrayList<Point> opposingAttacks = getAttacks(!whitesTurn);
|
ArrayList<Point> opposingAttacks = getAttacks(!whitesTurn);
|
||||||
final boolean weCanMove = !allValidMoves.isEmpty();
|
boolean weCanMove = !allValidMoves.isEmpty();
|
||||||
|
|
||||||
boolean inSchack = false;
|
boolean inSchack = false;
|
||||||
for (Point attack : opposingAttacks) {
|
for (Point attack : opposingAttacks) {
|
||||||
@ -189,7 +189,7 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<Point> getMoves(boolean whiteMovesAreWanted) {
|
private ArrayList<Point> getMoves(boolean whiteMovesAreWanted) {
|
||||||
final ArrayList<Point> allValidMoves = new ArrayList<>();
|
ArrayList<Point> allValidMoves = new ArrayList<>();
|
||||||
for (Piece[] pieceArr : pieces) {
|
for (Piece[] pieceArr : pieces) {
|
||||||
for (Piece piece : pieceArr) {
|
for (Piece piece : pieceArr) {
|
||||||
if (piece == null || whiteMovesAreWanted != piece.isWhite()) {
|
if (piece == null || whiteMovesAreWanted != piece.isWhite()) {
|
||||||
@ -202,7 +202,7 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Point> getAttacks(boolean whiteAttacksAreWanted) {
|
public ArrayList<Point> getAttacks(boolean whiteAttacksAreWanted) {
|
||||||
final ArrayList attacks = new ArrayList();
|
ArrayList attacks = new ArrayList();
|
||||||
for (Piece[] pieceArr : pieces) {
|
for (Piece[] pieceArr : pieces) {
|
||||||
for (Piece piece : pieceArr) {
|
for (Piece piece : pieceArr) {
|
||||||
if (piece == null || whiteAttacksAreWanted != piece.isWhite()) {
|
if (piece == null || whiteAttacksAreWanted != piece.isWhite()) {
|
||||||
|
@ -24,22 +24,22 @@ public final class King extends Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean[] somethingBetweenOrSchackOnTheWay = new boolean[2]; // Vänster, höger
|
boolean[] somethingBetweenOrSchackOnTheWay = new boolean[2]; // Vänster, höger
|
||||||
int left_modifier = -1, right_modifier = 1;
|
int leftModifier = -1, rightModifier = 1;
|
||||||
for (int modifier : new int[]{left_modifier, right_modifier}) {
|
for (int modifier : new int[]{leftModifier, rightModifier}) {
|
||||||
for (int loopX = this.position.x + modifier; loopX > 0 && loopX < 7; loopX += modifier) {
|
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))) {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
left_modifier = 0;
|
leftModifier = 0;
|
||||||
right_modifier = 1;
|
rightModifier = 1;
|
||||||
for (int direction : new int[]{left_modifier, right_modifier}) {
|
for (int direction : new int[]{leftModifier, rightModifier}) {
|
||||||
if (!somethingBetweenOrSchackOnTheWay[direction]) {
|
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()) {
|
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 {
|
} else {
|
||||||
super.move(pieces, toMove);
|
super.move(pieces, toMove);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -3,8 +3,6 @@ package schack;
|
|||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import javax.swing.JMenuItem;
|
|
||||||
import javax.swing.JPopupMenu;
|
|
||||||
|
|
||||||
public class Pawn extends Piece {
|
public class Pawn extends Piece {
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ public class Rook extends LongWalkers {
|
|||||||
pieces,
|
pieces,
|
||||||
allowedToRecurse
|
allowedToRecurse
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user