static removed
This commit is contained in:
parent
85ceead618
commit
1ea8eba451
|
|
@ -1,2 +1 @@
|
||||||
/backend/
|
/backend/
|
||||||
/windowInterface/
|
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -1,8 +1,7 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
private int width;
|
private int width;
|
||||||
|
|
@ -10,7 +9,7 @@ public class Board {
|
||||||
private int turnNumber;
|
private int turnNumber;
|
||||||
private boolean isWhiteTurn;
|
private boolean isWhiteTurn;
|
||||||
private ArrayList<Piece> pieces;
|
private ArrayList<Piece> pieces;
|
||||||
private Set<Move.Position> highlightedPositions = new HashSet<>();
|
private List<Move.Position> highlightedPositions = new ArrayList<>();
|
||||||
|
|
||||||
private Integer selectedX = null;
|
private Integer selectedX = null;
|
||||||
private Integer selectedY = null;
|
private Integer selectedY = null;
|
||||||
|
|
@ -22,7 +21,7 @@ public class Board {
|
||||||
this.turnNumber = 0;
|
this.turnNumber = 0;
|
||||||
this.isWhiteTurn = true;
|
this.isWhiteTurn = true;
|
||||||
this.pieces = new ArrayList<>();
|
this.pieces = new ArrayList<>();
|
||||||
this.highlightedPositions = new HashSet<>();
|
this.highlightedPositions = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -302,7 +301,12 @@ public class Board {
|
||||||
|
|
||||||
|
|
||||||
public boolean isHighlighted(int x, int y) {
|
public boolean isHighlighted(int x, int y) {
|
||||||
return highlightedPositions.contains(new Move.Position(x, y));
|
for (Move.Position pos : highlightedPositions) {
|
||||||
|
if (pos.x == x && pos.y == y) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -328,7 +332,7 @@ public class Board {
|
||||||
this.selectedY = board.selectedY;
|
this.selectedY = board.selectedY;
|
||||||
|
|
||||||
// Copy highlighted positions
|
// Copy highlighted positions
|
||||||
this.highlightedPositions = new HashSet<>();
|
this.highlightedPositions = new ArrayList<>();
|
||||||
this.highlightedPositions.addAll(board.highlightedPositions);
|
this.highlightedPositions.addAll(board.highlightedPositions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -349,7 +353,8 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get valid moves for the selected piece using the Move class
|
// Get valid moves for the selected piece using the Move class
|
||||||
Set<Move.Position> validMoves = Move.getValidMoves(selectedPiece, this);
|
Move moveHelper = new Move(selectedX, selectedY, selectedX, selectedY, this);
|
||||||
|
List<Move.Position> validMoves = moveHelper.getValidMoves(selectedPiece, this);
|
||||||
|
|
||||||
// Update highlighted positions
|
// Update highlighted positions
|
||||||
highlightedPositions.clear();
|
highlightedPositions.clear();
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.ArrayList;
|
||||||
import java.util.Set;
|
import java.util.List;
|
||||||
|
|
||||||
public class Move {
|
public class Move {
|
||||||
private int fromX;
|
private int fromX;
|
||||||
|
|
@ -28,8 +28,8 @@ public class Move {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the move is in the set of valid moves for this piece
|
// Check if the move is in the list of valid moves for this piece
|
||||||
Set<Position> validMoves = getValidDestinations(movingPiece, board);
|
List<Position> validMoves = getValidDestinations(movingPiece, board);
|
||||||
return validMoves.contains(new Position(toX, toY));
|
return validMoves.contains(new Position(toX, toY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,7 +111,7 @@ public class Move {
|
||||||
for (Piece p : tempBoard.getPieces()) {
|
for (Piece p : tempBoard.getPieces()) {
|
||||||
if (p.isWhite() != opponentColor) continue; // Skip pieces of the wrong color
|
if (p.isWhite() != opponentColor) continue; // Skip pieces of the wrong color
|
||||||
|
|
||||||
Set<Position> possibleMoves = getValidDestinations(p, tempBoard);
|
List<Position> possibleMoves = getValidDestinations(p, tempBoard);
|
||||||
for (Position pos : possibleMoves) {
|
for (Position pos : possibleMoves) {
|
||||||
// Create a temporary move
|
// Create a temporary move
|
||||||
Move testMove = new Move(p.getX(), p.getY(), pos.x, pos.y, tempBoard);
|
Move testMove = new Move(p.getX(), p.getY(), pos.x, pos.y, tempBoard);
|
||||||
|
|
@ -127,8 +127,8 @@ public class Move {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<Position> getValidDestinations(Piece piece, Board board) {
|
public List<Position> getValidDestinations(Piece piece, Board board) {
|
||||||
Set<Position> moves = new HashSet<>();
|
List<Position> moves = new ArrayList<>();
|
||||||
|
|
||||||
switch (piece.getType()) {
|
switch (piece.getType()) {
|
||||||
case Pawn:
|
case Pawn:
|
||||||
|
|
@ -155,12 +155,12 @@ public class Move {
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<Position> getValidMoves(Piece piece, Board board) {
|
public List<Position> getValidMoves(Piece piece, Board board) {
|
||||||
// Get all possible moves without considering check
|
// Get all possible moves without considering check
|
||||||
Set<Position> candidateMoves = getValidDestinations(piece, board);
|
List<Position> candidateMoves = getValidDestinations(piece, board);
|
||||||
|
|
||||||
// Filter out moves that would leave king in check
|
// Filter out moves that would leave king in check
|
||||||
Set<Position> legalMoves = new HashSet<>();
|
List<Position> legalMoves = new ArrayList<>();
|
||||||
for (Position pos : candidateMoves) {
|
for (Position pos : candidateMoves) {
|
||||||
Move move = new Move(piece.getX(), piece.getY(), pos.x, pos.y, board);
|
Move move = new Move(piece.getX(), piece.getY(), pos.x, pos.y, board);
|
||||||
if (!move.putsOwnKingInCheck()) {
|
if (!move.putsOwnKingInCheck()) {
|
||||||
|
|
@ -171,7 +171,7 @@ public class Move {
|
||||||
return legalMoves;
|
return legalMoves;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isKingInCheck(Board board, boolean isWhiteKing) {
|
public boolean isKingInCheck(Board board, boolean isWhiteKing) {
|
||||||
// Find the king's position
|
// Find the king's position
|
||||||
Piece king = null;
|
Piece king = null;
|
||||||
for (Piece p : board.getPieces()) {
|
for (Piece p : board.getPieces()) {
|
||||||
|
|
@ -188,7 +188,7 @@ public class Move {
|
||||||
if (p.isWhite() == isWhiteKing) continue; // Skip pieces of same color
|
if (p.isWhite() == isWhiteKing) continue; // Skip pieces of same color
|
||||||
|
|
||||||
// Get raw moves without check validation
|
// Get raw moves without check validation
|
||||||
Set<Position> attackMoves = getValidDestinations(p, board);
|
List<Position> attackMoves = getValidDestinations(p, board);
|
||||||
|
|
||||||
// If any piece can move to king's position, king is in check
|
// If any piece can move to king's position, king is in check
|
||||||
if (attackMoves.contains(new Position(king.getX(), king.getY()))) {
|
if (attackMoves.contains(new Position(king.getX(), king.getY()))) {
|
||||||
|
|
@ -199,7 +199,7 @@ public class Move {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addPawnMoves(Set<Position> validMoves, Piece piece, Board board) {
|
private void addPawnMoves(List<Position> validMoves, Piece piece, Board board) {
|
||||||
int x = piece.getX();
|
int x = piece.getX();
|
||||||
int y = piece.getY();
|
int y = piece.getY();
|
||||||
boolean isWhite = piece.isWhite();
|
boolean isWhite = piece.isWhite();
|
||||||
|
|
@ -236,7 +236,7 @@ public class Move {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addRookMoves(Set<Position> validMoves, Piece piece, Board board) {
|
private void addRookMoves(List<Position> validMoves, Piece piece, Board board) {
|
||||||
int x = piece.getX();
|
int x = piece.getX();
|
||||||
int y = piece.getY();
|
int y = piece.getY();
|
||||||
boolean isWhite = piece.isWhite();
|
boolean isWhite = piece.isWhite();
|
||||||
|
|
@ -272,7 +272,7 @@ public class Move {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addKnightMoves(Set<Position> validMoves, Piece piece, Board board) {
|
private void addKnightMoves(List<Position> validMoves, Piece piece, Board board) {
|
||||||
int x = piece.getX();
|
int x = piece.getX();
|
||||||
int y = piece.getY();
|
int y = piece.getY();
|
||||||
boolean isWhite = piece.isWhite();
|
boolean isWhite = piece.isWhite();
|
||||||
|
|
@ -298,7 +298,7 @@ public class Move {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addBishopMoves(Set<Position> validMoves, Piece piece, Board board) {
|
private void addBishopMoves(List<Position> validMoves, Piece piece, Board board) {
|
||||||
int x = piece.getX();
|
int x = piece.getX();
|
||||||
int y = piece.getY();
|
int y = piece.getY();
|
||||||
boolean isWhite = piece.isWhite();
|
boolean isWhite = piece.isWhite();
|
||||||
|
|
@ -334,7 +334,7 @@ public class Move {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addKingMoves(Set<Position> validMoves, Piece piece, Board board) {
|
private void addKingMoves(List<Position> validMoves, Piece piece, Board board) {
|
||||||
int x = piece.getX();
|
int x = piece.getX();
|
||||||
int y = piece.getY();
|
int y = piece.getY();
|
||||||
boolean isWhite = piece.isWhite();
|
boolean isWhite = piece.isWhite();
|
||||||
|
|
@ -359,7 +359,7 @@ public class Move {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Position {
|
public class Position {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
|
||||||
|
|
@ -375,10 +375,5 @@ public class Move {
|
||||||
Position position = (Position) obj;
|
Position position = (Position) obj;
|
||||||
return x == position.x && y == position.y;
|
return x == position.x && y == position.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return 31 * x + y;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue