For highlight: FIlled in hihglight method, added a Position class,

findMoves method (gives all possible cases), extend method (for rook,
queen, and bishop), updated user touch (to show the highlight)
This commit is contained in:
Juliette 2025-05-13 19:24:42 +02:00
parent 95aa5f0c34
commit b12735869a
2 changed files with 290 additions and 202 deletions

View File

@ -6,4 +6,4 @@ BP,BP,BP,BP,BP,BP,BP,BP
, , , , , , , , , , , , , ,
WP,WP,WP,WP,WP,WP,WP,WP WP,WP,WP,WP,WP,WP,WP,WP
WR,WN,WB,WQ,WK,WB,WN,WR WR,WN,WB,WQ,WK,WB,WN,WR
W

View File

@ -2,233 +2,321 @@ package backend;
import java.util.ArrayList; import java.util.ArrayList;
public class Board { public class Board {
private int colNum;
private int lineNum;
public String board[][];
private PieceType type;
private ArrayList<Piece> pieces;
private ArrayList<Position> highlight = new ArrayList<>();
private int selectedX = -1; // Tracks selected column (-1 = no selection)
private int selectedY = -1; // Tracks selected row (-1 = no selection)
private int turnNumber = 0; // Starts at 0 (White's turn)
private boolean isWhiteTurn = true; // Track current player's turn
private int colNum; private char getPieceSymbol(Piece p) {
private int lineNum; char symbol;
public String board[][]; switch (p.getType()) {
private PieceType type; case Pawn: symbol = 'P'; break;
private ArrayList<Piece> pieces; case Rook: symbol = 'R'; break;
private int selectedX = -1; // Tracks selected column (-1 = no selection) case Knight: symbol = 'N'; break;
private int selectedY = -1; // Tracks selected row (-1 = no selection) case Bishop: symbol = 'B'; break;
private int turnNumber = 0; // Starts at 0 (White's turn) case Queen: symbol = 'Q'; break;
private boolean isWhiteTurn = true; // Track current player's turn case King: symbol = 'K'; break;
default: symbol = '?'; // Should never happen
}
return p.isWhite() ? symbol : Character.toLowerCase(symbol);
}
public Board(int colNum, int lineNum) { private Piece getPieceAt(int x, int y) {
this.colNum = colNum; for (Piece p : pieces) {
this.lineNum = lineNum; if (p.getX() == x && p.getY() == y) return p;
this.pieces = new ArrayList<>(); }
this.turnNumber = 0; // Initialize turn number return null;
this.isWhiteTurn = true; // White starts first }
}
public int getWidth() { private static class Position {
//System.out.println("The width is " + lineNum); int x, y;
return lineNum; Position(int x, int y) {
} this.x = x;
this.y = y;
}
}
public int getHeight() { public Board(int colNum, int lineNum) {
//System.out.println("The height is " + colNum); this.colNum = colNum;
return colNum; this.lineNum = lineNum;
} this.pieces = new ArrayList<>();
this.turnNumber = 0; // Initialize turn number
this.isWhiteTurn = true; // White starts first
}
public int getTurnNumber() { public int getWidth() {
return turnNumber; return lineNum;
} }
public boolean isTurnWhite() { public int getHeight() {
return isWhiteTurn; return colNum;
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public int getTurnNumber() {
// Remove existing piece at coordinates (x,y) return turnNumber;
}
public boolean isTurnWhite() {
return isWhiteTurn;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
pieces.removeIf(p -> p.getX() == x && p.getY() == y); pieces.removeIf(p -> p.getX() == x && p.getY() == y);
// Add new piece
pieces.add(new Piece(isWhite, type, x, y)); pieces.add(new Piece(isWhite, type, x, y));
//TODO }
}
public void populateBoard() {
; for (int x = 0; x < 8; x++) {
setPiece(false, PieceType.Pawn, x, 1);
public void populateBoard() { setPiece(true, PieceType.Pawn, x, 6);
for(int x=0; x<8;x++) { }
setPiece(false,PieceType.Pawn,x,1); setPiece(false, PieceType.Rook, 0, 0);
setPiece(true,PieceType.Pawn,x,6); setPiece(false, PieceType.Rook, 7, 0);
} setPiece(true, PieceType.Rook, 0, 7);
setPiece(false,PieceType.Rook,0,0); setPiece(true, PieceType.Rook, 7, 7);
setPiece(false,PieceType.Rook,7,0);
setPiece(true,PieceType.Rook,0,7);
setPiece(true,PieceType.Rook,7,7);
setPiece(false,PieceType.Knight,1,0);
setPiece(false,PieceType.Knight,6,0);
setPiece(true,PieceType.Knight,1,7);
setPiece(true,PieceType.Knight,6,7);
setPiece(false,PieceType.Bishop,2,0);
setPiece(false,PieceType.Bishop,5,0);
setPiece(true,PieceType.Bishop,2,7);
setPiece(true,PieceType.Bishop,5,7);
setPiece(false,PieceType.Queen,3,0);
setPiece(true,PieceType.Queen,3,7);
setPiece(false,PieceType.King,4,0);
setPiece(true,PieceType.King,4,7);
}
public void cleanBoard() {
pieces.clear(); // Remove all pieces from the board
//TODO
}
public String toString() {
StringBuilder sb = new StringBuilder();
// Create a temporary grid representation
char[][] grid = new char[lineNum][colNum];
// Initialize grid with empty squares
for (int y = 0; y < lineNum; y++) {
for (int x = 0; x < colNum; x++) {
grid[y][x] = '.';
}
}
// Fill pieces into the grid
for (Piece p : pieces) {
int x = p.getX();
int y = p.getY();
if (x >= 0 && x < colNum && y >= 0 && y < lineNum) {
grid[y][x] = getPieceSymbol(p);
}
}
// Build header setPiece(false, PieceType.Knight, 1, 0);
sb.append(" "); setPiece(false, PieceType.Knight, 6, 0);
for (int x = 0; x < colNum; x++) { setPiece(true, PieceType.Knight, 1, 7);
sb.append((char) ('a' + x)).append(' '); setPiece(true, PieceType.Knight, 6, 7);
}
sb.append('\n');
// Build board rows (top to bottom) setPiece(false, PieceType.Bishop, 2, 0);
for (int y = 0; y < lineNum; y++) { setPiece(false, PieceType.Bishop, 5, 0);
sb.append(lineNum - y).append(' '); setPiece(true, PieceType.Bishop, 2, 7);
for (int x = 0; x < colNum; x++) { setPiece(true, PieceType.Bishop, 5, 7);
sb.append(grid[y][x]).append(' ');
}
sb.append('\n');
}
return sb.toString(); setPiece(false, PieceType.Queen, 3, 0);
} setPiece(true, PieceType.Queen, 3, 7);
// Helper method to convert Piece to symbol (e.g., 'K' for white king)
private char getPieceSymbol(Piece p) {
char symbol;
switch (p.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 = '?'; // Should never happen
}
return p.isWhite() ? symbol : Character.toLowerCase(symbol);
}
public ArrayList<Piece> getPieces() {
return pieces;
}
public void userTouch(int x, int y) { setPiece(false, PieceType.King, 4, 0);
if (selectedX == -1) { // No current selection setPiece(true, PieceType.King, 4, 7);
// Check if there's a piece at the clicked position }
for (Piece p : pieces) {
if (p.getX() == x && p.getY() == y) {
// Only allow selection if it's the correct player's turn
if (p.isWhite() == isWhiteTurn) {
selectedX = x;
selectedY = y;
}
return;
}
}
} else { // Already have a selected piece
if (x == selectedX && y == selectedY) { // Clicked same position: unselect
selectedX = -1;
selectedY = -1;
} else { // Attempt to move
Piece pieceToMove = null;
// Find the selected piece
for (Piece p : pieces) {
if (p.getX() == selectedX && p.getY() == selectedY) {
pieceToMove = p;
break;
}
}
if (pieceToMove != null) { public void cleanBoard() {
// Remove any piece at target location pieces.clear(); // Remove all pieces from the board
pieces.removeIf(p -> p.getX() == x && p.getY() == y); }
// Update piece position
pieceToMove.setX(x);
pieceToMove.setY(y);
// Advance game state
turnNumber++;
isWhiteTurn = !isWhiteTurn;
}
// Clear selection regardless of move success
selectedX = -1;
selectedY = -1;
}
}
}
public boolean isSelected(int x, int y) { public String toString() {
return (x == selectedX && y == selectedY); StringBuilder sb = new StringBuilder();
} char[][] grid = new char[lineNum][colNum];
for (int y = 0; y < lineNum; y++) {
/* saving-loading feature :*/ for (int x = 0; x < colNum; x++) {
grid[y][x] = '.';
}
}
public String[] toFileRep() { for (Piece p : pieces) {
//TODO int x = p.getX();
return null; int y = p.getY();
} if (x >= 0 && x < colNum && y >= 0 && y < lineNum) {
grid[y][x] = getPieceSymbol(p);
}
}
public Board(String[] array) { sb.append(" ");
//TODO for (int x = 0; x < colNum; x++) {
sb.append((char) ('a' + x)).append(' ');
}
sb.append('\n');
} for (int y = 0; y < lineNum; y++) {
sb.append(lineNum - y).append(' ');
/* The following methods require more work ! */ for (int x = 0; x < colNum; x++) {
sb.append(grid[y][x]).append(' ');
}
sb.append('\n');
}
public boolean isHighlighted(int x, int y) { return sb.toString();
//TODO }
return false;
}
public void undoLastMove() { public ArrayList<Piece> getPieces() {
//TODO return pieces;
}
}
public Board(Board board) { public void userTouch(int x, int y) {
//TODO if (selectedX != -1 && x >= 0 && x < colNum && y >= 0 && y < lineNum && isHighlighted(x, y)) {
Piece selectedPiece = getPieceAt(selectedX, selectedY);
if (selectedPiece != null) {
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
selectedPiece.setX(x);
selectedPiece.setY(y);
} turnNumber++;
isWhiteTurn = !isWhiteTurn;
public void playMove(Move move) { }
//TODO
} selectedX = -1;
selectedY = -1;
highlight.clear();
return;
}
Piece p = getPieceAt(x, y);
if (p != null && p.isWhite() == isWhiteTurn) {
selectedX = x;
selectedY = y;
highlight.clear();
highlight.addAll(getValidMoves(p));
} else {
selectedX = -1;
selectedY = -1;
highlight.clear();
}
}
public boolean isSelected(int x, int y) {
return (x == selectedX && y == selectedY);
}
public String[] toFileRep() {
//TODO
return null;
}
public Board(String[] array) {
//TODO
}
public boolean isHighlighted(int x, int y) {
for (Position pos : highlight) {
if (pos.x == x && pos.y == y) {
return true;
}
}
return false;
}
public void undoLastMove() {
//TODO
}
public Board(Board board) {
//TODO
}
public void playMove(Move move) {
//TODO
}
private ArrayList<Position> getValidMoves(Piece p) {
ArrayList<Position> moves = new ArrayList<>();
int x = p.getX();
int y = p.getY();
boolean isWhite = p.isWhite();
if (p.getType() == PieceType.Pawn) {
int direction = isWhite ? -1 : 1;
int startRow = isWhite ? 6 : 1;
if (x >= 0 && x < colNum && y + direction >= 0 && y + direction < lineNum &&
getPieceAt(x, y + direction) == null) {
moves.add(new Position(x, y + direction));
if (y == startRow && y + 2 * direction >= 0 && y + 2 * direction < lineNum &&
getPieceAt(x, y + 2 * direction) == null) {
moves.add(new Position(x, y + 2 * direction));
}
}
for (int dx = -1; dx <= 1; dx += 2) {
int nx = x + dx;
int ny = y + direction;
if (nx >= 0 && nx < colNum && ny >= 0 && ny < lineNum) {
Piece target = getPieceAt(nx, ny);
if (target != null && target.isWhite() != isWhite) {
moves.add(new Position(nx, ny));
}
}
}
}
if (p.getType() == PieceType.Knight) {
int[][] jumps = {
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
{1, -2}, {1, 2}, {2, -1}, {2, 1}
};
for (int[] j : jumps) {
int nx = x + j[0];
int ny = y + j[1];
if (nx >= 0 && nx < colNum && ny >= 0 && ny < lineNum) {
Piece target = getPieceAt(nx, ny);
if (target == null || target.isWhite() != isWhite) {
moves.add(new Position(nx, ny));
}
}
}
}
if (p.getType() == PieceType.Rook) {
addMovesInLine(moves, x, y, isWhite, 1, 0);
addMovesInLine(moves, x, y, isWhite, -1, 0);
addMovesInLine(moves, x, y, isWhite, 0, 1);
addMovesInLine(moves, x, y, isWhite, 0, -1);
}
if (p.getType() == PieceType.Bishop) {
addMovesInLine(moves, x, y, isWhite, 1, 1);
addMovesInLine(moves, x, y, isWhite, -1, 1);
addMovesInLine(moves, x, y, isWhite, 1, -1);
addMovesInLine(moves, x, y, isWhite, -1, -1);
}
if (p.getType() == PieceType.Queen) {
addMovesInLine(moves, x, y, isWhite, 1, 0);
addMovesInLine(moves, x, y, isWhite, -1, 0);
addMovesInLine(moves, x, y, isWhite, 0, 1);
addMovesInLine(moves, x, y, isWhite, 0, -1);
addMovesInLine(moves, x, y, isWhite, 1, 1);
addMovesInLine(moves, x, y, isWhite, -1, 1);
addMovesInLine(moves, x, y, isWhite, 1, -1);
addMovesInLine(moves, x, y, isWhite, -1, -1);
}
if (p.getType() == PieceType.King) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < colNum && ny >= 0 && ny < lineNum) {
Piece target = getPieceAt(nx, ny);
if (target == null || target.isWhite() != isWhite) {
moves.add(new Position(nx, ny));
}
}
}
}
}
return moves;
}
private void addMovesInLine(ArrayList<Position> moves, int x, int y, boolean isWhite, int dx, int dy) {
int nx = x + dx;
int ny = y + dy;
while (nx >= 0 && nx < colNum && ny >= 0 && ny < lineNum) {
Piece target = getPieceAt(nx, ny);
if (target == null) {
moves.add(new Position(nx, ny));
} else if (target.isWhite() != isWhite) {
moves.add(new Position(nx, ny));
break;
} else {
break;
}
nx += dx;
ny += dy;
}
}
} }