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,36 +2,61 @@ package backend;
import java.util.ArrayList; import java.util.ArrayList;
public class Board { public class Board {
private int colNum; private int colNum;
private int lineNum; private int lineNum;
public String board[][]; public String board[][];
private PieceType type; private PieceType type;
private ArrayList<Piece> pieces; private ArrayList<Piece> pieces;
private ArrayList<Position> highlight = new ArrayList<>();
private int selectedX = -1; // Tracks selected column (-1 = no selection) private int selectedX = -1; // Tracks selected column (-1 = no selection)
private int selectedY = -1; // Tracks selected row (-1 = no selection) private int selectedY = -1; // Tracks selected row (-1 = no selection)
private int turnNumber = 0; // Starts at 0 (White's turn) private int turnNumber = 0; // Starts at 0 (White's turn)
private boolean isWhiteTurn = true; // Track current player's turn private boolean isWhiteTurn = true; // Track current player's turn
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);
}
private Piece getPieceAt(int x, int y) {
for (Piece p : pieces) {
if (p.getX() == x && p.getY() == y) return p;
}
return null;
}
private static class Position {
int x, y;
Position(int x, int y) {
this.x = x;
this.y = y;
}
}
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.colNum = colNum; this.colNum = colNum;
this.lineNum = lineNum; this.lineNum = lineNum;
this.pieces = new ArrayList<>(); this.pieces = new ArrayList<>();
this.turnNumber = 0; // Initialize turn number this.turnNumber = 0; // Initialize turn number
this.isWhiteTurn = true; // White starts first this.isWhiteTurn = true; // White starts first
} }
public int getWidth() { public int getWidth() {
//System.out.println("The width is " + lineNum);
return lineNum; return lineNum;
} }
public int getHeight() { public int getHeight() {
//System.out.println("The height is " + colNum);
return colNum; return colNum;
} }
@ -44,16 +69,10 @@ public class Board {
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
// Remove existing piece at coordinates (x,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() { public void populateBoard() {
for (int x = 0; x < 8; x++) { for (int x = 0; x < 8; x++) {
setPiece(false, PieceType.Pawn, x, 1); setPiece(false, PieceType.Pawn, x, 1);
@ -83,22 +102,18 @@ public class Board {
public void cleanBoard() { public void cleanBoard() {
pieces.clear(); // Remove all pieces from the board pieces.clear(); // Remove all pieces from the board
//TODO
} }
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// Create a temporary grid representation
char[][] grid = new char[lineNum][colNum]; char[][] grid = new char[lineNum][colNum];
// Initialize grid with empty squares
for (int y = 0; y < lineNum; y++) { for (int y = 0; y < lineNum; y++) {
for (int x = 0; x < colNum; x++) { for (int x = 0; x < colNum; x++) {
grid[y][x] = '.'; grid[y][x] = '.';
} }
} }
// Fill pieces into the grid
for (Piece p : pieces) { for (Piece p : pieces) {
int x = p.getX(); int x = p.getX();
int y = p.getY(); int y = p.getY();
@ -107,14 +122,12 @@ public class Board {
} }
} }
// Build header
sb.append(" "); sb.append(" ");
for (int x = 0; x < colNum; x++) { for (int x = 0; x < colNum; x++) {
sb.append((char) ('a' + x)).append(' '); sb.append((char) ('a' + x)).append(' ');
} }
sb.append('\n'); sb.append('\n');
// Build board rows (top to bottom)
for (int y = 0; y < lineNum; y++) { for (int y = 0; y < lineNum; y++) {
sb.append(lineNum - y).append(' '); sb.append(lineNum - y).append(' ');
for (int x = 0; x < colNum; x++) { for (int x = 0; x < colNum; x++) {
@ -125,80 +138,47 @@ public class Board {
return sb.toString(); return sb.toString();
} }
// 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() { public ArrayList<Piece> getPieces() {
return pieces; return pieces;
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
if (selectedX == -1) { // No current selection if (selectedX != -1 && x >= 0 && x < colNum && y >= 0 && y < lineNum && isHighlighted(x, y)) {
// Check if there's a piece at the clicked position Piece selectedPiece = getPieceAt(selectedX, selectedY);
for (Piece p : pieces) { if (selectedPiece != null) {
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) {
// Remove any piece at target location
pieces.removeIf(p -> p.getX() == x && p.getY() == y); pieces.removeIf(p -> p.getX() == x && p.getY() == y);
selectedPiece.setX(x);
selectedPiece.setY(y);
// Update piece position
pieceToMove.setX(x);
pieceToMove.setY(y);
// Advance game state
turnNumber++; turnNumber++;
isWhiteTurn = !isWhiteTurn; isWhiteTurn = !isWhiteTurn;
} }
// Clear selection regardless of move success
selectedX = -1; selectedX = -1;
selectedY = -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) { public boolean isSelected(int x, int y) {
return (x == selectedX && y == selectedY); return (x == selectedX && y == selectedY);
} }
/* saving-loading feature :*/
public String[] toFileRep() { public String[] toFileRep() {
//TODO //TODO
return null; return null;
@ -206,29 +186,137 @@ public class Board {
public Board(String[] array) { public Board(String[] array) {
//TODO //TODO
} }
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
//TODO for (Position pos : highlight) {
if (pos.x == x && pos.y == y) {
return true;
}
}
return false; return false;
} }
public void undoLastMove() { public void undoLastMove() {
//TODO //TODO
} }
public Board(Board board) { public Board(Board board) {
//TODO //TODO
} }
public void playMove(Move move) { public void playMove(Move move) {
//TODO //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;
}
}
} }