populate board and cleaned the code (pieces exist but are not displayed)

This commit is contained in:
Alexandre ALTARIBA 2025-04-20 18:53:35 +02:00
parent cabf493202
commit cd59243c01
3 changed files with 58 additions and 31 deletions

View File

@ -4,14 +4,14 @@ import java.util.ArrayList;
public class Board {
private int cNum;
private int lNum;
private Piece newPiece;
private Piece[][]Board;
private int cNum; // nb de colonnes du damier
private int lNum; // nb de ligne du damier
private Piece[][] cells;
public Board(int colNum, int lineNum) {
cNum=colNum;
lNum=lineNum;
cells = new Piece[cNum][lNum]; // creation du tableau de pieces
}
public int getWidth() {
@ -40,37 +40,56 @@ public class Board {
}
public void setPiece(PieceType type, boolean isWhite, int x, int y) {
this.newPiece= new Piece(x,y,type,isWhite);
Piece newPiece= new Piece(type,isWhite,x,y);
cells[x][y] = newPiece;
}
public void populateBoard() {
//Black pieces populating
Board[1][1] = new Piece(1,1,PieceType.Rook,false);
Board[2][1] = new Piece(2,1,PieceType.Knight,false);
Board[3][1] = new Piece(3,1,PieceType.Bishop,false);
Board[4][1] = new Piece(4,1,PieceType.Queen,false);
Board[7][1] = new Piece(5,1,PieceType.King,false);
Board[6][1] = new Piece(6,1,PieceType.Bishop,false);
Board[8][1] = new Piece(7,1,PieceType.Knight,false);
Board[9][1] = new Piece(8,1,PieceType.Rook,false);
final int startWhite = 0;
final int startBlack = lNum-1;
for (int x = 1; x < 8; x++) {
Board[x][2] = new Piece(x,2,PieceType.Pawn,false);
//Black pieces populating
setPiece(PieceType.Rook,false,0,startWhite);
setPiece(PieceType.Knight,false, 1,startWhite);
setPiece(PieceType.Bishop,false,2,startWhite);
setPiece(PieceType.Queen,false,3,startWhite);
setPiece(PieceType.King,false,4,startWhite);
setPiece(PieceType.Bishop,false,5,startWhite);
setPiece(PieceType.Knight,false,6,startWhite);
setPiece(PieceType.Rook,false,7,startWhite);
for (int x = 0; x < cNum; x++) {
setPiece(PieceType.Pawn,false,x,startWhite+1);
}
//White pieces populating
Board[1][8] = new Piece(1,8,PieceType.Rook,true);
Board[2][8] = new Piece(2,8,PieceType.Knight,true);
Board[3][8] = new Piece(3,8,PieceType.Bishop,true);
Board[4][8] = new Piece(4,8,PieceType.Queen,true);
Board[7][8] = new Piece(5,8,PieceType.King,true);
Board[6][8] = new Piece(6,8,PieceType.Bishop,true);
Board[8][8] = new Piece(7,8,PieceType.Knight,true);
Board[9][8] = new Piece(8,8,PieceType.Rook,true);
setPiece(PieceType.Rook,true,0,startBlack);
setPiece(PieceType.Knight,true,1,startBlack);
setPiece(PieceType.Bishop,true,2,startBlack);
setPiece(PieceType.Queen,true,3,startBlack);
setPiece(PieceType.King,true,4,startBlack);
setPiece(PieceType.Bishop,true,5,startBlack);
setPiece(PieceType.Knight,true,6,startBlack);
setPiece(PieceType.Rook,true,7,startBlack);
for (int x = 1; x < 8; x++) {
for (int x = 0; x < cNum; x++) {
Board[x][7] = new Piece(x,7,PieceType.Pawn,true);
setPiece(PieceType.Pawn,true,x,startBlack-1);
}
for (int x = 0; x < cNum; x++) {
for(int y=0;y<lNum;y++) {
if (cells[x][y]==null) {
System.out.print("blank - ");
}
else {
System.out.print(cells[x][y].getName());
}
}
System.out.print("\r\n");
}
}
@ -78,7 +97,7 @@ public class Board {
for (int y = 1; y < 7; y++) {
for (int x = 1; x < 7; x++) {
Board[x][y] = null;
cells[x][y] = null;
}
}
}
@ -93,8 +112,8 @@ public class Board {
for (int y = 1; y < 7; y++) {
for (int x = 1; x < 7; x++) {
if (Board[x][y] != null) {
pieces.add(Board[x][y]);
if (this.cNum==0 && this.lNum==0) {
pieces.add(cells[x][y]);
}
}
}

View File

@ -33,7 +33,7 @@ public class Game extends Thread {
}
public void setPiece(PieceType type, boolean isWhite, int x, int y) {
this.newPiece= new Piece(x,y,type,isWhite);
this.newPiece= new Piece(type,isWhite, x,y);
}
public void run() {

View File

@ -7,7 +7,7 @@ public class Piece {
private PieceType type;
private boolean color;
public Piece(int xP, int yP, PieceType type_P, boolean color_P) {
public Piece(PieceType type_P, boolean color_P, int xP, int yP) {
x = xP;
y = yP;
type = type_P;
@ -29,5 +29,13 @@ public class Piece {
public boolean isWhite() {
return color;
}
public String getName() {
if(color) {
return type.getSummary().concat("W - ");
}
else {
return type.getSummary().concat("B - ");
}
}
}