works well for pawns (need to add others pieces)
This commit is contained in:
parent
d06059cf44
commit
b1c65c9612
|
|
@ -9,6 +9,13 @@ public class Board {
|
|||
private ArrayList<Piece> pieces;
|
||||
private int turnNumber;
|
||||
private boolean turnWhite;
|
||||
private Integer selectedX = null;
|
||||
private Integer selectedY = null;
|
||||
private ArrayList<int[]> highlightedSquares = new ArrayList<>();
|
||||
private ArrayList<Move> moveHistory = new ArrayList<>();
|
||||
|
||||
|
||||
|
||||
|
||||
/*public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
|
|
@ -119,37 +126,210 @@ public class Board {
|
|||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
//TODO
|
||||
Piece clickedPiece = getPieceAt(x, y);
|
||||
|
||||
// No selection yet
|
||||
if (selectedX == null || selectedY == null) {
|
||||
if (clickedPiece != null && clickedPiece.isWhite() == turnWhite) {
|
||||
selectedX = x;
|
||||
selectedY = y;
|
||||
highlightedSquares = computeLegalMoves(clickedPiece);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Clicked the same square again → unselect
|
||||
if (selectedX == x && selectedY == y) {
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedSquares.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise, try to move
|
||||
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||
if (selectedPiece == null) {
|
||||
// Somehow no piece at selected position (safety check)
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedSquares.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if clicked destination is valid
|
||||
boolean validMove = false;
|
||||
for (int[] pos : highlightedSquares) {
|
||||
if (pos[0] == x && pos[1] == y) {
|
||||
validMove = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!validMove) {
|
||||
// Invalid destination → reset
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedSquares.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// Move is valid → capture if needed
|
||||
Piece target = getPieceAt(x, y);
|
||||
|
||||
// Log the move for undo
|
||||
Move move = new Move(
|
||||
selectedPiece.getType(),
|
||||
selectedPiece.isWhite(),
|
||||
selectedX, selectedY,
|
||||
x, y,
|
||||
target // may be null
|
||||
);
|
||||
moveHistory.add(move);
|
||||
|
||||
// Remove captured piece if any
|
||||
if (target != null) {
|
||||
pieces.remove(target);
|
||||
}
|
||||
|
||||
// Remove the original piece
|
||||
pieces.remove(selectedPiece);
|
||||
|
||||
// Add new piece at destination
|
||||
setPiece(selectedPiece.isWhite(), selectedPiece.getType(), x, y);
|
||||
|
||||
// Update turn
|
||||
turnNumber++;
|
||||
turnWhite = !turnWhite;
|
||||
|
||||
// Clear selection and highlights
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedSquares.clear();
|
||||
}
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
//TODO
|
||||
return false;
|
||||
return selectedX != null && selectedY != null && selectedX == x && selectedY == y;
|
||||
}
|
||||
|
||||
|
||||
/* saving-loading feature :*/
|
||||
|
||||
public String[] toFileRep() {
|
||||
//TODO
|
||||
return null;
|
||||
String[] fileRep = new String[height + 1]; // 8 rows + 1 for turn
|
||||
String[][] grid = new String[height][width];
|
||||
|
||||
// Fill with empty squares
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
grid[y][x] = "--";
|
||||
}
|
||||
}
|
||||
|
||||
// Fill with pieces
|
||||
for (Piece piece : pieces) {
|
||||
String color = piece.isWhite() ? "W" : "B";
|
||||
String type = piece.getType().toString().substring(0, 1); // P, R, N, B, Q, K
|
||||
grid[piece.getY()][piece.getX()] = color + type;
|
||||
}
|
||||
|
||||
// Convert grid to CSV-like strings
|
||||
for (int y = 0; y < height; y++) {
|
||||
StringBuilder row = new StringBuilder();
|
||||
for (int x = 0; x < width; x++) {
|
||||
row.append(grid[y][x]);
|
||||
if (x < width - 1) {
|
||||
row.append(",");
|
||||
}
|
||||
}
|
||||
fileRep[y] = row.toString();
|
||||
}
|
||||
|
||||
// Last line = turn
|
||||
fileRep[height] = turnWhite ? "W" : "B";
|
||||
|
||||
return fileRep;
|
||||
}
|
||||
|
||||
|
||||
public Board(String[] array) {
|
||||
//TODO
|
||||
this.width = 8;
|
||||
this.height = 8;
|
||||
this.pieces = new ArrayList<>();
|
||||
this.turnNumber = 0;
|
||||
this.turnWhite = array[8].equals("W");
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
String[] row = array[y].split(",");
|
||||
for (int x = 0; x < width; x++) {
|
||||
String code = row[x];
|
||||
if (!code.equals("--")) {
|
||||
boolean isWhite = code.charAt(0) == 'W';
|
||||
char typeChar = code.charAt(1);
|
||||
PieceType type = null;
|
||||
|
||||
switch (typeChar) {
|
||||
case 'P':
|
||||
type = PieceType.Pawn;
|
||||
break;
|
||||
case 'R':
|
||||
type = PieceType.Rook;
|
||||
break;
|
||||
case 'N':
|
||||
type = PieceType.Knight;
|
||||
break;
|
||||
case 'B':
|
||||
type = PieceType.Bishop;
|
||||
break;
|
||||
case 'Q':
|
||||
type = PieceType.Queen;
|
||||
break;
|
||||
case 'K':
|
||||
type = PieceType.King;
|
||||
break;
|
||||
}
|
||||
|
||||
if (type != null) {
|
||||
setPiece(isWhite, type, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* The following methods require more work ! */
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
//TODO
|
||||
for (int[] pos : highlightedSquares) {
|
||||
if (pos[0] == x && pos[1] == y) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
//TODO
|
||||
if (moveHistory.isEmpty()) return;
|
||||
|
||||
Move lastMove = moveHistory.remove(moveHistory.size() - 1);
|
||||
|
||||
// Remove piece from destination
|
||||
Piece movedPiece = getPieceAt(lastMove.getToX(), lastMove.getToY());
|
||||
if (movedPiece != null) {
|
||||
pieces.remove(movedPiece);
|
||||
}
|
||||
|
||||
// Restore moved piece to original location
|
||||
setPiece(lastMove.isWhite(), lastMove.getType(), lastMove.getFromX(), lastMove.getFromY());
|
||||
|
||||
// Restore captured piece if there was one
|
||||
if (lastMove.getCapturedPiece() != null) {
|
||||
Piece cap = lastMove.getCapturedPiece();
|
||||
setPiece(cap.isWhite(), cap.getType(), cap.getX(), cap.getY());
|
||||
}
|
||||
|
||||
turnNumber--;
|
||||
turnWhite = !turnWhite;
|
||||
}
|
||||
|
||||
public Board(Board board) {
|
||||
|
|
@ -162,4 +342,51 @@ public class Board {
|
|||
|
||||
}
|
||||
|
||||
|
||||
private Piece getPieceAt(int x, int y) {
|
||||
for (Piece p : pieces) {
|
||||
if (p.getX() == x && p.getY() == y) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isEmpty(int x, int y) {
|
||||
return getPieceAt(x, y) == null;
|
||||
}
|
||||
|
||||
private boolean isEnemy(int x, int y, boolean isWhite) {
|
||||
Piece p = getPieceAt(x, y);
|
||||
return p != null && p.isWhite() != isWhite;
|
||||
}
|
||||
|
||||
private ArrayList<int[]> computeLegalMoves(Piece piece) {
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
PieceType type = piece.getType();
|
||||
|
||||
if (type == PieceType.Pawn) {
|
||||
int dir = piece.isWhite() ? 1 : -1;
|
||||
int nextY = y + dir;
|
||||
|
||||
// Move forward if square is empty
|
||||
if (isEmpty(x, nextY)) {
|
||||
moves.add(new int[]{x, nextY});
|
||||
}
|
||||
|
||||
// Diagonal capture
|
||||
if (isEnemy(x - 1, nextY, piece.isWhite())) {
|
||||
moves.add(new int[]{x - 1, nextY});
|
||||
}
|
||||
if (isEnemy(x + 1, nextY, piece.isWhite())) {
|
||||
moves.add(new int[]{x + 1, nextY});
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,27 @@
|
|||
package backend;
|
||||
|
||||
public class Move {
|
||||
private int fromX, fromY;
|
||||
private int toX, toY;
|
||||
private boolean isWhite;
|
||||
private PieceType type;
|
||||
private Piece capturedPiece;
|
||||
|
||||
public Move(PieceType type, boolean isWhite, int fromX, int fromY, int toX, int toY, Piece capturedPiece) {
|
||||
this.fromX = fromX;
|
||||
this.fromY = fromY;
|
||||
this.toX = toX;
|
||||
this.toY = toY;
|
||||
this.isWhite = isWhite;
|
||||
this.type = type;
|
||||
this.capturedPiece = capturedPiece;
|
||||
}
|
||||
|
||||
public int getFromX() { return fromX; }
|
||||
public int getFromY() { return fromY; }
|
||||
public int getToX() { return toX; }
|
||||
public int getToY() { return toY; }
|
||||
public boolean isWhite() { return isWhite; }
|
||||
public PieceType getType() { return type; }
|
||||
public Piece getCapturedPiece() { return capturedPiece; }
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue