working board
This commit is contained in:
parent
487cd37be6
commit
b4a48f6155
|
|
@ -1,6 +1,8 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Board {
|
||||
|
||||
|
|
@ -10,6 +12,9 @@ public class 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;
|
||||
|
|
@ -24,63 +29,60 @@ public class Board {
|
|||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int y = 0; y < getHeight(); y++) {
|
||||
|
|
@ -98,60 +100,145 @@ 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();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void playMove(Move move) {
|
||||
//TODO
|
||||
|
||||
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) {}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue