From 5a8de97bc2b74e50d203c65a543659c0ee341a78 Mon Sep 17 00:00:00 2001 From: Romain Murphy Date: Sat, 12 Apr 2025 12:35:30 +0200 Subject: [PATCH] =?UTF-8?q?bloc=20les=20possibilit=C3=A9s=20de=20mouvement?= =?UTF-8?q?=20et=20couleurs=20altern=C3=A9=20+=20selection=20des=20pions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OOP_2B1_Project/src/backend/Board.java | 76 ++++++++++--------- .../src/backend/PossibleMovements.java | 58 -------------- 2 files changed, 39 insertions(+), 95 deletions(-) delete mode 100644 OOP_2B1_Project/src/backend/PossibleMovements.java diff --git a/OOP_2B1_Project/src/backend/Board.java b/OOP_2B1_Project/src/backend/Board.java index 2564358..7713bc2 100644 --- a/OOP_2B1_Project/src/backend/Board.java +++ b/OOP_2B1_Project/src/backend/Board.java @@ -16,19 +16,19 @@ public class Board { this.width = colNum; this.height = lineNum; int rows = 8; - int cols = 8; - for (int i = 0; i < rows; i++) { - ArrayList row = new ArrayList<>(); - ArrayList rowB = new ArrayList<>(); - for (int j = 0; j < cols; j++) { - row.add(null); // Fill with null - rowB.add(false); - } - this.board.add(row); - this.possibleMoves.add(rowB); - } - this.turnNumber = 0; - this.turnColor = true; + int cols = 8; + for (int i = 0; i < rows; i++) { + ArrayList row = new ArrayList<>(); + ArrayList rowB = new ArrayList<>(); + for (int j = 0; j < cols; j++) { + row.add(null); // Fill with null + rowB.add(false); + } + this.board.add(row); + this.possibleMoves.add(rowB); + } + this.turnNumber = 0; + this.turnColor = true; } @@ -82,7 +82,7 @@ public class Board { } } } - + public void cleanBoard() { int rows = 8; int cols = 8; @@ -92,28 +92,28 @@ public class Board { } } } - + @Override public String toString() { return "Board [width=" + width + ", height=" + height + ", board=" + board + "]"; } - + public ArrayList getPieces() { ArrayList pieces = new ArrayList<>(); - for (ArrayList row : board) { - for (Piece piece : row) { - if (piece != null) { - pieces.add(piece); - } - } - } - return pieces; + for (ArrayList row : board) { + for (Piece piece : row) { + if (piece != null) { + pieces.add(piece); + } + } + } + return pieces; } - + public Piece getPiece(int x, int y) { return board.get(y).get(x); } - + public void movePiece(int x, int y) { if (select) { Piece pieceToMove = this.board.get(this.ym).get(this.xm); @@ -123,20 +123,22 @@ public class Board { } public void userTouch(int x, int y) { - if (this.select == false && board.get(y).get(x) != null) { + if ((this.select == false && board.get(y).get(x) != null && this.board.get(y).get(x).isWhite() == turnColor) || (select == true && board.get(y).get(x) != null && board.get(y).get(x).isWhite() == board.get(ym).get(xm).isWhite())) { this.xm = x; this.ym = y; select = true; possibleMoves = board.get(y).get(x).getPossibleMoves(board); } else if (select == true && this.xm != x || this.ym != y){ - this.movePiece(x, y); - select = false; - this.turnNumber += 1; - // System.out.println(this.toString()); // Debug - this.turnColor = !this.turnColor; + if (isHighlighted(x,y)) { + this.movePiece(x, y); + select = false; + this.turnNumber += 1; +// System.out.println(this.toString()); // Debug + this.turnColor = !this.turnColor; + } } else { -// System.out.println("a"); // debug +// System.out.println("a"); // Debug select = false; } } @@ -147,7 +149,7 @@ public class Board { else {S = false;} return S; } - + /* saving-loading feature :*/ public String[] toFileRep() { @@ -159,7 +161,7 @@ public class Board { //TODO } - + /* The following methods require more work ! */ public boolean isHighlighted(int x, int y) { @@ -173,14 +175,14 @@ public class Board { public void undoLastMove() { //TODO - + } public Board(Board board) { //TODO } - + public void playMove(Move move) { //TODO diff --git a/OOP_2B1_Project/src/backend/PossibleMovements.java b/OOP_2B1_Project/src/backend/PossibleMovements.java deleted file mode 100644 index c3c4708..0000000 --- a/OOP_2B1_Project/src/backend/PossibleMovements.java +++ /dev/null @@ -1,58 +0,0 @@ -package backend; - -import java.util.ArrayList; - -public class PossibleMovements { - ArrayList> board; - Piece pieceToMove; - PieceType type; - int x; - int y; - boolean turnColor; - public PossibleMovements(ArrayList> board, int x, int y,boolean turnColor) { - this.board = board; - this.pieceToMove = board.get(y).get(x); - this.type = pieceToMove.getType(); - this.x = x; - this.y = y; - this.turnColor = turnColor; - } - public ArrayList> PM(){ - ArrayList> possibleMoves = new ArrayList<>(); - int rows = 8; - int cols = 8; - for (int i = 0; i < rows; i++) { - ArrayList row = new ArrayList<>(); - for (int j = 0; j < cols; j++) { - row.add(false); // Fill with false - } - possibleMoves.add(row); - } - // Pawn movements for white - if (turnColor) { - if (type == PieceType.Pawn) { - if (y!= 0) { - if (x == 0) { - if (board.get(y-1).get(x+1) != null) { - possibleMoves.get(y-1).set(x+1, true); - } - } - else if (x==7) { - if (board.get(y-1).get(x-1) != null) { - possibleMoves.get(y-1).set(x-1, true); - } - } - else { - if (board.get(y-1).get(x-1) != null) { - possibleMoves.get(y-1).set(x-1, true); - } - if (board.get(y-1).get(x+1) != null) { - possibleMoves.get(y-1).set(x+1, true); - } - possibleMoves.get(y-1).set(x, true); - } - } - } - } - } -}