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