diff --git a/OOP_1A2_Project/bin/.gitignore b/OOP_1A2_Project/bin/.gitignore index 5c7ebe3..6d0c385 100644 --- a/OOP_1A2_Project/bin/.gitignore +++ b/OOP_1A2_Project/bin/.gitignore @@ -1,2 +1 @@ /backend/ -/windowInterface/ diff --git a/OOP_1A2_Project/bin/backend/Board.class b/OOP_1A2_Project/bin/backend/Board.class index 42bc97c..ff9f6d5 100644 Binary files a/OOP_1A2_Project/bin/backend/Board.class and b/OOP_1A2_Project/bin/backend/Board.class differ diff --git a/OOP_1A2_Project/bin/backend/Move.class b/OOP_1A2_Project/bin/backend/Move.class index 0dd9d80..dee37af 100644 Binary files a/OOP_1A2_Project/bin/backend/Move.class and b/OOP_1A2_Project/bin/backend/Move.class differ diff --git a/OOP_1A2_Project/src/backend/Board.java b/OOP_1A2_Project/src/backend/Board.java index ab75d92..05d1ad9 100644 --- a/OOP_1A2_Project/src/backend/Board.java +++ b/OOP_1A2_Project/src/backend/Board.java @@ -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 pieces; - private Set highlightedPositions = new HashSet<>(); + private List 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 validMoves = Move.getValidMoves(selectedPiece, this); + Move moveHelper = new Move(selectedX, selectedY, selectedX, selectedY, this); + List validMoves = moveHelper.getValidMoves(selectedPiece, this); // Update highlighted positions highlightedPositions.clear(); diff --git a/OOP_1A2_Project/src/backend/Move.java b/OOP_1A2_Project/src/backend/Move.java index faf1019..02c7719 100644 --- a/OOP_1A2_Project/src/backend/Move.java +++ b/OOP_1A2_Project/src/backend/Move.java @@ -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 validMoves = getValidDestinations(movingPiece, board); + // Check if the move is in the list of valid moves for this piece + List 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 possibleMoves = getValidDestinations(p, tempBoard); + List 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 getValidDestinations(Piece piece, Board board) { - Set moves = new HashSet<>(); + public List getValidDestinations(Piece piece, Board board) { + List moves = new ArrayList<>(); switch (piece.getType()) { case Pawn: @@ -155,12 +155,12 @@ public class Move { return moves; } - public static Set getValidMoves(Piece piece, Board board) { + public List getValidMoves(Piece piece, Board board) { // Get all possible moves without considering check - Set candidateMoves = getValidDestinations(piece, board); + List candidateMoves = getValidDestinations(piece, board); // Filter out moves that would leave king in check - Set legalMoves = new HashSet<>(); + List 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 attackMoves = getValidDestinations(p, board); + List 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 validMoves, Piece piece, Board board) { + private void addPawnMoves(List 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 validMoves, Piece piece, Board board) { + private void addRookMoves(List 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 validMoves, Piece piece, Board board) { + private void addKnightMoves(List 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 validMoves, Piece piece, Board board) { + private void addBishopMoves(List 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 validMoves, Piece piece, Board board) { + private void addKingMoves(List 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; - } } } \ No newline at end of file