From 9e15058fb854c22bcf45ee1d7f65646853e7079a Mon Sep 17 00:00:00 2001 From: "MSI-AB\\antoineB" Date: Sun, 20 Apr 2025 19:15:59 +0200 Subject: [PATCH] les pieces bougent !!!!! --- src/backend/Board.java | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/src/backend/Board.java b/src/backend/Board.java index 1a6e5ed..2f7bacb 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -432,8 +432,100 @@ private ArrayList computeLegalMoves(Piece piece) { if (isEnemy(x + 1, nextY, piece.isWhite())) { moves.add(new int[]{x + 1, nextY}); } + } + if (type == PieceType.Rook) { + // Directions : haut, bas, gauche, droite + int[][] directions = {{1,0},{-1,0},{0,1},{0,-1}}; + for (int[] dir : directions) { + int nx = x + dir[0]; + int ny = y + dir[1]; + while (nx >= 0 && nx < width && ny >= 0 && ny < height) { + if (isEmpty(nx, ny)) { + moves.add(new int[]{nx, ny}); + } else { + if (isEnemy(nx, ny, piece.isWhite())) { + moves.add(new int[]{nx, ny}); + } + break; + } + nx += dir[0]; + ny += dir[1]; + } + } + } + + if (type == PieceType.Bishop) { + // Directions : diagonales + int[][] directions = {{1,1},{1,-1},{-1,1},{-1,-1}}; + for (int[] dir : directions) { + int nx = x + dir[0]; + int ny = y + dir[1]; + while (nx >= 0 && nx < width && ny >= 0 && ny < height) { + if (isEmpty(nx, ny)) { + moves.add(new int[]{nx, ny}); + } else { + if (isEnemy(nx, ny, piece.isWhite())) { + moves.add(new int[]{nx, ny}); + } + break; + } + nx += dir[0]; + ny += dir[1]; + } + } + } + + if (type == PieceType.Queen) { + // Combine Rook + Bishop + int[][] directions = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; + for (int[] dir : directions) { + int nx = x + dir[0]; + int ny = y + dir[1]; + while (nx >= 0 && nx < width && ny >= 0 && ny < height) { + if (isEmpty(nx, ny)) { + moves.add(new int[]{nx, ny}); + } else { + if (isEnemy(nx, ny, piece.isWhite())) { + moves.add(new int[]{nx, ny}); + } + break; + } + nx += dir[0]; + ny += dir[1]; + } + } + } + + if (type == PieceType.Knight) { + // 8 mouvements possibles + int[][] jumps = {{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}}; + for (int[] jump : jumps) { + int nx = x + jump[0]; + int ny = y + jump[1]; + if (nx >= 0 && nx < width && ny >= 0 && ny < height) { + if (isEmpty(nx, ny) || isEnemy(nx, ny, piece.isWhite())) { + moves.add(new int[]{nx, ny}); + } + } + } + } + + if (type == PieceType.King) { + // 8 directions, une seule case + int[][] directions = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; + for (int[] dir : directions) { + int nx = x + dir[0]; + int ny = y + dir[1]; + if (nx >= 0 && nx < width && ny >= 0 && ny < height) { + if (isEmpty(nx, ny) || isEnemy(nx, ny, piece.isWhite())) { + moves.add(new int[]{nx, ny}); + } + } + } + } + return moves; }