This commit is contained in:
louis.flichy 2025-04-15 10:10:13 +02:00
parent 6d42ba903a
commit d3843705e4
1 changed files with 140 additions and 12 deletions

View File

@ -7,13 +7,28 @@ public class Board {
//Board dimensions //Board dimensions
private int width; private int width;
private int height; private int height;
private ArrayList<Piece> pieces = new ArrayList<>();
private int turnNumber = 0;
private boolean isTurnWhite = true;
private int selectedX = -1;
private int selectedY = -1;
private Piece getPieceAt(int x, int y) {
for (Piece piece : pieces) {
if (piece.getX() == x && piece.getY() == y) {
return piece;
}
}
return null;
}
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
this.height = lineNum; this.height = lineNum;
//TODO this.turnNumber = 0;
this.isTurnWhite = true;
} }
public int getWidth() { public int getWidth() {
//TODO //TODO
return width; return width;
@ -25,47 +40,160 @@ public class Board {
} }
public int getTurnNumber() { public int getTurnNumber() {
return turnNumber;
//TODO //TODO
return 0;
} }
public boolean isTurnWhite() { public boolean isTurnWhite() {
return isTurnWhite;
//TODO //TODO
return false;
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height) {
return;
}
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
if (piece.getX() == x && piece.getY() == y) {
pieces.remove(i);
break;
}
}
pieces.add(new Piece(x, y, type, isWhite));
//TODO //TODO
} }
public void populateBoard() { public void populateBoard() {
cleanBoard();
for (int x = 0; x < getWidth(); x++) {
setPiece(true, PieceType.Pawn, x, 6);
setPiece(false, PieceType.Pawn, x, 1);
}
setPiece(true, PieceType.Rook, 0, 7);
setPiece(true, PieceType.Rook, 7, 7);
setPiece(false, PieceType.Rook, 0, 0);
setPiece(false, PieceType.Rook, 7, 0);
setPiece(true, PieceType.Knight, 1, 7);
setPiece(true, PieceType.Knight, 6, 7);
setPiece(false, PieceType.Knight, 1, 0);
setPiece(false, PieceType.Knight, 6, 0);
setPiece(true, PieceType.Bishop, 2, 7);
setPiece(true, PieceType.Bishop, 5, 7);
setPiece(false, PieceType.Bishop, 2, 0);
setPiece(false, PieceType.Bishop, 5, 0);
setPiece(true, PieceType.Queen, 3, 7);
setPiece(false, PieceType.Queen, 3, 0);
setPiece(true, PieceType.King, 4, 7);
setPiece(false, PieceType.King, 4, 0);
//TODO //TODO
} }
public void cleanBoard() { public void cleanBoard() {
pieces.clear();
//TODO //TODO
} }
public String toString() { public String toString() {
StringBuilder result = new StringBuilder();
char[][] board = new char[height][width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
board[y][x] = '.';
}
}
for (Piece piece : pieces) {
char symbol;
switch (piece.getType()) {
case Pawn: symbol = 'P'; break;
case Rook: symbol = 'R'; break;
case Knight: symbol = 'N'; break;
case Bishop: symbol = 'B'; break;
case Queen: symbol = 'Q'; break;
case King: symbol = 'K'; break;
default: symbol = '?';
}
if (!piece.isWhite()) {
symbol = Character.toLowerCase(symbol);
}
board[piece.getY()][piece.getX()] = symbol;
}
for (int y = 0; y < height; y++) {
result.append(8 - y).append(" ");
for (int x = 0; x < width; x++) {
result.append(board[y][x]).append(" ");
}
result.append("\n");
}
result.append(" ");
for (int x = 0; x < width; x++) {
result.append((char)('a' + x)).append(" ");
}
return result.toString();
//TODO //TODO
return "";
} }
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); return new ArrayList<>(pieces);
//TODO
return pieces;
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
//TODO if (x < 0 || x >= width || y < 0 || y >= height) {
return;
}
if (selectedX == -1 && selectedY == -1) {
Piece touchedPiece = getPieceAt(x, y);
if (touchedPiece != null && touchedPiece.isWhite() == isTurnWhite) {
selectedX = x;
selectedY = y;
}
}
else {
if (selectedX == x && selectedY == y) {
selectedX = -1;
selectedY = -1;
}
else {
Piece selectedPiece = getPieceAt(selectedX, selectedY);
if (selectedPiece != null) {
setPiece(selectedPiece.isWhite(), selectedPiece.getType(), x, y);
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
if (piece.getX() == selectedX && piece.getY() == selectedY) {
pieces.remove(i);
break;
}
}
turnNumber++;
isTurnWhite = !isTurnWhite;
selectedX = -1;
selectedY = -1;
}
}
}
} }
//TODO
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
return selectedX == x && selectedY == y;
//TODO //TODO
return false;
} }
/* saving-loading feature :*/ /* saving-loading feature :*/