highlighted ça marche lets go
This commit is contained in:
parent
6e3e5311b8
commit
8eab89ecf9
|
|
@ -1,7 +1,8 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
public class Board {
|
||||
|
||||
private int width;
|
||||
|
|
@ -9,6 +10,29 @@ public class Board {
|
|||
private int turnNumber;
|
||||
private boolean isWhiteTurn;
|
||||
private ArrayList<Piece> pieces;
|
||||
private Set<Position> highlightedPositions = new HashSet<>();
|
||||
private static class Position {
|
||||
int x;
|
||||
int y;
|
||||
|
||||
public Position(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
Position position = (Position) obj;
|
||||
return x == position.x && y == position.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 31 * x + y;
|
||||
}
|
||||
}
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
|
|
@ -16,6 +40,7 @@ public class Board {
|
|||
this.turnNumber = 0;
|
||||
this.isWhiteTurn = true;
|
||||
this.pieces = new ArrayList<>();
|
||||
this.highlightedPositions = new HashSet<>();
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
|
|
@ -140,32 +165,42 @@ public class Board {
|
|||
if (clickedPiece != null && clickedPiece.isWhite() == isWhiteTurn) {
|
||||
selectedX = x;
|
||||
selectedY = y;
|
||||
calculateHighlights(); // Calculate valid moves
|
||||
}
|
||||
} else {
|
||||
if (selectedX == x && selectedY == y) {
|
||||
// Unselection
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
clearHighlights(); // Clear highlights
|
||||
} else {
|
||||
// Attempt to move
|
||||
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||
if (selectedPiece != null) {
|
||||
// Remove captured piece (if any)
|
||||
Piece toRemove = getPieceAt(x, y);
|
||||
if (toRemove != null && toRemove.isWhite() != isWhiteTurn) {
|
||||
pieces.remove(toRemove);
|
||||
// Check if the destination is a valid move
|
||||
if (isHighlighted(x, y)) {
|
||||
// Remove captured piece (if any)
|
||||
Piece toRemove = getPieceAt(x, y);
|
||||
if (toRemove != null) {
|
||||
pieces.remove(toRemove);
|
||||
}
|
||||
|
||||
// Move piece
|
||||
selectedPiece.moveTo(x, y);
|
||||
|
||||
// Advance turn
|
||||
turnNumber++;
|
||||
isWhiteTurn = !isWhiteTurn;
|
||||
|
||||
// Unselect and clear highlights
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
clearHighlights();
|
||||
} else if (clickedPiece != null && clickedPiece.isWhite() == isWhiteTurn) {
|
||||
// Select a different piece of same color
|
||||
selectedX = x;
|
||||
selectedY = y;
|
||||
calculateHighlights(); // Calculate valid moves for new piece
|
||||
}
|
||||
|
||||
// Move piece
|
||||
selectedPiece.moveTo(x, y);
|
||||
|
||||
// Advance turn
|
||||
turnNumber++;
|
||||
isWhiteTurn = !isWhiteTurn;
|
||||
|
||||
// Unselect
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -199,6 +234,7 @@ public class Board {
|
|||
this.turnNumber = Integer.parseInt(firstLine[2]);
|
||||
this.isWhiteTurn = Boolean.parseBoolean(firstLine[3]);
|
||||
this.pieces = new ArrayList<>();
|
||||
this.highlightedPositions = new HashSet<>();
|
||||
|
||||
// Remaining lines: x y PieceType isWhite
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
|
|
@ -219,8 +255,7 @@ public class Board {
|
|||
/* The following methods require more work ! */
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
//TODO
|
||||
return false;
|
||||
return highlightedPositions.contains(new Position(x, y));
|
||||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
|
|
@ -229,10 +264,225 @@ public class Board {
|
|||
}
|
||||
|
||||
public Board(Board board) {
|
||||
//TODO
|
||||
|
||||
this.width = board.width;
|
||||
this.height = board.height;
|
||||
this.turnNumber = board.turnNumber;
|
||||
this.isWhiteTurn = board.isWhiteTurn;
|
||||
|
||||
// Deep copy pieces
|
||||
this.pieces = new ArrayList<>();
|
||||
for (Piece p : board.pieces) {
|
||||
this.pieces.add(new Piece(p.getX(), p.getY(), p.getType(), p.isWhite()));
|
||||
}
|
||||
|
||||
// Copy selection
|
||||
this.selectedX = board.selectedX;
|
||||
this.selectedY = board.selectedY;
|
||||
|
||||
// Copy highlighted positions
|
||||
this.highlightedPositions = new HashSet<>();
|
||||
this.highlightedPositions.addAll(board.highlightedPositions);
|
||||
}
|
||||
|
||||
// Add these methods after userTouch or before isHighlighted
|
||||
private void clearHighlights() {
|
||||
highlightedPositions.clear();
|
||||
}
|
||||
|
||||
private void calculateHighlights() {
|
||||
if (selectedX == null || selectedY == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||
if (selectedPiece == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get valid moves for the selected piece
|
||||
Set<Position> validMoves = getValidMoves(selectedPiece);
|
||||
|
||||
// Update highlighted positions
|
||||
highlightedPositions.clear();
|
||||
highlightedPositions.addAll(validMoves);
|
||||
}
|
||||
// Calculate valid moves for a piece
|
||||
private Set<Position> getValidMoves(Piece piece) {
|
||||
Set<Position> validMoves = new HashSet<>();
|
||||
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
switch (piece.getType()) {
|
||||
case Pawn:
|
||||
addPawnMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case Rook:
|
||||
addRookMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case Knight:
|
||||
addKnightMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case Bishop:
|
||||
addBishopMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case Queen:
|
||||
addRookMoves(validMoves, x, y, isWhite);
|
||||
addBishopMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case King:
|
||||
addKingMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
}
|
||||
|
||||
return validMoves;
|
||||
}
|
||||
|
||||
// Add valid pawn moves
|
||||
private void addPawnMoves(Set<Position> validMoves, int x, int y, boolean isWhite) {
|
||||
int direction = isWhite ? -1 : 1; // White pawns move up, black pawns move down
|
||||
|
||||
// Forward movement
|
||||
int newY = y + direction;
|
||||
if (newY >= 0 && newY < height) {
|
||||
// Single square forward
|
||||
if (getPieceAt(x, newY) == null) {
|
||||
validMoves.add(new Position(x, newY));
|
||||
|
||||
// Initial double move
|
||||
int startRow = isWhite ? 6 : 1;
|
||||
if (y == startRow) {
|
||||
int doubleY = newY + direction;
|
||||
if (doubleY >= 0 && doubleY < height && getPieceAt(x, doubleY) == null) {
|
||||
validMoves.add(new Position(x, doubleY));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Diagonal captures
|
||||
for (int dx = -1; dx <= 1; dx += 2) {
|
||||
int newX = x + dx;
|
||||
if (newX >= 0 && newX < width) {
|
||||
Piece targetPiece = getPieceAt(newX, newY);
|
||||
if (targetPiece != null && targetPiece.isWhite() != isWhite) {
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add valid rook moves
|
||||
private void addRookMoves(Set<Position> validMoves, int x, int y, boolean isWhite) {
|
||||
// Directions: horizontal and vertical
|
||||
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
|
||||
|
||||
for (int[] dir : directions) {
|
||||
int dx = dir[0];
|
||||
int dy = dir[1];
|
||||
|
||||
int newX = x + dx;
|
||||
int newY = y + dy;
|
||||
|
||||
while (newX >= 0 && newX < width && newY >= 0 && newY < height) {
|
||||
Piece targetPiece = getPieceAt(newX, newY);
|
||||
|
||||
if (targetPiece == null) {
|
||||
// Empty square
|
||||
validMoves.add(new Position(newX, newY));
|
||||
} else {
|
||||
// Occupied square
|
||||
if (targetPiece.isWhite() != isWhite) {
|
||||
// Capture opponent's piece
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
break; // Cannot move beyond a piece
|
||||
}
|
||||
|
||||
newX += dx;
|
||||
newY += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add valid knight moves
|
||||
private void addKnightMoves(Set<Position> validMoves, int x, int y, boolean isWhite) {
|
||||
// All possible knight moves: L-shapes
|
||||
int[][] knightMoves = {
|
||||
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
|
||||
{1, -2}, {1, 2}, {2, -1}, {2, 1}
|
||||
};
|
||||
|
||||
for (int[] move : knightMoves) {
|
||||
int newX = x + move[0];
|
||||
int newY = y + move[1];
|
||||
|
||||
if (newX >= 0 && newX < width && newY >= 0 && newY < height) {
|
||||
Piece targetPiece = getPieceAt(newX, newY);
|
||||
|
||||
if (targetPiece == null || targetPiece.isWhite() != isWhite) {
|
||||
// Empty square or can capture opponent's piece
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add valid bishop moves
|
||||
private void addBishopMoves(Set<Position> validMoves, int x, int y, boolean isWhite) {
|
||||
// Directions: diagonals
|
||||
int[][] directions = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
|
||||
|
||||
for (int[] dir : directions) {
|
||||
int dx = dir[0];
|
||||
int dy = dir[1];
|
||||
|
||||
int newX = x + dx;
|
||||
int newY = y + dy;
|
||||
|
||||
while (newX >= 0 && newX < width && newY >= 0 && newY < height) {
|
||||
Piece targetPiece = getPieceAt(newX, newY);
|
||||
|
||||
if (targetPiece == null) {
|
||||
// Empty square
|
||||
validMoves.add(new Position(newX, newY));
|
||||
} else {
|
||||
// Occupied square
|
||||
if (targetPiece.isWhite() != isWhite) {
|
||||
// Capture opponent's piece
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
break; // Cannot move beyond a piece
|
||||
}
|
||||
|
||||
newX += dx;
|
||||
newY += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add valid king moves
|
||||
private void addKingMoves(Set<Position> validMoves, int x, int y, boolean isWhite) {
|
||||
// All adjacent squares
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
if (dx == 0 && dy == 0) continue; // Skip the current position
|
||||
|
||||
int newX = x + dx;
|
||||
int newY = y + dy;
|
||||
|
||||
if (newX >= 0 && newX < width && newY >= 0 && newY < height) {
|
||||
Piece targetPiece = getPieceAt(newX, newY);
|
||||
|
||||
if (targetPiece == null || targetPiece.isWhite() != isWhite) {
|
||||
// Empty square or can capture opponent's piece
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void playMove(Move move) {
|
||||
//TODO
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue