mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +01:00
bryt ut getMoves + final
This commit is contained in:
parent
24aa37a749
commit
452d7b1c4c
@ -8,7 +8,6 @@ import java.awt.Point;
|
|||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
import java.awt.event.MouseListener;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@ -116,25 +115,14 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
turnCount++;
|
turnCount++;
|
||||||
whitesTurn = !whitesTurn;
|
whitesTurn = !whitesTurn;
|
||||||
|
|
||||||
final ArrayList<Point> allValidMoves = new ArrayList<>();
|
final ArrayList<Point> allValidMoves = getMoves(whitesTurn);
|
||||||
for (Piece[] pieceArr : pieces) {
|
final ArrayList<Point> opposingAttacks = getAttacks(!whitesTurn);
|
||||||
for (Piece piece : pieceArr) {
|
|
||||||
if (piece == null || whitesTurn != piece.isWhite()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Kolla ifall vi är samma färg som får röra sig
|
|
||||||
// Ifall en pjäs får röra sig sätt weCanMove till sant och sluta
|
|
||||||
allValidMoves.addAll(piece.validMoves(pieces, true));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn);
|
|
||||||
|
|
||||||
final boolean weCanMove = !allValidMoves.isEmpty();
|
final boolean weCanMove = !allValidMoves.isEmpty();
|
||||||
boolean inSchack = false;
|
boolean inSchack = false;
|
||||||
|
|
||||||
for (Point attack : opposingAttacks) {
|
for (Point attack : opposingAttacks) {
|
||||||
Piece attacked = pieces[attack.x][attack.y];
|
final Piece attacked = pieces[attack.x][attack.y];
|
||||||
if (attacked == null) {
|
if (attacked == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -168,7 +156,6 @@ 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];
|
final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
|
||||||
|
|
||||||
if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn) {
|
if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn) {
|
||||||
@ -183,19 +170,29 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
getParent().repaint();
|
getParent().repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Point> checkAttacks(boolean preferedColor) {
|
private ArrayList<Point> getMoves(boolean whiteMovesAreWanted) {
|
||||||
|
final ArrayList<Point> allValidMoves = new ArrayList<>();
|
||||||
|
for (Piece[] pieceArr : pieces) {
|
||||||
|
for (Piece piece : pieceArr) {
|
||||||
|
if (piece == null || whiteMovesAreWanted != piece.isWhite()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
allValidMoves.addAll(piece.validMoves(pieces, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return allValidMoves;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Point> getAttacks(boolean whiteAttacksAreWanted) {
|
||||||
final ArrayList attacks = new ArrayList();
|
final ArrayList attacks = new ArrayList();
|
||||||
for (Piece[] pieceArr : pieces) {
|
for (Piece[] pieceArr : pieces) {
|
||||||
for (Piece piece : pieceArr) {
|
for (Piece piece : pieceArr) {
|
||||||
// Ifall det är tomrum skippa
|
if (piece == null || whiteAttacksAreWanted != piece.isWhite()) {
|
||||||
if (piece == null || preferedColor != piece.isWhite()) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Lägg till alla attacker för respektive färg
|
|
||||||
attacks.addAll(piece.validAttacks(pieces, true));
|
attacks.addAll(piece.validAttacks(pieces, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return attacks;
|
return attacks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user