set piece done

This commit is contained in:
hugomanipoud2 2025-05-15 18:55:51 +02:00
parent 6d5e762636
commit faa41e63cd
1 changed files with 22 additions and 11 deletions

View File

@ -3,9 +3,10 @@ package backend;
import java.util.ArrayList;
public class Board {
private int column;
private int line;
private ArrayList<Piece> pieces;
private int column; // constructors for column and line
private int line; // constructors for column and line
private ArrayList<Piece> pieces; // list of all pieces on the board
private Piece newPiece; // to add a piece to the board
//initiation of the board, creating an 8 * 8 grid
public Board(int colNum, int lineNum) {
@ -34,11 +35,15 @@ public class Board {
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
//TODO
// set the piece to the newPiece object made for this very purpose
this.newPiece = new Piece(x, y, type, isWhite);
// add newPiece (Piece object) to the arraylist for it to appear on the board
pieces.add(newPiece);
// reset newPiece for future selection of other pieces
newPiece = null ;
}
//to search how to implement pieces bc ???
public void populateBoard() {
//for loop avec les coordones de board attribuées via les coordonées des pieces ?
// pieces noires
pieces.add(new Piece(0, 0, PieceType.Rook, false));
pieces.add(new Piece(1, 0, PieceType.Knight, false));
@ -52,7 +57,6 @@ public class Board {
for (int i = 0; i <= 7; i++) {
pieces.add(new Piece(i, 1, PieceType.Pawn, false));
}
// blanches
pieces.add(new Piece(0, 7, PieceType.Rook, true));
pieces.add(new Piece(1, 7, PieceType.Knight, true));
@ -70,13 +74,17 @@ public class Board {
}
public void cleanBoard() {
//removes all pieces from arraylist pieces
pieces.clear();
}
@Override
public String toString() {
//initialize the str variable + black and white str
String boardState = "";
String bOrW = "" ;
//for loop that iterates through all the pieces in the arraylist pieces, adding conditions and simplifying the state of the board
for(Piece piece : pieces) {
if(piece.isWhite() == true) {
@ -89,14 +97,17 @@ public class Board {
String type = piece.getType().getSummary();
int xCord = piece.getX() + 1; //adding one to better understand the grid as a human, this will convert the 0-7 coord
int yCord = piece.getY() + 1; //system to a much more understandable 1-8 value range, which is better for an 8x8 grid
boardState += bOrW + "[" + type + "] (" + xCord + ";" + yCord + ")\n";
bOrW = "";
boardState += bOrW + "[" + type + "] (" + xCord + ";" + yCord + ")\n"; // str concatenation
bOrW = ""; //reinitialization of this local var bc white and black will pile up if not
}
return boardState;
return boardState; //gives back the full string
//TODO
//maybe i should add a flag variable to display 5 or 6 coordinates on the same line if i have time instead of 1 coord per line
}
public ArrayList<Piece> getPieces() {
//TODOa
//basic getter function
return pieces;
}