working board

This commit is contained in:
utente 2025-04-15 11:40:28 +02:00
parent 487cd37be6
commit b4a48f6155
2 changed files with 160 additions and 78 deletions

View File

@ -1,86 +1,88 @@
package backend;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Board {
private int colNum;
private int lineNum;
private Piece[][] board;
private int turnNumber = 0;
private boolean isWhiteTurn = true;
private Piece selectedPiece = null;
private Set<String> highlightedPositions = new HashSet<>();
public Board(int colNum, int lineNum) {
this.colNum = colNum;
this.lineNum = lineNum;
this.board = new Piece[lineNum][colNum];
}
public int getWidth() {
return colNum;
public int getWidth() {
return colNum;
}
public int getHeight() {
return lineNum;
public int getHeight() {
return lineNum;
}
public int getTurnNumber() {
return turnNumber;
}
public int getTurnNumber() { return turnNumber; }
public boolean isTurnWhite() {
return isWhiteTurn;
}
public boolean isTurnWhite() { return isWhiteTurn; }
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
board[y][x] = new Piece(x,y,type, isWhite);
board[y][x] = new Piece(x, y, type, isWhite);
}
public void populateBoard() {
cleanBoard(); // Start with an empty board
// Pawns
cleanBoard();
for (int x = 0; x < getWidth(); x++) {
setPiece(false, PieceType.Pawn, x, 1); // Black pawns
setPiece(true, PieceType.Pawn, x, 6); // White pawns
setPiece(false, PieceType.Pawn, x, 1);
setPiece(true, PieceType.Pawn, x, 6);
}
// Rooks
setPiece(false, PieceType.Rook, 0, 0);
setPiece(false, PieceType.Rook, 7, 0);
setPiece(true, PieceType.Rook, 0, 7);
setPiece(true, PieceType.Rook, 7, 7);
// Knights
setPiece(false, PieceType.Knight, 1, 0);
setPiece(false, PieceType.Knight, 6, 0);
setPiece(true, PieceType.Knight, 1, 7);
setPiece(true, PieceType.Knight, 6, 7);
// Bishops
setPiece(false, PieceType.Bishop, 2, 0);
setPiece(false, PieceType.Bishop, 5, 0);
setPiece(true, PieceType.Bishop, 2, 7);
setPiece(true, PieceType.Bishop, 5, 7);
// Queens
setPiece(false, PieceType.Queen, 3, 0);
setPiece(true, PieceType.Queen, 3, 7);
// Kings
setPiece(false, PieceType.King, 4, 0);
setPiece(true, PieceType.King, 4, 7);
}
}
public void cleanBoard() {
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
board[y][x] = null;
}
}
}
selectedPiece = null;
highlightedPositions.clear();
}
@Override
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
if (board[y][x] != null) {
pieces.add(board[y][x]);
}
}
}
return pieces;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int y = 0; y < getHeight(); y++) {
@ -97,61 +99,146 @@ public class Board {
}
return sb.toString();
}
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
if (board[y][x] != null) {
pieces.add(board[y][x]);
}
}
}
return pieces;
}
public void userTouch(int x, int y) {
//TODO
if (selectedPiece == null) {
Piece p = board[y][x];
if (p != null && p.isWhite() == isWhiteTurn) {
selectedPiece = p;
highlightedPositions = getValidMoves(p);
}
} else {
if (highlightedPositions.contains(x + "," + y)) {
movePiece(selectedPiece.getX(), selectedPiece.getY(), x, y);
isWhiteTurn = !isWhiteTurn;
turnNumber++;
}
selectedPiece = null;
highlightedPositions.clear();
}
}
public boolean isSelected(int x, int y) {
//TODO
return false;
return selectedPiece != null && selectedPiece.getX() == x && selectedPiece.getY() == y;
}
/* saving-loading feature :*/
public String[] toFileRep() {
//TODO
return null;
}
public Board(String[] array) {
//TODO
}
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
//TODO
return false;
return highlightedPositions.contains(x + "," + y);
}
public void undoLastMove() {
//TODO
private void movePiece(int fromX, int fromY, int toX, int toY) {
Piece moving = board[fromY][fromX];
board[toY][toX] = new Piece(toX, toY, moving.getType(), moving.isWhite());
board[fromY][fromX] = null;
}
public Board(Board board) {
//TODO
private Set<String> getValidMoves(Piece p) {
Set<String> valid = new HashSet<>();
int x = p.getX();
int y = p.getY();
PieceType type = p.getType();
boolean isWhite = p.isWhite();
}
public void playMove(Move move) {
//TODO
int dir = isWhite ? -1 : 1;
switch (type) {
case Pawn:
if (inBounds(x, y + dir) && board[y + dir][x] == null) {
valid.add(x + "," + (y + dir));
}
// First move 2-steps
if ((isWhite && y == 6) || (!isWhite && y == 1)) {
if (board[y + dir][x] == null && board[y + 2 * dir][x] == null) {
valid.add(x + "," + (y + 2 * dir));
}
}
// Capture diagonally
if (inBounds(x - 1, y + dir) && board[y + dir][x - 1] != null && board[y + dir][x - 1].isWhite() != isWhite)
valid.add((x - 1) + "," + (y + dir));
if (inBounds(x + 1, y + dir) && board[y + dir][x + 1] != null && board[y + dir][x + 1].isWhite() != isWhite)
valid.add((x + 1) + "," + (y + dir));
break;
case Rook:
addLinearMoves(valid, x, y, isWhite, 1, 0);
addLinearMoves(valid, x, y, isWhite, -1, 0);
addLinearMoves(valid, x, y, isWhite, 0, 1);
addLinearMoves(valid, x, y, isWhite, 0, -1);
break;
case Bishop:
addLinearMoves(valid, x, y, isWhite, 1, 1);
addLinearMoves(valid, x, y, isWhite, 1, -1);
addLinearMoves(valid, x, y, isWhite, -1, 1);
addLinearMoves(valid, x, y, isWhite, -1, -1);
break;
case Queen:
addLinearMoves(valid, x, y, isWhite, 1, 0);
addLinearMoves(valid, x, y, isWhite, -1, 0);
addLinearMoves(valid, x, y, isWhite, 0, 1);
addLinearMoves(valid, x, y, isWhite, 0, -1);
addLinearMoves(valid, x, y, isWhite, 1, 1);
addLinearMoves(valid, x, y, isWhite, 1, -1);
addLinearMoves(valid, x, y, isWhite, -1, 1);
addLinearMoves(valid, x, y, isWhite, -1, -1);
break;
case Knight:
int[][] knightMoves = {
{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
{1, -2}, {2, -1}, {-1, -2}, {-2, -1}
};
for (int[] m : knightMoves) {
int nx = x + m[0];
int ny = y + m[1];
if (inBounds(nx, ny) && (board[ny][nx] == null || board[ny][nx].isWhite() != isWhite)) {
valid.add(nx + "," + ny);
}
}
break;
case King:
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue;
int nx = x + dx, ny = y + dy;
if (inBounds(nx, ny) && (board[ny][nx] == null || board[ny][nx].isWhite() != isWhite)) {
valid.add(nx + "," + ny);
}
}
}
break;
}
return valid;
}
private void addLinearMoves(Set<String> valid, int x, int y, boolean isWhite, int dx, int dy) {
int nx = x + dx;
int ny = y + dy;
while (inBounds(nx, ny)) {
if (board[ny][nx] == null) {
valid.add(nx + "," + ny);
} else {
if (board[ny][nx].isWhite() != isWhite) {
valid.add(nx + "," + ny);
}
break;
}
nx += dx;
ny += dy;
}
}
private boolean inBounds(int x, int y) {
return x >= 0 && y >= 0 && x < colNum && y < lineNum;
}
// other methods not changed
public String[] toFileRep() { return null; }
public Board(String[] array) {}
public void undoLastMove() {}
public Board(Board board) {}
public void playMove(Move move) {}
}

View File

@ -20,9 +20,4 @@ public enum PieceType {
default: return Queen;
}
}
// New method to generate image path
public String getImagePath(boolean isWhite) {
return "/Chess_pieces/" + this.name() + "_" + (isWhite ? "white" : "black") + ".png";
}
}