From 1916f1f86e10926f69c29d5beedbd71f6f0af253 Mon Sep 17 00:00:00 2001 From: carol Date: Tue, 13 May 2025 13:59:14 +0200 Subject: [PATCH 1/3] part 2 complete, the game works --- src/backend/Board.java | 165 ++++++++++++++++++++++++++++++++++------- 1 file changed, 139 insertions(+), 26 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 97eb061..8f0ed81 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -12,6 +12,7 @@ public class Board { private int selectedY = -1; private int turnNumber = 0; private boolean turnWhite = true; + private ArrayList highlightedSquares = new ArrayList<>(); public Board(int colNum, int lineNum) { this.width = colNum; @@ -122,8 +123,117 @@ public class Board { return hasSelectedPiece && selectedX == x && selectedY == y; } + private boolean inBounds(int x, int y) { + return x >= 0 && x < width && y >= 0 && y < height; + } + - + private ArrayList getValidMoves(Piece piece) { + ArrayList moves = new ArrayList<>(); + int x = piece.getX(); + int y = piece.getY(); + PieceType type = piece.getType(); + + if (type == PieceType.Pawn) { + int dir = piece.isWhite() ? 1 : -1; + int startRow = piece.isWhite() ? 1 : 6; + + int oneStep = y + dir; + if (inBounds(x, oneStep) && board[x][oneStep] == null) { + moves.add(new int[]{x, oneStep}); + + // If on start row, try two steps forward + int twoStep = y + 2 * dir; + if (y == startRow && inBounds(x, twoStep) && board[x][twoStep] == null) { + moves.add(new int[]{x, twoStep}); + } + } + + // Capture diagonals + for (int dx : new int[]{-1, 1}) { + int nx = x + dx; + int ny = y + dir; + if (inBounds(nx, ny) && board[nx][ny] != null && + board[nx][ny].isWhite() != piece.isWhite()) { + moves.add(new int[]{nx, ny}); + } + } + } + else if (type == PieceType.Rook) { + int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}}; + for (int[] d : dirs) { + int nx = x + d[0], ny = y + d[1]; + while (inBounds(nx, ny)) { + if (board[nx][ny] == null) { + moves.add(new int[]{nx, ny}); + } else { + if (board[nx][ny].isWhite() != piece.isWhite()) { + moves.add(new int[]{nx, ny}); + } + break; + } + nx += d[0]; ny += d[1]; + } + } + } + + else if (type == PieceType.Bishop) { + int[][] dirs = {{1,1}, {-1,1}, {-1,-1}, {1,-1}}; + for (int[] d : dirs) { + int nx = x + d[0], ny = y + d[1]; + while (inBounds(nx, ny)) { + if (board[nx][ny] == null) { + moves.add(new int[]{nx, ny}); + } else { + if (board[nx][ny].isWhite() != piece.isWhite()) { + moves.add(new int[]{nx, ny}); + } + break; + } + nx += d[0]; ny += d[1]; + } + } + } + + else if (type == PieceType.Queen) { + // Queen = Rook + Bishop + moves.addAll(getValidMoves(new Piece(x, y, piece.isWhite(), PieceType.Rook))); + moves.addAll(getValidMoves(new Piece(x, y, piece.isWhite(), PieceType.Bishop))); + } + + else if (type == PieceType.Knight) { + int[][] jumps = { + {2,1},{1,2},{-1,2},{-2,1}, + {-2,-1},{-1,-2},{1,-2},{2,-1} + }; + for (int[] j : jumps) { + int nx = x + j[0], ny = y + j[1]; + if (inBounds(nx, ny)) { + if (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite()) { + moves.add(new int[]{nx, ny}); + } + } + } + } + + else if (type == PieceType.King) { + int[][] dirs = { + {1,0}, {-1,0}, {0,1}, {0,-1}, + {1,1}, {-1,1}, {-1,-1}, {1,-1} + }; + for (int[] d : dirs) { + int nx = x + d[0], ny = y + d[1]; + if (inBounds(nx, ny)) { + if (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite()) { + moves.add(new int[]{nx, ny}); + } + } + } + } + + return moves; + } + @@ -138,43 +248,41 @@ public class Board { selectedX = x; selectedY = y; hasSelectedPiece = true; + highlightedSquares = getValidMoves(clicked); // ✅ Highlight legal moves } } else { + // Check if clicked again on the same square to unselect if (selectedX == x && selectedY == y) { - // Unselect hasSelectedPiece = false; - } else { + highlightedSquares.clear(); + } + // If clicked on a highlighted square, move there + else if (isHighlighted(x, y)) { Piece selectedPiece = board[selectedX][selectedY]; - if (selectedPiece != null) { - // Capture if needed - if (board[x][y] != null) { - if (board[x][y].isWhite() == selectedPiece.isWhite()) { - // Can't capture own piece - hasSelectedPiece = false; - return; - } - } - // Move piece - board[x][y] = selectedPiece; - board[selectedX][selectedY] = null; - selectedPiece.setX(x); - selectedPiece.setY(y); + // Move piece + board[x][y] = selectedPiece; + board[selectedX][selectedY] = null; + selectedPiece.setX(x); + selectedPiece.setY(y); - // Switch turn - turnWhite = !turnWhite; - turnNumber++; + // Update turn + turnWhite = !turnWhite; + turnNumber++; - // Unselect - hasSelectedPiece = false; - } + // Clear selection & highlights + hasSelectedPiece = false; + highlightedSquares.clear(); + } + // Invalid move: just unselect + else { + hasSelectedPiece = false; + highlightedSquares.clear(); } } } - //public boolean isSelected(int x, int y) { - //return false - // } + /* saving-loading feature */ public String[] toFileRep() { @@ -187,6 +295,11 @@ public class Board { /* The following methods require more work */ public boolean isHighlighted(int x, int y) { + for (int[] pos : highlightedSquares) { + if (pos[0] == x && pos[1] == y) { + return true; + } + } return false; } From 4c7111cdb1c13dd6efd747ab4c2ff1a6eb97e532 Mon Sep 17 00:00:00 2001 From: carol Date: Tue, 13 May 2025 14:14:05 +0200 Subject: [PATCH 2/3] part 2 finished everything works --- src/backend/Board.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 8f0ed81..f9b3a5f 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -308,7 +308,7 @@ public class Board { } public Board(Board board) { - // TODO + // TODO hello } public void playMove(Move move) { From 7316afed2d584d9853f09d78d5e99ed1cdf8d373 Mon Sep 17 00:00:00 2001 From: carol Date: Tue, 13 May 2025 14:18:08 +0200 Subject: [PATCH 3/3] finished part 2 --- src/backend/Board.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index f9b3a5f..7db3edb 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -308,7 +308,7 @@ public class Board { } public Board(Board board) { - // TODO hello + // TODO helloyo } public void playMove(Move move) {