end lab 1

This commit is contained in:
MSI-AB\antoineB 2025-04-18 17:20:00 +02:00
parent 5fafd75e4f
commit 440a02ecd4
2 changed files with 72 additions and 26 deletions

View File

@ -10,12 +10,19 @@ public class Board {
private int turnNumber;
private boolean turnWhite;
public Board(int colNum, int lineNum) {
/*public Board(int colNum, int lineNum) {
this.width = colNum;
this.height = lineNum;
this.pieces = new ArrayList<>();
}
*/
public Board(int colNum, int lineNum) {
this.width = colNum;
this.height = lineNum;
this.pieces = new ArrayList<>();
this.turnNumber = 0;
this.turnWhite = true; // White starts first in chess
}
public int getWidth() {
return width;
@ -27,25 +34,53 @@ public class Board {
}
public int getTurnNumber() {
//TODO
return this.turnNumber;
}
public boolean isTurnWhite() {
//TODO
return this.turnWhite;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
//TODO
pieces.add(new Piece(isWhite, type, x, y));
}
public void populateBoard() {
//TODO
cleanBoard(); // make sure it's empty first
// Place white pawns
for (int x = 0; x < 8; x++) {
setPiece(true, PieceType.Pawn, x, 1);
}
// Place black pawns
for (int x = 0; x < 8; x++) {
setPiece(false, PieceType.Pawn, x, 6);
}
// Back rows (R, N, B, Q, K, B, N, R)
PieceType[] backRow = {
PieceType.Rook, PieceType.Knight, PieceType.Bishop, PieceType.Queen,
PieceType.King, PieceType.Bishop, PieceType.Knight, PieceType.Rook
};
// White back row
for (int x = 0; x < 8; x++) {
setPiece(true, backRow[x], x, 0);
}
// Black back row
for (int x = 0; x < 8; x++) {
setPiece(false, backRow[x], x, 7);
}
}
public void cleanBoard() {
//TODO
pieces.clear();
}
public String toString() {
@ -54,10 +89,7 @@ public class Board {
}
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
//TODO
return pieces;
return pieces; // this refers to the instance variable
}
public void userTouch(int x, int y) {

View File

@ -1,21 +1,35 @@
package backend;
public class Piece {
private boolean isWhite;
private PieceType type;
private int x;
private int y;
// Constructor
public Piece(boolean isWhite, PieceType type, int x, int y) {
this.isWhite = isWhite;
this.type = type;
this.x = x;
this.y = y;
}
// Accessors
public int getX() {
return 0;
return x;
}
public int getY() {
return 0;
return y;
}
public PieceType getType() {
return null;
return type;
}
public boolean isWhite() {
return false;
return isWhite;
}
}
}