From 9a02a7db7ca2e69d59a0315579a07e75848860ab Mon Sep 17 00:00:00 2001 From: lrave Date: Tue, 6 May 2025 15:49:07 +0200 Subject: [PATCH 1/2] changed possible moves to pieces --- src/backend/Board.java | 108 +++----------------------------------- src/backend/Piece.java | 114 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 103 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index d21b0ac..2ab145a 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -90,6 +90,10 @@ public class Board { } } } + public Piece getPiece(int x, int y) { + if (!isInBounds (x, y)) return null; + return board [x][y]; + } private void clearConsole() { // ***************CONSOLE for (int i = 0; i < 50; i++) { @@ -212,112 +216,12 @@ public class Board { } /* utility methods */ - private boolean isInBounds(int x, int y) { + public boolean isInBounds(int x, int y) { return x >= 0 && x < width && y >= 0 && y < height; } - private void addLinearMoves(ArrayList moves, int x, int y, Piece piece, int dx, int dy) { - int nx = x + dx; - int ny = y + dy; - while (isInBounds(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 += dx; - ny += dy; - } - } - private ArrayList getValidMoves(Piece piece) { - ArrayList moves = new ArrayList<>(); - int x = piece.getX(); - int y = piece.getY(); - - switch (piece.getType()) { - case Pawn: - int direction = piece.isWhite() ? -1 : 1; - int nextY = y + direction; - - // forward move - if (isInBounds(x, nextY) && board[x][nextY] == null) { - moves.add(new int[]{x, nextY}); - - // double move from starting position - int startRow = piece.isWhite() ? 6 : 1; - int doubleStepY = y + 2 * direction; - if (y == startRow && isInBounds(x, doubleStepY) && board[x][doubleStepY] == null) { - moves.add(new int[]{x, doubleStepY}); - } - } - - // diagonal captures - for (int dx = -1; dx <= 1; dx += 2) { - int nx = x + dx; - if (isInBounds(nx, nextY) && board[nx][nextY] != null && board[nx][nextY].isWhite() != piece.isWhite()) { - moves.add(new int[]{nx, nextY}); - } - } - break; - - case Rook: - addLinearMoves(moves, x, y, piece, 1, 0); - addLinearMoves(moves, x, y, piece, -1, 0); - addLinearMoves(moves, x, y, piece, 0, 1); - addLinearMoves(moves, x, y, piece, 0, -1); - break; - - case Bishop: - addLinearMoves(moves, x, y, piece, 1, 1); - addLinearMoves(moves, x, y, piece, -1, 1); - addLinearMoves(moves, x, y, piece, 1, -1); - addLinearMoves(moves, x, y, piece, -1, -1); - break; - - case Queen: - for (int dx = -1; dx <= 1; dx++) { - for (int dy = -1; dy <= 1; dy++) { - if (dx != 0 || dy != 0) { - addLinearMoves(moves, x, y, piece, dx, dy); - } - } - } - break; - - case King: - for (int dx = -1; dx <= 1; dx++) { - for (int dy = -1; dy <= 1; dy++) { - if (dx != 0 || dy != 0) { - int nx = x + dx; - int ny = y + dy; - if (isInBounds(nx, ny) && (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite())) { - moves.add(new int[]{nx, ny}); - } - } - } - } - break; - - case Knight: - int[][] jumps = { - {1, 2}, {2, 1}, {-1, 2}, {-2, 1}, - {-1, -2}, {-2, -1}, {1, -2}, {2, -1} - }; - for (int[] j : jumps) { - int nx = x + j[0]; - int ny = y + j[1]; - if (isInBounds(nx, ny) && (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite())) { - moves.add(new int[]{nx, ny}); - } - } - break; - } - - return moves; + return piece.getValidMoves(this); } /* saving-loading feature : */ diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 55cd98a..531c84d 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -22,6 +22,7 @@ public class Piece { public int getY() { return y; } + public PieceType getType() { return type; @@ -30,5 +31,116 @@ public class Piece { public boolean isWhite() { return pieceColor; } - + public ArrayList getValidMoves(Board board) { + ArrayList moves = new ArrayList<>(); + int x = this.getX(); + int y = this.getY(); + + switch (type) { + case Pawn: + int direction = isWhite() ? -1 : 1; + int nextY = y + direction; + + // forward move + if (board.isInBounds(x, nextY) && board.getPiece(x, nextY) == null) { + moves.add(new int[]{x, nextY}); + + // double move from starting position + int startRow = isWhite() ? 6 : 1; + int doubleStepY = y + 2 * direction; + if (y == startRow && board.isInBounds(x, doubleStepY) && board.getPiece(x, doubleStepY) == null) { + moves.add(new int[]{x, doubleStepY}); + } + } + + // diagonal captures + for (int dx = -1; dx <= 1; dx += 2) { + int nx = x + dx; + if (board.isInBounds(nx, nextY)) { + Piece target = board.getPiece(nx, nextY); + if (target != null && target.isWhite() != this.isWhite()) { + moves.add(new int[]{nx, nextY}); + } + } + } + break; + + case Rook: + addLinearMoves(board, moves, x, y, 1, 0); + addLinearMoves(board, moves, x, y, -1, 0); + addLinearMoves(board, moves, x, y, 0, 1); + addLinearMoves(board, moves, x, y, 0, -1); + break; + + case Bishop: + addLinearMoves(board, moves, x, y, 1, 1); + addLinearMoves(board, moves, x, y, -1, 1); + addLinearMoves(board, moves, x, y, 1, -1); + addLinearMoves(board, moves, x, y, -1, -1); + break; + + case Queen: + for (int dx = -1; dx <= 1; dx++) { + for (int dy = -1; dy <= 1; dy++) { + if (dx != 0 || dy != 0) { + addLinearMoves(board, moves, x, y, dx, dy); + } + } + } + break; + + case King: + for (int dx = -1; dx <= 1; dx++) { + for (int dy = -1; dy <= 1; dy++) { + if (dx != 0 || dy != 0) { + int nx = x + dx; + int ny = y + dy; + if (board.isInBounds(nx, ny)) { + Piece target = board.getPiece(nx, ny); + if (target == null || target.isWhite() != this.isWhite()) { + moves.add(new int[]{nx, ny}); + } + } + } + } + } + break; + + case Knight: + int[][] jumps = { + {1, 2}, {2, 1}, {-1, 2}, {-2, 1}, + {-1, -2}, {-2, -1}, {1, -2}, {2, -1} + }; + for (int[] j : jumps) { + int nx = x + j[0]; + int ny = y + j[1]; + if (board.isInBounds(nx, ny)) { + Piece target = board.getPiece(nx, ny); + if (target == null || target.isWhite() != this.isWhite()) { + moves.add(new int[]{nx, ny}); + } + } + } + break; + } + + return moves; + } + private void addLinearMoves(Board board, ArrayList moves, int x, int y, int dx, int dy) { + int nx = x + dx; + int ny = y + dy; + while (board.isInBounds(nx, ny)) { + Piece target = board.getPiece(nx, ny); + if (target == null) { + moves.add(new int[]{nx, ny}); + } else { + if (target.isWhite() != this.isWhite()) { + moves.add(new int[]{nx, ny}); + } + break; + } + nx += dx; + ny += dy; + } + } } \ No newline at end of file From e8192a83499b83f29087f2a7812428ad4a9e3546 Mon Sep 17 00:00:00 2001 From: pierrepagot Date: Tue, 6 May 2025 16:15:17 +0200 Subject: [PATCH 2/2] added Pawn --- src/backend/Pawn.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/backend/Pawn.java diff --git a/src/backend/Pawn.java b/src/backend/Pawn.java new file mode 100644 index 0000000..8123ed9 --- /dev/null +++ b/src/backend/Pawn.java @@ -0,0 +1,5 @@ +package backend; + +public class Pawn { + +}