tostring now has a flag variable and is weel readable thanks to lines

skipped
This commit is contained in:
hugomanipoud2 2025-05-23 10:11:00 +02:00
parent 5e9ee6d35b
commit f3b9526521
1 changed files with 16 additions and 3 deletions

View File

@ -103,24 +103,37 @@ public class Board {
//initialize the str variable + black and white str //initialize the str variable + black and white str
String boardState = ""; String boardState = "";
String bOrW = "" ; String bOrW = "" ;
int pieceCount = 0 ;
//initialize the str variable + black and white str //initialize the str variable + black and white str
//for loop that iterates through all the pieces in the arraylist pieces, adding conditions and simplifying the state of the board //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) { for(Piece piece : pieces) {
if(piece.isWhite() == true) { if(piece.isWhite() == true) {
bOrW += "White"; bOrW += "w";
} }
else { else {
bOrW += "Black"; bOrW += "b";
} }
//gathering all other piece data //gathering all other piece data
String type = piece.getType().getSummary(); 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 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 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"; // str concatenation boardState += bOrW + "[" + type + "] (" + xCord + ";" + yCord + ")"; // str concatenation
pieceCount ++;
if (pieceCount % 8 == 0) {
boardState += "\n"; //add a new line
} else {
boardState += " "; //add space if not divsisble by 8
}
bOrW = ""; //reinitialization of this local var bc white and black will pile up if not bOrW = ""; //reinitialization of this local var bc white and black will pile up if not
} }
if (pieceCount % 8 != 0) {
boardState += "\n"; // skip a line at the very end if piececount isnt modulo 8
}
return boardState; //gives back the full string return boardState; //gives back the full string
//TODO //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 //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